1 28 package org.jruby.internal.runtime.methods; 29 30 import java.lang.reflect.InvocationTargetException ; 31 import java.lang.reflect.Method ; 32 import java.util.Arrays ; 33 34 import org.jruby.Ruby; 35 import org.jruby.RubyModule; 36 import org.jruby.exceptions.JumpException; 37 import org.jruby.exceptions.MainExitException; 38 import org.jruby.exceptions.RaiseException; 39 import org.jruby.exceptions.ThreadKill; 40 import org.jruby.runtime.Arity; 41 import org.jruby.runtime.Block; 42 import org.jruby.runtime.ThreadContext; 43 import org.jruby.runtime.Visibility; 44 import org.jruby.runtime.builtin.IRubyObject; 45 46 public class SimpleReflectedMethod extends DynamicMethod { 47 private Method method; 48 private Class type; 49 private String methodName; 50 private Arity arity; 51 52 public SimpleReflectedMethod(RubyModule implementationClass, Class type, String methodName, 53 Arity arity, Visibility visibility) { 54 super(implementationClass, visibility); 55 this.type = type; 56 this.methodName = methodName; 57 this.arity = arity; 58 59 assert type != null; 60 assert methodName != null; 61 assert arity != null; 62 63 Class [] parameterTypes; 64 if (arity.isFixed()) { 65 parameterTypes = new Class [arity.getValue()]; 66 Arrays.fill(parameterTypes, IRubyObject.class); 67 } else { 68 parameterTypes = new Class [1]; 69 parameterTypes[0] = IRubyObject[].class; 70 } 71 try { 72 method = type.getMethod(methodName, parameterTypes); 73 } catch (NoSuchMethodException e) { 74 assert false : e; 75 } catch (SecurityException e) { 76 assert false : e; 77 } 78 79 assert method != null; 80 } 81 82 public void preMethod(ThreadContext context, RubyModule klazz, IRubyObject self, String name, IRubyObject[] args, boolean noSuper, Block block) { 83 } 84 85 public void postMethod(ThreadContext context) { 86 } 87 88 public IRubyObject internalCall(ThreadContext context, RubyModule klazz, IRubyObject self, String name, IRubyObject[] args, boolean noSuper, Block block) { 89 assert false; 90 return null; 91 } 92 93 public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String name, IRubyObject[] args, boolean noSuper, Block block) { 94 Ruby runtime = context.getRuntime(); 95 arity.checkArity(runtime, args); 96 97 assert self != null; 98 assert args != null; 99 assert method != null; 100 101 Object [] methodArgs = !arity.isFixed() ? new Object []{args} : args; 102 103 try { 104 return (IRubyObject) method.invoke(self, methodArgs); 105 } catch (InvocationTargetException e) { 106 if (e.getTargetException() instanceof RaiseException) { 107 throw (RaiseException) e.getTargetException(); 108 } else if (e.getTargetException() instanceof JumpException) { 109 throw (JumpException) e.getTargetException(); 110 } else if (e.getTargetException() instanceof ThreadKill) { 111 throw (ThreadKill) e.getTargetException(); 113 } else if (e.getTargetException() instanceof Exception ) { 114 if(e.getTargetException() instanceof MainExitException) { 115 throw (RuntimeException )e.getTargetException(); 116 } 117 runtime.getJavaSupport().handleNativeException(e.getTargetException()); 118 return runtime.getNil(); 119 } else { 120 throw (Error ) e.getTargetException(); 121 } 122 } catch (IllegalAccessException e) { 123 StringBuffer message = new StringBuffer (); 124 message.append(e.getMessage()); 125 message.append(':'); 126 message.append(" methodName=").append(methodName); 127 message.append(" recv=").append(self.toString()); 128 message.append(" type=").append(type.getName()); 129 message.append(" methodArgs=["); 130 for (int i = 0; i < methodArgs.length; i++) { 131 message.append(methodArgs[i]); 132 message.append(' '); 133 } 134 message.append(']'); 135 assert false : message.toString(); 136 return null; 137 } catch (final IllegalArgumentException e) { 138 150 assert false : e; 151 return null; 152 } 153 } 154 155 public DynamicMethod dup() { 156 SimpleReflectedMethod newMethod = 157 new SimpleReflectedMethod(getImplementationClass(), type, methodName, arity, getVisibility()); 158 159 newMethod.method = method; 160 161 return newMethod; 162 } 163 164 public Arity getArity() { 166 return arity; 167 } 168 } 169 | Popular Tags |