Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有前缀的情况下调用Java方法并从嵌入式groovy访问属性_Java_Groovy - Fatal编程技术网

如何在没有前缀的情况下调用Java方法并从嵌入式groovy访问属性

如何在没有前缀的情况下调用Java方法并从嵌入式groovy访问属性,java,groovy,Java,Groovy,我正在使用GroovyScriptEngine将Groovy嵌入Java应用程序中。我把所有相关的属性都放进了绑定中,所有的工作都很好。为了完整起见,这里有一个片段: public class GE2 { GroovyScriptEngine gse; Binding binding; public GE2() throws Exception { this.gse = new GroovyScriptEngine(new String[]{"scrip

我正在使用GroovyScriptEngine将Groovy嵌入Java应用程序中。我把所有相关的属性都放进了绑定中,所有的工作都很好。为了完整起见,这里有一个片段:

public class GE2 {
    GroovyScriptEngine gse;
    Binding binding;

    public GE2() throws Exception {
        this.gse = new GroovyScriptEngine(new String[]{"scripts"});
        binding = new Binding() {

            @Override
            public Object getProperty(String property) {
                // this method is never called when trying println name2 from groovy
                return "Prop: " + property;
            }

        };
        binding.setVariable("GE2", this);
        gse.run("t1.groovy", binding);
    }

    public String getName() {
        return "theName";
    }

    public void doIt(String... args) {
        System.out.printf("Doing it with %s\n", Arrays.toString(args));
    }

    public static void main(String[] args) throws Exception {
        new GE2();
    }
}
我的groovy脚本t1.groovy如下所示:

println GE2.name // this correctly prints theName
// println name2 <- this raises No such property: name2 for class: t1
GE2.doIt('a', 1, 42); // this works as expected too
println GE2.name//这将正确打印名称

//println name2您可以自己实现所有成员并链接它们

public void doIt(String... args) {
    GE2.doIt(args);
}
因此,可以从您自己的类调用它们。

允许您设置一个
scriptBaseClass
,从中调用内容。你能用吗?
GroovyScriptEngine
CompilerConfiguration
似乎存在以下问题(尽管它们可能已解决/可变通):

文件
Shell.groovy

def script = '''
  println GE3.name // this now prints the GE3's class name
  println name 
  doIt 'a', '1', '42'
'''


def config = new org.codehaus.groovy.control.CompilerConfiguration(scriptBaseClass: GE3.class.name)
def binding = new Binding()


new GroovyShell(binding, config).evaluate script
abstract class GE3 extends Script {
  String getName() { "John Doe" }

  void doIt(String... args) {
    System.out.printf("Doing it with %s\n", Arrays.toString(args));
  }
}
文件
GE3.groovy

def script = '''
  println GE3.name // this now prints the GE3's class name
  println name 
  doIt 'a', '1', '42'
'''


def config = new org.codehaus.groovy.control.CompilerConfiguration(scriptBaseClass: GE3.class.name)
def binding = new Binding()


new GroovyShell(binding, config).evaluate script
abstract class GE3 extends Script {
  String getName() { "John Doe" }

  void doIt(String... args) {
    System.out.printf("Doing it with %s\n", Arrays.toString(args));
  }
}

但接下来我必须在groovy中再次执行所有这些方法和属性。我希望我的groovy不要有任何像这样的额外样板文件。如果你想绕过静态GE2调用,你必须在它之间加上一些东西。在Java中没有其他方法可以做到这一点。省略
GE2.
部分的唯一方法是使可调用方法成为您正在处理的类的方法。