1 21 package org.lobobrowser.js; 22 23 import org.mozilla.javascript.*; 24 25 public class JavaConstructorObject extends ScriptableObject implements Function { 26 private final JavaClassWrapper classWrapper; 27 private final JavaInstantiator instantiator; 28 private final String name; 29 30 public JavaConstructorObject(String name, JavaClassWrapper classWrapper) { 31 this.name = name; 32 this.classWrapper = classWrapper; 33 this.instantiator = new SimpleInstantiator(classWrapper); 34 } 35 36 public JavaConstructorObject(String name, JavaClassWrapper classWrapper, JavaInstantiator instantiator) { 37 this.name = name; 38 this.classWrapper = classWrapper; 39 this.instantiator = instantiator; 40 } 41 42 public String getClassName() { 43 return this.name; 44 } 45 46 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object [] args) { 47 throw new UnsupportedOperationException (); 48 } 49 50 public Scriptable construct(Context cx, Scriptable scope, Object [] args) { 51 try { 52 Object javaObject = this.instantiator.newInstance(); 53 Scriptable newObject = new JavaObjectWrapper(this.classWrapper, javaObject); 54 newObject.setParentScope(scope); 55 return newObject; 56 } catch(Exception err) { 57 throw new IllegalStateException (err.getMessage()); 58 } 59 } 60 61 public java.lang.Object getDefaultValue(java.lang.Class hint) { 62 if(String .class.equals(hint)) { 63 return "function " + this.name; 64 } 65 else { 66 return super.getDefaultValue(hint); 67 } 68 } 69 70 public static class SimpleInstantiator implements JavaInstantiator { 71 private final JavaClassWrapper classWrapper; 72 73 public SimpleInstantiator(final JavaClassWrapper classWrapper) { 74 super(); 75 this.classWrapper = classWrapper; 76 } 77 78 public Object newInstance() throws InstantiationException , IllegalAccessException { 79 return this.classWrapper.newInstance(); 80 } 81 } 82 } 83 | Popular Tags |