public static void invokeMain( Class<?> clas, String [] args ) throws Exception { Invocable main = Reflect.resolveJavaMethod( clas, "main", new Class [] { String [].class }, true/*onlyStatic*/); if ( main != null ) main.invoke( null, new Object [] { 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; } }
+ " for type: " + StringUtil.typeString(cls)); try { return getter.invoke(obj); } catch(InvocationTargetException e) { throw new ReflectError("Property accessor threw exception: "
/** Invoke a method known to be static. No object instance is needed and there is no possibility of the method being a bsh scripted method. */ public static Object invokeStaticMethod( BshClassManager bcm, Class<?> clas, String methodName, Object [] args, SimpleNode callerInfo ) throws ReflectError, UtilEvalError, InvocationTargetException { Interpreter.debug("invoke static Method"); NameSpace ns = getThisNS(clas); if (null != ns) ns.setNode(callerInfo); Invocable method = resolveExpectedJavaMethod( bcm, clas, null, methodName, args, true ); return method.invoke(null, args); }
@SuppressWarnings({"rawtypes", "unchecked"}) public static Object setObjectProperty( Object obj, Object propName, Object value) { if ( Types.isPropertyTypeMap(obj) ) return ((Map) obj).put(propName, Primitive.unwrap(value)); if ( Types.isPropertyTypeEntry(obj) ) { Entry entre = (Entry) obj; if ( propName.equals(entre.getKey()) ) return entre.setValue(Primitive.unwrap(value)); throw new ReflectError("No such property setter: " + propName + " for type: " + StringUtil.typeString(obj)); } Class<?> cls = obj.getClass(); if ( Types.isPropertyTypeEntryList(cls) ) return getEntryForKey(propName, (Entry[]) obj) .setValue(Primitive.unwrap(value)); if ( obj instanceof Class ) cls = (Class) obj; Invocable setter = BshClassManager.memberCache.get(cls) .findSetter(propName.toString()); if ( null == setter ) throw new ReflectError("No such property setter: " + propName + " for type: " + StringUtil.typeString(cls)); try { return setter.invoke(obj, new Object[] { Primitive.unwrap(value) }); } catch(InvocationTargetException e) { throw new ReflectError("Property accessor threw exception: " + e.getCause(), e.getCause()); } }
return Objects.requireNonNull(field).invoke(object); } catch( ReflectiveOperationException e2 ) { throw new UtilEvalError("Can't read field: " + field, e2);
if (null != ns) ns.setNode(callerInfo); return method.invoke(object, args); } catch ( UtilEvalError e ) { throw e.toEvalError( callerInfo, callstack );
try { Invocable f = resolveExpectedJavaField(clas, fieldName, staticOnly); return f.invoke(object); } catch ( ReflectError e ) { NameSpace ns = getThisNS(clas);
nameSpace.setVariableOrProperty( varName, val, strictJava ); } else if ( type == FIELD ) try { Objects.requireNonNull(field).invoke( object, val ); return val;
return javaMethod.invoke(javaObject, argValues); } catch ( ReflectError e ) { throw new EvalError(