Example #1
0
  static Method getFromNativeMethod(FromNativeType fromNativeType, AsmClassLoader classLoader) {
    FromNativeConverter fromNativeConverter = fromNativeType.getFromNativeConverter();
    if (fromNativeConverter == null) {
      return null;
    }

    try {
      Class<? extends FromNativeConverter> fromNativeConverterClass =
          fromNativeConverter.getClass();
      if (Modifier.isPublic(fromNativeConverterClass.getModifiers())) {
        for (Method method : fromNativeConverterClass.getMethods()) {
          if (!method.getName().equals("fromNative")) continue;
          Class[] methodParameterTypes = method.getParameterTypes();
          Class javaType =
              fromNativeType.getDeclaredType().isPrimitive()
                  ? boxedType(fromNativeType.getDeclaredType())
                  : fromNativeType.getDeclaredType();
          if (javaType.isAssignableFrom(method.getReturnType())
              && methodParameterTypes.length == 2
              && methodParameterTypes[0].isAssignableFrom(fromNativeConverter.nativeType())
              && methodParameterTypes[1] == FromNativeContext.class
              && methodIsAccessible(method)
              && classIsVisible(classLoader, method.getDeclaringClass())) {
            return method;
          }
        }
      }
      Method method =
          fromNativeConverterClass.getMethod("fromNative", Object.class, FromNativeContext.class);
      return methodIsAccessible(method) && classIsVisible(classLoader, method.getDeclaringClass())
          ? method
          : FromNativeConverter.class.getDeclaredMethod(
              "fromNative", Object.class, FromNativeContext.class);

    } catch (NoSuchMethodException nsme) {
      try {
        return FromNativeConverter.class.getDeclaredMethod(
            "fromNative", Object.class, FromNativeContext.class);
      } catch (NoSuchMethodException nsme2) {
        throw new RuntimeException(
            "internal error. " + FromNativeConverter.class + " has no fromNative() method");
      }
    }
  }
Example #2
0
  static Method getToNativeMethod(ToNativeType toNativeType, AsmClassLoader classLoader) {
    ToNativeConverter toNativeConverter = toNativeType.getToNativeConverter();
    if (toNativeConverter == null) {
      return null;
    }

    try {
      Class<? extends ToNativeConverter> toNativeConverterClass = toNativeConverter.getClass();
      if (Modifier.isPublic(toNativeConverterClass.getModifiers())) {
        for (Method method : toNativeConverterClass.getMethods()) {
          if (!method.getName().equals("toNative")) continue;
          Class[] methodParameterTypes = method.getParameterTypes();
          if (toNativeConverter.nativeType().isAssignableFrom(method.getReturnType())
              && methodParameterTypes.length == 2
              && methodParameterTypes[0].isAssignableFrom(toNativeType.getDeclaredType())
              && methodParameterTypes[1] == ToNativeContext.class
              && methodIsAccessible(method)
              && classIsVisible(classLoader, method.getDeclaringClass())) {
            return method;
          }
        }
      }
      Method method =
          toNativeConverterClass.getMethod("toNative", Object.class, ToNativeContext.class);
      return methodIsAccessible(method) && classIsVisible(classLoader, method.getDeclaringClass())
          ? method
          : ToNativeConverter.class.getDeclaredMethod(
              "toNative", Object.class, ToNativeContext.class);

    } catch (NoSuchMethodException nsme) {
      try {
        return ToNativeConverter.class.getDeclaredMethod(
            "toNative", Object.class, ToNativeContext.class);
      } catch (NoSuchMethodException nsme2) {
        throw new RuntimeException(
            "internal error. " + ToNativeConverter.class + " has no toNative() method");
      }
    }
  }