static boolean derivesFrom(Class<?> c, String className) { while (c != null) { if (c.getName().equals(className)) return true; c = c.getSuperclass(); } return false; }
@Override public String toString() { StringBuilder b = new StringBuilder(); for (Class<?> ann : annotations) { b.append(ann.getSimpleName()).append(' '); } b.append((type instanceof Class<?>) ? ((Class<?>) type).getSimpleName() : type.toString()); return b.toString(); }
static boolean hasInstance(Object[] array, Class<? extends Annotation>... cs) { for (Object o : array) { for (Class<?> c : cs) { if (c.isInstance(o)) { return true; } } } return false; }
static int getArgumentsStackSize(Method method) { int total = 0; Type[] paramTypes = method.getGenericParameterTypes(); Annotation[][] anns = method.getParameterAnnotations(); for (int iArg = 0, nArgs = paramTypes.length; iArg < nArgs; iArg++) { Class<?> paramType = getTypeClass(paramTypes[iArg]); if (paramType == int.class) { total += 4; } else if (paramType == long.class) { Annotation[] as = anns[iArg]; if (isAnnotationPresent(Ptr.class, as) || isAnnotationPresent( org.bridj.ann.CLong.class, as)) // if (hasInstance(anns[iArg], Ptr.class, CLong.class)) { total += Pointer.SIZE; } else { total += 8; } } else if (paramType == float.class) { total += 4; } else if (paramType == double.class) { total += 8; } else if (paramType == byte.class) { total += 1; } else if (paramType == char.class) { total += Platform.WCHAR_T_SIZE; } else if (paramType == CLong.class) { total += Platform.CLONG_SIZE; } else if (paramType == SizeT.class) { total += Platform.SIZE_T_SIZE; } else if (paramType == TimeT.class) { total += Platform.TIME_T_SIZE; } else if (paramType == short.class) { total += 2; } else if (paramType == boolean.class) { total += 1; } else if (Pointer.class.isAssignableFrom(paramType)) { total += Pointer.SIZE; } else if (NativeObject.class.isAssignableFrom(paramType)) { total += ((CRuntime) BridJ.getRuntime(paramType)).sizeOf(paramTypes[iArg], null); } else if (FlagSet.class.isAssignableFrom(paramType)) { total += 4; // TODO } else { throw new RuntimeException("Type not handled : " + paramType.getName()); } } return total; }
public boolean matchesEnclosingType(Method method) { TypeRef et = getEnclosingType(); if (et == null) { return true; } Annotations annotations = annotations(method); Class dc = method.getDeclaringClass(); do { if (et.matches(dc, annotations)) { return true; } dc = dc.getSuperclass(); } while (dc != null && dc != Object.class); return false; }
@Override public boolean matches(Type type, Annotations annotations) { Class<?> tc = getTypeClass(this.type); Class<?> typec = getTypeClass(type); if (tc == typec || tc.equals(typec)) { return true; } if ((type == Long.TYPE && Pointer.class.isAssignableFrom(tc)) || (Pointer.class.isAssignableFrom(typec) && tc == Long.TYPE)) { return true; } return equivalentTypes( type, typec, annotations, this.type, tc, null); // TODO isAssignableFrom or the opposite, depending on context }
public boolean matchesDestructor(Class<?> type) { if (!symbol.contains(type.getSimpleName())) { return false; } parse(); try { if (ref != null) { return ref.matchesDestructor(type); } } catch (Exception ex) { ex.printStackTrace(); } return false; }
public void appendToSignature( int iParam, ValueType type, Class<?> parameterType, Type genericParameterType, StringBuilder javaSig, StringBuilder dcSig, StringBuilder asmSig) { char dcChar; String javaChar, asmChar = null; switch (type) { case eVoidValue: dcChar = DC_SIGCHAR_VOID; javaChar = "V"; break; case eIntValue: dcChar = DC_SIGCHAR_INT; javaChar = "I"; break; case eLongValue: dcChar = DC_SIGCHAR_LONGLONG; javaChar = "J"; break; case eSizeTValue: javaChar = "J"; if (Platform.SIZE_T_SIZE == 8) { dcChar = DC_SIGCHAR_LONGLONG; } else { dcChar = DC_SIGCHAR_INT; direct = false; } break; case eShortValue: dcChar = DC_SIGCHAR_SHORT; javaChar = "S"; break; case eDoubleValue: dcChar = DC_SIGCHAR_DOUBLE; javaChar = "D"; break; case eFloatValue: dcChar = DC_SIGCHAR_FLOAT; javaChar = "F"; break; case eByteValue: dcChar = DC_SIGCHAR_CHAR; javaChar = "B"; break; case eBooleanValue: dcChar = DC_SIGCHAR_BOOL; javaChar = "Z"; break; case eWCharValue: switch (Platform.WCHAR_T_SIZE) { case 1: dcChar = DC_SIGCHAR_CHAR; direct = false; break; case 2: dcChar = DC_SIGCHAR_SHORT; break; case 4: dcChar = DC_SIGCHAR_INT; direct = false; break; default: throw new RuntimeException( "Unhandled sizeof(wchar_t) in GetJavaTypeSignature: " + Platform.WCHAR_T_SIZE); } javaChar = "C"; break; case eIntFlagSet: dcChar = DC_SIGCHAR_INT; javaChar = "L" + parameterType.getName().replace('.', '/') + ";"; // "Lorg/bridj/ValuedEnum;"; direct = false; break; case eCLongObjectValue: dcChar = DC_SIGCHAR_POINTER; javaChar = "Lorg/bridj/CLong;"; direct = false; break; case eSizeTObjectValue: dcChar = DC_SIGCHAR_POINTER; javaChar = "Lorg/bridj/SizeT;"; direct = false; break; case eTimeTObjectValue: dcChar = DC_SIGCHAR_POINTER; javaChar = "Lorg/bridj/TimeT;"; direct = false; break; case ePointerValue: dcChar = DC_SIGCHAR_POINTER; javaChar = "L" + parameterType.getName().replace('.', '/') + ";"; // javaChar = "Lorg/bridj/Pointer;"; direct = false; break; case eNativeObjectValue: dcChar = DC_SIGCHAR_STRUCT; // TODO : unroll struct signature ? javaChar = "L" + parameterType.getName().replace('.', '/') + ";"; direct = false; // if (parameterType.equals(declaringClass)) { // // special case of self-returning pointers // dcChar = DC_SIGCHAR_POINTER; break; case eEllipsis: javaChar = "[Ljava/lang/Object;"; dcChar = '?'; break; default: direct = false; throw new RuntimeException("Unhandled " + ValueType.class.getSimpleName() + ": " + type); } if (genericParameterType instanceof ParameterizedType && iParam < 0) { ParameterizedType pt = (ParameterizedType) genericParameterType; // TODO handle all cases !!! Type[] ts = pt.getActualTypeArguments(); if (ts != null && ts.length == 1) { Type t = ts[0]; if (t instanceof ParameterizedType) t = ((ParameterizedType) t).getRawType(); if (t instanceof Class) { Class c = (Class) t; if (javaChar.endsWith(";")) { asmChar = javaChar.substring(0, javaChar.length() - 1) + "<*L" + c.getName().replace('.', '/') + ";>"; // asmChar += ";"; } } } } if (javaSig != null) javaSig.append(javaChar); if (asmChar == null) asmChar = javaChar; if (asmSig != null) asmSig.append(asmChar); if (dcSig != null) dcSig.append(dcChar); }
public ValueType getValueType( int iParam, int nParams, Class<?> c, Type t, AnnotatedElement element, Annotation... directAnnotations) { boolean isPtr = isAnnotationPresent(Ptr.class, element, directAnnotations); boolean isCLong = isAnnotationPresent(org.bridj.ann.CLong.class, element, directAnnotations); Constructor cons = getAnnotation(Constructor.class, element, directAnnotations); if (isPtr || cons != null || isCLong) { if (!(c == Long.class || c == Long.TYPE)) throw new RuntimeException( "Annotation should only be used on a long parameter, not on a " + c.getName()); if (isPtr) { if (!Platform.is64Bits()) direct = false; } else if (isCLong) { if (Platform.CLONG_SIZE != 8) direct = false; } else if (cons != null) { isCPlusPlus = true; startsWithThis = true; if (iParam != 0) throw new RuntimeException( "Annotation " + Constructor.class.getName() + " cannot have more than one (long) argument"); } return ValueType.eSizeTValue; } if (c == null || c.equals(Void.TYPE)) return ValueType.eVoidValue; if (c == Integer.class || c == Integer.TYPE) return ValueType.eIntValue; if (c == Long.class || c == Long.TYPE) { return !isPtr || Platform.is64Bits() ? ValueType.eLongValue : ValueType.eIntValue; } if (c == Short.class || c == Short.TYPE) return ValueType.eShortValue; if (c == Byte.class || c == Byte.TYPE) return ValueType.eByteValue; if (c == Boolean.class || c == Boolean.TYPE) return ValueType.eBooleanValue; if (c == Float.class || c == Float.TYPE) { usesFloats(); return ValueType.eFloatValue; } if (c == char.class || c == Character.TYPE) { if (Platform.WCHAR_T_SIZE != 2) direct = false; return ValueType.eWCharValue; } if (c == Double.class || c == Double.TYPE) { usesFloats(); return ValueType.eDoubleValue; } if (c == CLong.class) { direct = false; return ValueType.eCLongObjectValue; } if (c == SizeT.class) { direct = false; return ValueType.eSizeTObjectValue; } if (c == TimeT.class) { direct = false; return ValueType.eTimeTObjectValue; } if (Pointer.class.isAssignableFrom(c)) { direct = false; CallIO cio = CallIO.Utils.createPointerCallIO(c, t); if (BridJ.veryVerbose) BridJ.info("CallIO : " + cio); addCallIO(cio); return ValueType.ePointerValue; } if (c.isArray() && iParam == nParams - 1) { direct = false; return ValueType.eEllipsis; } if (ValuedEnum.class.isAssignableFrom(c)) { direct = false; CallIO cio = CallIO.Utils.createValuedEnumCallIO( (Class) Utils.getClass(Utils.getUniqueParameterizedTypeParameter(t))); if (BridJ.veryVerbose) BridJ.info("CallIO : " + cio); addCallIO(cio); return ValueType.eIntFlagSet; } if (NativeObject.class.isAssignableFrom(c)) { Pointer<DCstruct> pStruct = null; if (StructObject.class.isAssignableFrom(c)) { StructIO io = StructIO.getInstance(c, t); try { pStruct = DyncallStructs.buildDCstruct(io.desc); } catch (Throwable th) { BridJ.error( "Unable to create low-level struct metadata for " + Utils.toString(t) + " : won't be able to use it as a by-value function argument.", th); } } addCallIO(new CallIO.NativeObjectHandler((Class<? extends NativeObject>) c, t, pStruct)); direct = false; return ValueType.eNativeObjectValue; } throw new NoSuchElementException( "No " + ValueType.class.getSimpleName() + " for class " + c.getName()); }
protected void init( AnnotatedElement annotatedElement, Class returnType, Type genericReturnType, Annotation[] returnAnnotations, Class[] parameterTypes, Type[] genericParameterTypes, Annotation[][] paramsAnnotations, boolean prependJNIPointers, boolean isVirtual, boolean isDirectModeAllowed) { assert returnType != null; assert genericReturnType != null; assert parameterTypes != null; assert genericParameterTypes != null; assert returnAnnotations != null; assert parameterTypes.length == genericParameterTypes.length; assert paramsAnnotations.length == genericParameterTypes.length; int nParams = genericParameterTypes.length; paramsValueTypes = new int[nParams]; direct = isDirectModeAllowed; // TODO on native side : test number of parameters (on 64 bits win : // must be <= 4) StringBuilder javaSig = new StringBuilder(64), asmSig = new StringBuilder(64), dcSig = new StringBuilder(16); javaSig.append('('); asmSig.append('('); if (prependJNIPointers) // !isCPlusPlus) dcSig .append(DC_SIGCHAR_POINTER) .append(DC_SIGCHAR_POINTER); // JNIEnv*, jobject: always present in native-bound functions if (BridJ.debug) BridJ.info( "Analyzing " + (declaringClass == null ? "anonymous method" : declaringClass.getName() + "." + methodName)); if (isObjCBlock) appendToSignature( 0, ValueType.ePointerValue, Pointer.class, Pointer.class, null, dcSig, null); for (int iParam = 0; iParam < nParams; iParam++) { // Options paramOptions = paramsOptions[iParam] = new Options(); Type genericParameterType = genericParameterTypes[iParam]; Class<?> parameterType = parameterTypes[iParam]; ValueType paramValueType = getValueType( iParam, nParams, parameterType, genericParameterType, null, paramsAnnotations[iParam]); if (BridJ.veryVerbose) BridJ.info("\tparam " + paramValueType); paramsValueTypes[iParam] = paramValueType.ordinal(); appendToSignature( iParam, paramValueType, parameterType, genericParameterType, javaSig, dcSig, asmSig); } javaSig.append(')'); asmSig.append(')'); dcSig.append(')'); ValueType retType = getValueType( -1, nParams, returnType, genericReturnType, annotatedElement, returnAnnotations); if (BridJ.veryVerbose) BridJ.info("\treturns " + retType); appendToSignature(-1, retType, returnType, genericReturnType, javaSig, dcSig, asmSig); returnValueType = retType.ordinal(); javaSignature = javaSig.toString(); asmSignature = asmSig.toString(); dcSignature = dcSig.toString(); isCPlusPlus = isCPlusPlus || isVirtual; if (isCPlusPlus && !isStatic) { if (!startsWithThis) direct = false; bNeedsThisPointer = true; if (Platform.isWindows()) { if (!Platform.is64Bits()) setDcCallingConvention(DC_CALL_C_X86_WIN32_THIS_MS); } else { // if (!Platform.is64Bits()) // setDcCallingConvention(DC_CALL_C_X86_WIN32_THIS_GNU); } } if (nParams > Platform.getMaxDirectMappingArgCount()) this.direct = false; if (BridJ.veryVerbose) { BridJ.info("\t-> direct " + direct); BridJ.info("\t-> javaSignature " + javaSignature); BridJ.info("\t-> callIOs " + callIOs); BridJ.info("\t-> asmSignature " + asmSignature); BridJ.info("\t-> dcSignature " + dcSignature); } if (BridJ.veryVerbose) BridJ.info((direct ? "[mappable as direct] " : "[not mappable as direct] ") + method); }
public boolean matches(Type type, Annotations annotations) { String thisName = getQualifiedName(new StringBuilder(), false).toString(); Class typeClass = getTypeClass(type); String typeName = typeClass.getSimpleName(); return thisName.equals(typeName) || thisName.equals(typeClass.getName()); }