是否有可能在執行的JavaScript代碼中攔截Java對象的字段(和方法)訪問?就像JSCocoa中允許處理屬性和方法訪問的委託一樣。攔截犀牛中java對象的字段訪問
6
A
回答
5
您可以使用Context.setWrapFactory指定如何包裝Java對象。 這裏顯示打印字段訪問和方法調用包裝:
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
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);
}
}
這是一個測試代碼:
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
作爲hog1e3建議的替代方案,也可以實現腳本接口來攔截對類的方法/字段的調用。
相關問題
- 1. 犀牛方法快門/攔截器
- 2. 訪問犀牛的原生JSON.Stringify從Java
- 3. 犀牛對象瀏覽器
- 4. 的Java 8犀牛空白問題
- 5. 犀牛:在Javascript實現中訪問Java接口變量
- 6. 犀牛嘲笑訪問數據庫
- 7. Java的犀牛 - 從javascrict
- 8. 犀牛:用數字
- 9. 攔截Windows對thumbcache.db文件的訪問
- 10. 訪問Java對象中的JavaScript對象的字段
- 11. 犀牛JS的eval比犀牛JS
- 12. 犀牛模擬,從非嘲笑對象
- 13. 犀牛
- 14. 在C++中攔截字段訪問/修改
- 15. Unicode中的犀牛
- 16. 從java訪問scala對象字段
- 17. 在JavaScript中攔截對象屬性訪問
- 18. 犀牛和envjs:與Java應用程序共享的JavaScript對象
- 19. java攔截器不攔截
- 20. 對象未設置爲犀牛的對象的實例嘲笑
- 21. 問題鏈接到犀牛
- 22. 犀牛模擬問題
- 23. THREEjs Raycaster攔截對象問題
- 24. 訪問在Spring攔截
- 25. 攔截JavaScript訪問DOM
- 26. 攔截文件訪問
- 27. Linq訪問對象字段
- 28. Mozilla犀牛字符編碼
- 29. ASM - 在外部進行攔截字段訪問方法
- 30. 攔截私人字段訪問做延遲加載
非常感謝,這正是我所尋找的。我很好奇,你在哪裏找到關於該主題的信息? –