简体   繁体   English

如何重新实例化由 javascript(Nashorn 引擎)实例化的 class

[英]how can I re-instantiate a class instanced by javascript (Nashorn Engine)

so, i instantiate a new class in javascript, and then add it to a list... Later I go through the list and instantiate all the classes again, to work with them.所以,我在 javascript 中实例化了一个新的 class,然后将其添加到列表中……后来我通过列表 go 并再次实例化所有类,以使用它们。

the Javascript: Javascript:

var BPmanager = Java.type('paperVS.tabs.blueprint.BlueprintManager');
var abstractFunction= Java.extend(Java.type('paperVS.logic.function.Function'));

var getYPos = new abstractFunction("getYPos") {

   //Some Functions
}

BPmanager.allFunctions.add(getYPos);

and later in Java:后来在 Java 中:

for (Function fun : allFunctions) { 
     try {
        Class<?> c = Class.forName(fun.getClass().getName());
        Object object = c.newInstance();
        instances.add((Function)object);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Exeption:例外:

java.lang.ClassNotFoundException: jdk.nashorn.javaadapters.paperVS_logic_function_Function
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at paperVS.tabs.blueprint.BlueprintManager.getFromList(BlueprintManager.java:176)

This code works for all classes except the Javascript classes.此代码适用于除 Javascript 类之外的所有类。 The Javascript works fine (loading and executing the single instance), but not instancing of the Class. Javascript 工作正常(加载和执行单个实例),但不能实例化 Class。 The normal ClassLoader does the same普通的 ClassLoader 做同样的事情

Java ClassLoader s use a delegation model, where the system instantiates a "primordial" (or "system") class loader, and then class loaders can be created that look in specialized places for classes, and if not found, look in the ancestor (or delegate) class loader. Java ClassLoader s use a delegation model, where the system instantiates a "primordial" (or "system") class loader, and then class loaders can be created that look in specialized places for classes, and if not found, look in the ancestor (或委托)class 装载机。

Nashorn has its own internal class loaders to deal with script classes; Nashorn 有自己的内部 class 加载器来处理脚本类; see Java 8 ScriptEngine across ClassLoaders .请参阅Java 8 ScriptEngine 跨类加载器 So you are creating classes in those class loaders, and then trying to use them in an ancestor class loader (you can see that in the stack trace) by name.因此,您正在那些class 加载器中创建类,然后尝试按名称在祖先 class 加载器中使用它们(您可以在堆栈跟踪中看到)。

So that's one problem to solve.所以这是一个需要解决的问题。

Another is that these types created by Java.extend are Nashorn-managed objects, bound to a particular Nashorn script context, and will be inconvenient to use from other Java code (see comments below, where the OP notes that the classes can't be used easily, even if they're accessible).另一个是由Java.extend创建的这些类型是 Nashorn 管理的对象,绑定到特定的 Nashorn 脚本上下文,并且不方便从其他 Java 代码中使用(请参阅下面的注释,其中 OP 指出这些类不能易于使用,即使它们可以访问)。

If you want to use the JavaScript-created objects in Java code, you'll want to create Java objects and then pass them back to Java.如果您想在 Java 代码中使用 JavaScript 创建的对象,您需要创建 Java对象,然后将它们传递回 Java。 You seem to be doing this, so perhaps you're just using them incorrectly.你似乎正在这样做,所以也许你只是不正确地使用它们。 Making guesses based on your class names, etc., I am guessing that rather than:根据您的 class 名称等进行猜测,我猜测不是:

Class<?> c = Class.forName(fun.getClass().getName());
Object object = c.newInstance();
instances.add((Function)object);

... you may just want something like: ...您可能只想要类似的东西:

instances.add((Function)fun);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM