Main method that demonstrates creating and using custom JavaScript classes from Java objects. This method shows advanced JavaScript-Java integration by defining a custom JavaScript class (Counter) based on a Java class, creating an instance with initial parameters, and making it available to Jav
(String args[])
| 68 | * reflection errors during class definition. |
| 69 | */ |
| 70 | public static void main(String args[]) throws Exception { |
| 71 | Context cx = Context.enter(); |
| 72 | try { |
| 73 | TopLevel scope = cx.initStandardObjects(); |
| 74 | |
| 75 | // Use the Counter class to define a Counter constructor |
| 76 | // and prototype in JavaScript. |
| 77 | ScriptableObject.defineClass(scope, Counter.class); |
| 78 | |
| 79 | // Create an instance of Counter and assign it to |
| 80 | // the top-level variable "myCounter". This is |
| 81 | // equivalent to the JavaScript code |
| 82 | // myCounter = new Counter(7); |
| 83 | Object[] arg = {Integer.valueOf(7)}; |
| 84 | Scriptable myCounter = cx.newObject(scope, "Counter", arg); |
| 85 | scope.put("myCounter", scope, myCounter); |
| 86 | |
| 87 | String s = ""; |
| 88 | for (int i = 0; i < args.length; i++) { |
| 89 | s += args[i]; |
| 90 | } |
| 91 | Object result = cx.evaluateString(scope, s, "<cmd>", 1, null); |
| 92 | System.err.println(Context.toString(result)); |
| 93 | } finally { |
| 94 | Context.exit(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** Private constructor to prevent instantiation of this utility class. */ |
| 99 | private RunScript4() { |
nothing calls this directly
no test coverage detected