Ich habe eine Basisklasse, die Entity
als Konstruktorargument verwendet. Ich erweitere diese Basisklasse in JavaScript, und es sieht wie folgt aus:Java Nashorn JS-Objekt in Java erstellen, wenn ich ein Objekt habe, das JS-Funktion darstellt
Java.extend(BaseClass, {
someAbstractMethod : function() { ... },
someAdditionalField : ...,
etc
})
Soweit ich verstanden, ich nicht die zusätzlichen Felder/Methoden verwenden können, wenn ich Instanz davon als Java-Objekt haben, aber ich kann, wenn Es ist ein JavaScript-Objekt. Wie instanziiere ich diese Klasse als JS-Objekt mit Java-Code?
public class ScriptedEntity extends Entity implements Scripted<EntityScriptFunctions> {
private CompiledScript script;
private Object implemented_script_class;
private Object my_script_instance;
private Invocable invocable;
public ScriptedEntity(float x, float y, CompiledScript script) {
super(x, y);
invocable = (Invocable) script.getEngine();
try {
implemented_script_class = script.eval();
my_script_instance = invocable.invokeFunction("???", this); //'this' is the constructor argument
} catch (ScriptException | IllegalArgumentException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
...
}