private static FunctionObject getFunctionObject( Class aClass, String methodName, Scriptable scriptable ) { Hashtable functionMap = (Hashtable) _classFunctionMaps.get( aClass ); if (functionMap == null) { _classFunctionMaps.put( aClass, functionMap = new Hashtable() ); } Object result = functionMap.get( methodName ); if (result == NO_SUCH_PROPERTY) return null; if (result != null) return (FunctionObject) result; Method[] methods = aClass.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equalsIgnoreCase( methodName )) { FunctionObject function = new FunctionObject( methodName, method, scriptable ); functionMap.put( methodName, function ); return function; } } functionMap.put( methodName, NO_SUCH_PROPERTY ); return null; }
/** * Convenience method to locate a function name for this object and * create an appriate Function instance to represent it. It assumes that * the name you give it is the normal name and will add a "jsFunction_" * prefix to locate that from the method details. There is also the * implicit assumption that you have made a check for this name being a * valid function for this object before you call this method. If a * function object is found for this method, it will automatically be * registered and you can also have a copy of it returned to use. * * @param name The real method name to look for * @return The function object corresponding to the munged method name */ protected FunctionObject locateFunction(String name) { String real_name = JS_FUNCTION_PREFIX + name; Method[] methods = FunctionObject.findMethods(getClass(), real_name); if (methods == null) { errorReporter.warningReport("Unknown function: " + real_name + " on: " + getClass(), null); return null; } FunctionObject function = new FunctionObject(name, methods[0], this); functionObjects.put(name, function); return function; }
public static Object toJavaValue(Object object) { if (object == null || object.equals(Context.getUndefinedValue())) { return null; } else if (object.getClass().getPackage().getName().startsWith("java.")) { return object; } else if (object instanceof FunctionObject) { throw new IllegalArgumentException(String.format("Cannot convert function object to value (object: %s)", object)); } else if (object instanceof Scriptable) { return toMap((Scriptable) object); } else { throw new IllegalArgumentException(String.format("Can't convert JS object %s (type: %s) to native Java object", object, object.getClass().getName())); } }
/** * Exports a Java method as a Javascript (top-level-)function. Only works * with methods in this class (because scope is used as value for 'this' * when invoking methods). * * @param methodName * @param parameterSignature */ private void exportMethod(String methodName, Class[] parameterSignature) { try { Method m = JavascriptScope.class.getMethod(methodName, parameterSignature); while (methodName.charAt(0) == '_') { methodName = methodName.substring(1); } // remove leading _ for JS name FunctionObject functionJS = new FunctionObject(methodName, m, this); ScriptableObject.putProperty(this, methodName, functionJS); } catch (NoSuchMethodException nse) { nse.printStackTrace(); } }
public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) { int flags = DONTENUM | READONLY | PERMANENT; /* for horizontalSize, verticalSize */ constructor.defineProperty("wrap_view", WRAP_VIEW, flags); constructor.defineProperty("fill_container", FILL_CONTAINER, flags); /* for verticalAlignment, horizontalAlignment */ constructor.defineProperty("left", LEFT, flags); constructor.defineProperty("center", CENTER, flags); constructor.defineProperty("right", RIGHT, flags); constructor.defineProperty("top", TOP, flags); constructor.defineProperty("bottom", BOTTOM, flags); }
public List<FunctionObject> getFunctions(Scriptable scope) { List<String> names = this.getMethodNames(); List<FunctionObject> functions = new ArrayList<FunctionObject>(); Method[] methods = this.getClass().getMethods(); for (Method method : methods) if (names.contains(method.getName())) functions.add(new FunctionObject(method.getName(), method, scope)); return functions; }
public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) { System.out.println("finishInit is called."); globalPrototype = prototype; }
private void addFunctionExtension(Scriptable scope, FunctionExtension extension) { List<FunctionObject> functions = extension.getFunctions(scope); if (functions != null) for (FunctionObject func : functions) ScriptableObject.putProperty(scope, func.getFunctionName(), func); }
/** * Convenience method to locate a function name for this object and * create an appriate Function instance to represent it. It assumes that * the name you give it is the normal name and will add a "jsFunction_" * prefix to locate that from the method details. There is also the * implicit assumption that you have made a check for this name being a * valid function for this object before you call this method. If a * function object is found for this method, it will automatically be * registered and you can also have a copy of it returned to use. * * @param name The real method name to look for * @return The function object corresponding to the munged method name */ private FunctionObject locateFunction(String name) { String real_name = JS_FUNCTION_PREFIX + name; Method[] methods = FunctionObject.findMethods(getClass(), real_name); FunctionObject function = new FunctionObject(name, methods[0], this); functionObjects.put(name, function); return function; }
/** * Convenience method to locate a function name for this object and * create an appriate Function instance to represent it. It assumes that * the name you give it is the normal name and will add a "jsFunction_" * prefix to locate that from the method details. There is also the * implicit assumption that you have made a check for this name being a * valid function for this object before you call this method. If a * function object is found for this method, it will automatically be * registered and you can also have a copy of it returned to use. * * @param name The real method name to look for * @return The function object corresponding to the munged method name */ protected FunctionObject locateFunction(String name) { String real_name = JS_FUNCTION_PREFIX + name; Method[] methods = FunctionObject.findMethods(getClass(), real_name); FunctionObject function = new FunctionObject(name, methods[0], this); registerFunction(name, function); return function; }
/** * Convenience method to locate a function name for this object and * create an appriate Function instance to represent it. It assumes that * the name you give it is the normal name and will add a "jsFunction_" * prefix to locate that from the method details. There is also the * implicit assumption that you have made a check for this name being a * valid function for this object before you call this method. If a * function object is found for this method, it will automatically be * registered and you can also have a copy of it returned to use. * * @param name The real method name to look for * @return The function object corresponding to the munged method name */ protected FunctionObject locateFunction(String name) { String real_name = JS_FUNCTION_PREFIX + name; Method[] methods = FunctionObject.findMethods(getClass(), real_name); FunctionObject function = new FunctionObject(name, methods[0], this); functionObjects.put(name, function); return function; }