Example #1
0
  private static void findFields(
      ThreadContext context,
      RubyModule topModule,
      IRubyObject args[],
      boolean asReader,
      boolean asWriter) {
    Map<String, String> fieldMap = getFieldListFromArgs(args);

    for (RubyModule module = topModule; module != null; module = module.getSuperClass()) {
      Class<?> javaClass = getJavaClass(context, module);

      // Hit a non-java proxy class (included Modules can be a cause of this...skip)
      if (javaClass == null) continue;

      Field[] fields = JavaClass.getDeclaredFields(javaClass);
      for (int j = 0; j < fields.length; j++) {
        installField(context, fieldMap, fields[j], module, asReader, asWriter);
      }
    }

    // We could not find all of them print out first one (we could print them all?)
    if (!fieldMap.isEmpty()) {
      throw JavaClass.undefinedFieldError(
          context.getRuntime(), topModule.getName(), fieldMap.keySet().iterator().next());
    }
  }
 protected RubyArray buildRubyArray(Class[] classes) {
   RubyArray result = getRuntime().newArray(classes.length);
   for (int i = 0; i < classes.length; i++) {
     result.append(JavaClass.get(getRuntime(), classes[i]));
   }
   return result;
 }
Example #3
0
  @JRubyMethod
  public static IRubyObject to_java(
      ThreadContext context, IRubyObject fromObject, IRubyObject type) {
    if (type.isNil()) {
      return to_java(context, fromObject);
    }

    Ruby runtime = context.runtime;
    JavaClass targetType = getTargetType(context, runtime, type);

    if (fromObject instanceof RubyArray) {
      return targetType.javaArrayFromRubyArray(context, fromObject);
    } else {
      return Java.getInstance(runtime, fromObject.toJava(targetType.javaClass()));
    }
  }
Example #4
0
  private static JavaClass getTargetType(ThreadContext context, Ruby runtime, IRubyObject type) {
    JavaClass targetType;

    if (type instanceof RubyString || type instanceof RubySymbol) {
      targetType = runtime.getJavaSupport().getNameClassMap().get(type.asJavaString());
      if (targetType == null) targetType = JavaClass.forNameVerbose(runtime, type.asJavaString());
    } else if (type instanceof RubyModule && type.respondsTo("java_class")) {
      targetType = (JavaClass) Helpers.invoke(context, type, "java_class");
    } else if (type instanceof JavaProxy) {
      if (((JavaProxy) type).getObject() instanceof Class) {
        targetType = JavaClass.get(runtime, (Class) ((JavaProxy) type).getObject());
      } else {
        throw runtime.newTypeError("not a valid target type: " + type);
      }
    } else {
      throw runtime.newTypeError("unable to convert to type: " + type);
    }

    return targetType;
  }
 @JRubyMethod
 public IRubyObject java_class() {
   return JavaClass.get(getRuntime(), getJavaClass());
 }
Example #6
0
 private static boolean assignable(Class type, IRubyObject arg) {
   return JavaClass.assignable(type, argClass(arg));
 }