1 35 package org.jruby.javasupport; 36 37 import java.lang.reflect.AccessibleObject ; 38 import java.lang.reflect.Constructor ; 39 import java.lang.reflect.InvocationTargetException ; 40 41 import org.jruby.Ruby; 42 import org.jruby.RubyClass; 43 import org.jruby.RubyModule; 44 import org.jruby.runtime.CallbackFactory; 45 import org.jruby.runtime.ObjectAllocator; 46 import org.jruby.runtime.builtin.IRubyObject; 47 48 public class JavaConstructor extends JavaCallable { 49 private Constructor constructor; 50 51 public static RubyClass createJavaConstructorClass(Ruby runtime, RubyModule javaModule) { 52 RubyClass result = 55 javaModule.defineClassUnder("JavaConstructor", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 56 CallbackFactory callbackFactory = runtime.callbackFactory(JavaConstructor.class); 57 58 JavaCallable.registerRubyMethods(runtime, result, JavaConstructor.class); 59 result.defineFastMethod("arity", callbackFactory.getFastMethod("arity")); 60 result.defineFastMethod("inspect", callbackFactory.getFastMethod("inspect")); 61 result.defineFastMethod("argument_types", callbackFactory.getFastMethod("argument_types")); 62 result.defineFastMethod("new_instance", callbackFactory.getFastOptMethod("new_instance")); 63 64 return result; 65 } 66 67 public JavaConstructor(Ruby runtime, Constructor constructor) { 68 super(runtime, runtime.getModule("Java").getClass("JavaConstructor")); 69 this.constructor = constructor; 70 } 71 72 public int getArity() { 73 return constructor.getParameterTypes().length; 74 } 75 76 public IRubyObject new_instance(IRubyObject[] args) { 77 if (args.length != getArity()) { 78 throw getRuntime().newArgumentError(args.length, getArity()); 79 } 80 Object [] constructorArguments = new Object [args.length]; 81 Class [] types = constructor.getParameterTypes(); 82 for (int i = 0; i < args.length; i++) { 83 constructorArguments[i] = JavaUtil.convertArgument(args[i], types[i]); 84 } 85 try { 86 Object result = constructor.newInstance(constructorArguments); 87 return JavaObject.wrap(getRuntime(), result); 88 89 } catch (IllegalArgumentException iae) { 90 throw getRuntime().newTypeError("expected " + argument_types().inspect() + 91 ", got [" + constructorArguments[0].getClass().getName() + ", ...]"); 92 } catch (IllegalAccessException iae) { 93 throw getRuntime().newTypeError("illegal access"); 94 } catch (InvocationTargetException ite) { 95 getRuntime().getJavaSupport().handleNativeException(ite.getTargetException()); 96 assert false; 98 return null; 99 } catch (InstantiationException ie) { 100 throw getRuntime().newTypeError("can't make instance of " + constructor.getDeclaringClass().getName()); 101 } 102 } 103 104 105 protected String nameOnInspection() { 106 return getType().toString(); 107 } 108 109 protected Class [] parameterTypes() { 110 return constructor.getParameterTypes(); 111 } 112 113 protected int getModifiers() { 114 return constructor.getModifiers(); 115 } 116 117 protected AccessibleObject accesibleObject() { 118 return constructor; 119 } 120 } 121 | Popular Tags |