/** Find invocable for the given name and arguments. * Arguments are converted to type parameters. * @param name of member * @param args parameter argument values * @return the most specific member or null */ public Invocable findMethod(String name, Object... args) { return findMethod(name, Types.getTypes(args)); }
/** Produce a method string from supplied value args. * @param name method name. * @param args the value to process. * @return still just a method string. */ public static String methodString(String name, Object[] args) { return methodString(name, Types.getTypes(args)); }
/** * This method wraps resolveJavaMethod() and expects a non-null method * result. If the method is not found it throws a descriptive ReflectError. */ protected static Method resolveExpectedJavaMethod(BshClassManager bcm, Class clas, Object object, String name, Object[] args, boolean staticOnly) throws ReflectError, UtilEvalError { if (object == Primitive.NULL) { //noinspection ThrowableInstanceNeverThrown throw new UtilTargetError(new NullPointerException("Attempt to invoke method " + name + " on null value")); } Class[] types = Types.getTypes(args); Method method = resolveJavaMethod(bcm, clas, name, types, staticOnly); if (method == null) { throw new ReflectError((staticOnly ? "Static method " : "Method ") + StringUtil.methodString(name, types) + " not found in class'" + clas.getName() + "'"); } return method; }
public static Object invokeSuperclassMethodImpl(BshClassManager bcm, Object instance, String methodName, Object[] args) throws UtilEvalError, ReflectError, InvocationTargetException { String superName = ClassGeneratorUtil.BSHSUPER + methodName; // look for the specially named super delegate method Class clas = instance.getClass(); Method superMethod = Reflect.resolveJavaMethod(bcm, clas, superName, Types.getTypes(args), false/*onlyStatic*/); if (superMethod != null) return Reflect.invokeMethod(superMethod, instance, args); // No super method, try to invoke regular method // could be a superfluous "super." which is legal. Class superClass = clas.getSuperclass(); superMethod = Reflect.resolveExpectedJavaMethod(bcm, superClass, instance, methodName, args, false/*onlyStatic*/); return Reflect.invokeMethod(superMethod, instance, args); }
public static Object invokeSuperclassMethodImpl(BshClassManager bcm, Object instance, String methodName, Object[] args) throws UtilEvalError, ReflectError, InvocationTargetException { String superName = BSHSUPER + methodName; // look for the specially named super delegate method Class<?> clas = instance.getClass(); Invocable superMethod = Reflect.resolveJavaMethod(clas, superName, Types.getTypes(args), false/*onlyStatic*/); if (superMethod != null) return superMethod.invoke(instance, args); // No super method, try to invoke regular method // could be a superfluous "super." which is legal. Class<?> superClass = clas.getSuperclass(); superMethod = Reflect.resolveExpectedJavaMethod(bcm, superClass, instance, methodName, args, false/*onlyStatic*/); return superMethod.invoke(instance, args); }
static Object constructObject( Class<?> clas, Object object, Object[] args ) throws ReflectError, InvocationTargetException { if ( null == clas ) return Primitive.NULL; if ( clas.isInterface() ) throw new ReflectError( "Can't create instance of an interface: "+clas); Class<?>[] types = Types.getTypes(args); if (clas.isMemberClass() && !isStatic(clas) && null != object) types = Stream.concat(Stream.of(object.getClass()), Stream.of(types)).toArray(Class[]::new); Interpreter.debug("Looking for most specific constructor: ", clas); Invocable con = BshClassManager.memberCache.get(clas) .findMethod(clas.getName(), types); if ( con == null || (args.length != con.getParameterCount() && !con.isVarArgs() && !con.isInnerClass())) throw cantFindConstructor( clas, types ); try { return con.invoke( object, args ); } catch(InvocationTargetException e) { if (e.getCause().getCause() instanceof IllegalAccessException) throw new ReflectError( "We don't have permission to create an instance. " + e.getCause().getCause().getMessage() + " Use setAccessibility(true) to enable access.", e.getCause().getCause()); throw e; } }
BshMethod constructor = instanceNameSpace.getMethod(Types.getBaseName(className), Types.getTypes(args), true/*declaredOnly*/);
/** This method wraps resolveJavaMethod() and expects a non-null method result. If the method is not found it throws a descriptive ReflectError. */ protected static Invocable resolveExpectedJavaMethod( BshClassManager bcm, Class<?> clas, Object object, String name, Object[] args, boolean staticOnly ) throws ReflectError, UtilEvalError { if ( object == Primitive.NULL ) throw new UtilTargetError( new NullPointerException( "Attempt to invoke method " +name+" on null value" ) ); Class<?>[] types = Types.getTypes(args); Invocable method = resolveJavaMethod( clas, name, types, staticOnly ); if ( null != bcm && bcm.getStrictJava() && method != null && method.getDeclaringClass().isInterface() && method.getDeclaringClass() != clas && Modifier.isStatic(method.getModifiers())) // static interface methods are class only method = null; if ( method == null ) throw new ReflectError( ( staticOnly ? "Static method " : "Method " ) + StringUtil.methodString(name, types) + " not found in class'" + clas.getName() + "'"); return method; }
Class[] types = Types.getTypes(args);
/** This is the invocation handler for the dynamic proxy. <p> Notes: Inner class for the invocation handler seems to shield this unavailable interface from JDK1.2 VM... I don't understand this. JThis works just fine even if those classes aren't there (doesn't it?) This class shouldn't be loaded if an XThis isn't instantiated in NameSpace.java, should it? */ class Handler implements InvocationHandler, java.io.Serializable { public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable { try { return invokeImpl( proxy, method, args ); } catch ( TargetError te ) { // Unwrap target exception. If the interface declares that // it throws the ex it will be delivered. If not it will be // wrapped in an UndeclaredThrowable // This isn't simple because unwrapping this loses all context info. // So rewrap is better than unwrap. - fschmidt Throwable t = te.getTarget(); Class<? extends Throwable> c = t.getClass(); String msg = t.getMessage(); try {
Class [] types = Types.getTypes( args ); BshMethod bshMethod = null; try {
Class<?>[] argTypes = Types.getTypes(args); args = Primitive.unwrap(args);
NameSpace thisNamespace = Reflect.getThisNS(obj); if ( null != thisNamespace ) { BshMethod m = thisNamespace.getMethod(methodName, Types.getTypes(args), true); if ( null != m ) return m.invoke(args, interpreter, callstack, callerInfo);
Class[] sig = Types.getTypes(args); CallStack callstack = new CallStack(); Interpreter interpreter;
Class[] argTypes = Types.getTypes(args); args = Primitive.unwrap(args); Class superClass = interpreter.getClassManager().classForName(superClassName);
Class [] argTypes = Types.getTypes( args );
Class [] argTypes = Types.getTypes( args );