Ist es möglich, den Feld- (und Methoden-) Zugriff von Java-Objekten im ausgeführten JavaScript-Code abzufangen? Genauso wie der Delegate in JSCocoa, der den Zugriff auf Eigenschaften und Methoden ermöglicht.Intercept-Feldzugriff von Java-Objekten in Rhino
6
A
Antwort
5
Sie können Context.setWrapFactory verwenden, um anzugeben, wie Java-Objekte umgebrochen werden. zeigt hier Wrapper, die Feldzugriff und Methodenaufruf druckt:
InterceptWrapFactory.java
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;
public class InterceptWrapFactory extends WrapFactory{
@Override
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
Object javaObject, Class<?> staticType) {
return new InterceptNativeObject(scope, javaObject, staticType);
}
}
InterceptNativeObject.java
import org.mozilla.javascript.NativeJavaMethod;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
public class InterceptNativeObject extends NativeJavaObject {
@Override
public Object get(String name, Scriptable start) {
Object res = super.get(name, start);
System.out.println("Field get name="+name+" result="+res);
if (res instanceof NativeJavaMethod) {
NativeJavaMethod method = (NativeJavaMethod) res;
return new JavaMethodWrapper(method);
}
return res;
}
public InterceptNativeObject(Scriptable scope, Object javaObject,
Class<?> staticType) {
super(scope, javaObject, staticType);
}
}
JavaMethodWrapper.java
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJavaMethod;
import org.mozilla.javascript.Scriptable;
public class JavaMethodWrapper implements Function {
NativeJavaMethod method;
public JavaMethodWrapper(NativeJavaMethod method) {
this.method = method;
}
public boolean hasInstance(Scriptable instance) {
return method.hasInstance(instance);
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
Object[] args) {
System.out.println("Call method: "+method);
return method.call(cx, scope, thisObj, args);
}
public boolean has(int index, Scriptable start) {
return method.has(index, start);
}
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
return method.construct(cx, scope, args);
}
public void put(int index, Scriptable start, Object value) {
method.put(index, start, value);
}
public void delete(int index) {
method.delete(index);
}
public Scriptable createObject(Context cx, Scriptable scope) {
return method.createObject(cx, scope);
}
public boolean has(String name, Scriptable start) {
return method.has(name, start);
}
public void defineConst(String name, Scriptable start) {
method.defineConst(name, start);
}
public void put(String name, Scriptable start, Object value) {
method.put(name, start, value);
}
public void delete(String name) {
method.delete(name);
}
public Scriptable getPrototype() {
return method.getPrototype();
}
public void setPrototype(Scriptable m) {
method.setPrototype(m);
}
public Scriptable getParentScope() {
return method.getParentScope();
}
public void setParentScope(Scriptable m) {
method.setParentScope(m);
}
public Object[] getIds() {
return method.getIds();
}
public Object get(int index, Scriptable start) {
return method.get(index, start);
}
public Object get(String name, Scriptable start) {
return method.get(name, start);
}
public String getClassName() {
return method.getClassName();
}
public Object getDefaultValue(Class<?> typeHint) {
return method.getDefaultValue(typeHint);
}
}
Und dies ist ein Testcode:
import java.util.Vector;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
public class InterceptTest {
public static void main(String[] args) {
Context cx=Context.enter();
cx.setWrapFactory(new InterceptWrapFactory());
Scriptable root=cx.initStandardObjects();
ScriptableObject.putProperty(root,"v", new Vector<String>());
cx.evaluateString(root, "v.add('foo'); v.get(0)", "src", 1, null);
}
}
0
Als Alternative zu dem, was hog1e3 vorgeschlagen hat, ist es auch möglich, die Scriptable-Schnittstelle zu implementieren, um Aufrufe von Methoden/Feldern einer Klasse abzufangen.
Verwandte Themen
- 1. XMLHttpRequest in Rhino?
- 2. Rhino Commons und Rhino Mocks Referenzdokumente?
- 3. Unicode in Rhino
- 4. AssertWasCalled in Rhino Mocks
- 5. Wechsel von Rhino nach Nashorn
- 6. Mischen von Stubs und Mocks in Rhino
- 7. Rhino Einbettung
- 8. Teilargument Übereinstimmung in Rhino Mocks
- 9. Split Java-Strings in Rhino
- 10. Zurückgeben eines Hostobjekts in Rhino
- 11. konfigurieren Rhino in Javascript Eclipse
- 12. Konsoleneingabefunktion für Rhino?
- 13. Rhino Mocks mit Singleton
- 14. Vorteile der Verwendung von Rhino (Mozilla Nashorn)
- 15. Rhino: Wie JS Funktion von Java
- 16. Rhino Mocks Überschreiben von Stubs möglich?
- 17. Verfügbarmachen einer Funktion von Java zu Rhino
- 18. Rhino Mocks Partial Mock
- 19. Rhino Mocks: Mocking HttpRequestBase.Files
- 20. Haben Rhino-Skript vorladen?
- 21. JVM fehlt Rhino
- 22. Rhino Mocks, VerifyAllExpections
- 23. Rhino Mocks & Compact Framework
- 24. Wro4j - Rhino Implementierung
- 25. Rhino Mocks Record Wiedergabesyntax
- 26. Wer nutzt Rhino Security?
- 27. Wie gebe ich etwas in Rhino aus?
- 28. NMocks2 Argument Syntax in Rhino Mocks?
- 29. Mozilla Rhino: Standard-JavaScript-Konformitätsstufe
- 30. Rhino load() - Funktion in JavaScript zur Verfügung gestellt von javax.script?
Vielen Dank, das ist genau das, was ich gesucht habe. Ich bin neugierig, wo hast du Informationen zu dem Thema gefunden? –