The*_*tor 19 java groovy binding template-engine
Groovy模板中的常规操作是将命名对象绑定到模板的范围,如下所示:
map.put("someObject",object)
template.bind(map)
Run Code Online (Sandbox Code Playgroud)
然后,在模板中我可以像这样引用并使用'someObject':
someObject.helloWorld()
someObject.helloWorld("Hi Everyone!")
someObject.helloWorld("Hi", "Everyone!")
Run Code Online (Sandbox Code Playgroud)
在模板内部,Groovy还允许我将方法句柄定义为模板中的第一类变量,如下所示:
someObject = someObject.&helloWorld
Run Code Online (Sandbox Code Playgroud)
然后,我可以在不参考对象和方法名称的情况下执行这些操作:
someObject()
someObject("Hello World")
someObject("Hello", "World")
Run Code Online (Sandbox Code Playgroud)
如何在'template.bind(map)'阶段绑定这样的方法引用以及自动解析脚本提供的所有参数组合,如'.&'运算符?
使用MethodClosure不起作用 - 这是一个简单的测试和我得到的错误
class TestMethodClass {
public void test() {
System.out.println("test()");
}
public void test(Object arg) {
System.out.println("test( " + arg + " )");
}
public void test(Object arg1, Object arg2) {
System.out.println("test( " + arg1 + ", " + arg2 + " )");
}
}
String basic = "<%" +
" def mc1=testInstance.&test;" +
"println \"mc1 class ${mc1.getClass()}\";" +
"println \"mc1 metaclass ${mc1.getMetaClass()}\";" +
"println mc1.getClass();" +
"mc1();" +
"mc1('var1');" +
"mc1('var1', 'var2');" +
"testMethod();" +
" %>";
Map<Object, Object> bindings = new HashMap<>();
bindings.put("testInstance", new TestMethodClass());
bindings.put("testMethod", new MethodClosure(new TestMethodClass(), "test"));
TemplateEngine engine = new GStringTemplateEngine();
Template t = engine.createTemplate(basic);
String result = t.make(bindings).toString();
Run Code Online (Sandbox Code Playgroud)
错误
mc1 class class org.codehaus.groovy.runtime.MethodClosure
mc1 metaclass org.codehaus.groovy.runtime.HandleMetaClass@6069db50[groovy.lang.MetaClassImpl@6069db50[class org.codehaus.groovy.runtime.MethodClosure]]
class org.codehaus.groovy.runtime.MethodClosure
test()
test( var1 )
test( var1, var2 )
groovy.lang.MissingMethodException: No signature of method: groovy.lang.Binding.testMethod() is applicable for argument types: () values: []
Run Code Online (Sandbox Code Playgroud)
用户建议我只使用'.call(..)'
"testMethod.call();" +
"testMethod.call(1);" +
"testMethod.call(1,2);" +
Run Code Online (Sandbox Code Playgroud)
但这违背了目的.在这种情况下,我不妨只是绑定对象而不是'testMethod',并在常规方法调用的Groovy模板中正常使用它.所以这不是解决方案.
该解决方案将创建一个绑定,以便可以像这样调用testMethod(),并为所有重载方法解析,就像"mc1 = testInstance.&test"提供的那样.
mc1是一个MethodClosure,'mc1 = testInstance.&test'做了一些魔法,我想在绑定阶段做那个魔法!
mc1的元类是'HandleMetaClass'.我也可以从Java端自定义方法框的元类.我只是想知道如何做到这一点来获得相同的行为.Groovy正在模板中执行此操作(从模板解释器中的Java端),因此我想提前做同样的事情.
请注意,通常,流式传输模板已经创建了自己的委托.当模板代码'def mc1 = testInstance.&test;'时 在解释时,Groovy编译器/解释器在使用HandleMetaClass创建MethodClosure时使用该委托,然后将其安装在现有委托中.
然后,正确的答案不会按照下面的@Dany回答安装替换委托,而是使用现有委托并创建正确的对象以便于使用没有'.call'语法的mc1.
这将有效:
"<% " +
"def mc1=testInstance.&test;" +
"mc1();" +
"mc1('var1');" +
"mc1('var1', 'var2');" +
"testMethod.call();" +
"testMethod.call(1);" +
"testMethod.call(1,2);" +
" %>"
Run Code Online (Sandbox Code Playgroud)
testMethod.call(...)有效,因为它被翻译成getProperty('testMethod').invokeMethod('call', ...).该testMethod属性在绑定中定义,并且是MethodClosure具有已call定义方法的类型.
但是testMethod(...)被翻译成invokeMethod('testMethod', ...).它失败了,因为没有testMethod定义方法,你不能通过绑定来定义它.
编辑
Main.java:
public class Main {
public static void main(String[] args) throws Throwable {
String basic = "<%" +
" def mc1=testInstance.&test;" +
"mc1();" +
"mc1('var1');" +
"mc1('var1', 'var2');" +
"testMethod();" +
"testMethod(1);" +
"testMethod(2,3);" +
" %>";
Map<Object, Object> bindings = new HashMap<>();
bindings.put("testInstance", new TestMethodClass());
bindings.put("testMethod", new MethodClosure(new TestMethodClass(), "test"));
TemplateEngine engine = new GStringTemplateEngine();
Template t = engine.createTemplate(basic);
Closure make = (Closure) t.make();
make.setDelegate(new MyDelegate(bindings));
String result = make.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
MyDelegate.groovy:
class MyDelegate extends Binding {
MyDelegate(Map binding) {
super(binding)
}
def invokeMethod(String name, Object args) {
getProperty(name).call(args)
}
}
Run Code Online (Sandbox Code Playgroud)
或者MyDelegate.java:
public class MyDelegate extends Binding{
public MyDelegate(Map binding) {
super(binding);
}
public Object invokeMethod(String name, Object args) {
return DefaultGroovyMethods.invokeMethod(getProperty(name), "call", args);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过将ResolveStrategy更改为OWNER_FIRST来实现所需的行为。由于您想直接访问有界闭包(没有 . 符号),因此您需要将闭包方法绑定到“所有者”(模板对象本身),而不是通过绑定映射(委托)提供。
您修改后的示例:
String basic = "<%" +
" def mc1=testInstance.&test;" +
"println \"mc1 class ${mc1.getClass()}\";" +
"println \"mc1 metaclass ${mc1.getMetaClass()}\";" +
"println mc1.getClass();" +
"mc1();" +
"mc1('var1');" +
"mc1('var1', 'var2');" +
"testMethod();" +
"testMethod('var1');" +
" %>";
TemplateEngine engine = new GStringTemplateEngine();
TestMethodClass instance = new TestMethodClass();
// Prepare binding map
Map<String, Object> bindings = new HashMap<>();
bindings.put("testInstance", instance);
Template t = engine.createTemplate(basic);
Closure<?> make = (Closure<?>) t.make(bindings); // cast as closure
int resolveStrategy = make.getResolveStrategy();
make.setResolveStrategy(Closure.OWNER_FIRST);
// set method closure which you want to invoke directly (without .
// notation). This is more or less same what you pass via binding map
// but too verbose. You can encapsulate this inside a nice static
// method
InvokerHelper.setProperty(
make.getOwner(), "testMethod", new MethodClosure(instance, "test")
);
make.setResolveStrategy(resolveStrategy);
String result = make.toString();
Run Code Online (Sandbox Code Playgroud)
希望这能满足您的要求。
| 归档时间: |
|
| 查看次数: |
810 次 |
| 最近记录: |