private static Class<?> nativeType(Class<?> cls) { return NativeMappedConverter.getInstance(cls).nativeType(); } // Called from native code
private Class<?> getNativeType(Class<?> cls) { if (Structure.class.isAssignableFrom(cls)) { // Make sure we can instantiate an argument of this type Structure.validate((Class<? extends Structure>)cls); if (!Structure.ByValue.class.isAssignableFrom(cls)) return Pointer.class; } else if (NativeMapped.class.isAssignableFrom(cls)) { return NativeMappedConverter.getInstance(cls).nativeType(); } else if (cls == String.class || cls == WString.class || cls == String[].class || cls == WString[].class || Callback.class.isAssignableFrom(cls)) { return Pointer.class; } return cls; }
private static NativeMapped fromNative(Method m, Object value) { Class<?> cls = m.getReturnType(); return (NativeMapped)NativeMappedConverter.getInstance(cls).fromNative(value, new MethodResultContext(cls, null, null, m)); } // Called from native code
/** Override this to handle any custom class mappings. * @param cls Java class of a parameter * @return number of native bytes used for this class on the stack */ protected int getArgumentNativeStackSize(Class<?> cls) { if (NativeMapped.class.isAssignableFrom(cls)) { cls = NativeMappedConverter.getInstance(cls).nativeType(); } if (cls.isArray()) { return Native.POINTER_SIZE; } try { return Native.getNativeSize(cls); } catch(IllegalArgumentException e) { throw new IllegalArgumentException("Unknown native stack allocation size for " + cls); } }
private static NativeMapped fromNative(Class<?> cls, Object value) { // NOTE: technically should be CallbackParameterContext return (NativeMapped)NativeMappedConverter.getInstance(cls).fromNative(value, new FromNativeContext(cls)); } // Called from native code
public DefaultCallbackProxy(Method callbackMethod, TypeMapper mapper, String encoding) { this.callbackMethod = callbackMethod; this.encoding = encoding; Class<?>[] argTypes = callbackMethod.getParameterTypes(); Class<?> returnType = callbackMethod.getReturnType(); fromNative = new FromNativeConverter[argTypes.length]; if (NativeMapped.class.isAssignableFrom(returnType)) { toNative = NativeMappedConverter.getInstance(returnType); } else if (mapper != null) { toNative = mapper.getToNativeConverter(returnType); } for (int i=0;i < fromNative.length;i++) { if (NativeMapped.class.isAssignableFrom(argTypes[i])) { fromNative[i] = new NativeMappedConverter(argTypes[i]); } else if (mapper != null) { fromNative[i] = mapper.getFromNativeConverter(argTypes[i]); } } if (!callbackMethod.isAccessible()) { try { callbackMethod.setAccessible(true); } catch(SecurityException e) { throw new IllegalArgumentException("Callback method is inaccessible, make sure the interface is public: " + callbackMethod); } } }
private Object initializeField(Field field, Class<?> type) { Object value = null; if (Structure.class.isAssignableFrom(type) && !(ByReference.class.isAssignableFrom(type))) { try { value = newInstance((Class<? extends Structure>) type, PLACEHOLDER_MEMORY); setFieldValue(field, value); } catch(IllegalArgumentException e) { String msg = "Can't determine size of nested structure"; throw new IllegalArgumentException(msg, e); } } else if (NativeMapped.class.isAssignableFrom(type)) { NativeMappedConverter tc = NativeMappedConverter.getInstance(type); value = tc.defaultValue(); setFieldValue(field, value); } return value; }
cls = NativeMappedConverter.getInstance(cls).nativeType();
Class<?> nativeType = NativeMappedConverter.getInstance(type).nativeType(); if (nativeType == String.class) { return CVT_NATIVE_MAPPED_STRING;
int alignment = 1; if (NativeMapped.class.isAssignableFrom(type)) { NativeMappedConverter tc = NativeMappedConverter.getInstance(type); type = tc.nativeType(); value = tc.toNative(value, new ToNativeContext());
NativeMappedConverter tc = NativeMappedConverter.getInstance(type); nativeType = tc.nativeType(); structField.writeConverter = tc;
NativeMappedConverter c = NativeMappedConverter.getInstance(cls); return get(c.toNative(obj, new ToNativeContext()), c.nativeType());
FromNativeConverter resultConverter = null; if (NativeMapped.class.isAssignableFrom(returnType)) { NativeMappedConverter tc = NativeMappedConverter.getInstance(returnType); resultConverter = tc; nativeReturnType = tc.nativeType();
setPointer(offset, p); } else if (NativeMapped.class.isAssignableFrom(type)) { NativeMappedConverter tc = NativeMappedConverter.getInstance(type); Class<?> nativeType = tc.nativeType(); setValue(offset, tc.toNative(value, new ToNativeContext()), nativeType);
case CVT_POINTER_TYPE: closure_rtype = FFIType.get(Pointer.class).peer; rtype = FFIType.get(NativeMappedConverter.getInstance(rclass).nativeType()).peer; break; case CVT_STRUCTURE: || (conversionType == CVT_NATIVE_MAPPED_WSTRING) || (conversionType == CVT_INTEGER_TYPE)) { type = NativeMappedConverter.getInstance(type).nativeType(); } else if ((conversionType == CVT_TYPE_MAPPER) || (conversionType == CVT_TYPE_MAPPER_STRING)
ToNativeConverter converter = null; if (NativeMapped.class.isAssignableFrom(type)) { converter = NativeMappedConverter.getInstance(type); } else if (mapper != null) { converter = mapper.getToNativeConverter(type);
NativeMappedConverter tc = NativeMappedConverter.getInstance(type); Object value = getValue(offset, tc.nativeType(), null); result = tc.fromNative(value, new FromNativeContext(type));
NativeMappedConverter tc = NativeMappedConverter.getInstance(cls); int size = Native.getNativeSize(result.getClass(), result) / array.length; for (int i=0;i < array.length;i++) {
NativeMappedConverter tc = NativeMappedConverter.getInstance(cls); Class<?> nativeType = tc.nativeType(); int size = Native.getNativeSize(value.getClass(), value) / buf.length;
private static NativeMapped fromNative(Method m, Object value) { Class<?> cls = m.getReturnType(); return (NativeMapped)NativeMappedConverter.getInstance(cls).fromNative(value, new MethodResultContext(cls, null, null, m)); } // Called from native code