我了解 groovy 基础知识 - 和闭包......
我正在尝试从 java 调用 groovy:
// 病人.java
public class Patient {
//... other data
private Map<String, String> attribStore = new HashMap<String,String>();
// getters/setters for attribStore omitted
public void addAttribute(String key, String val) {
if (!attribStore.containsKey(key)) {
attribStore.put(key, val);
}
}
// GroovyHelper.java
public class GroovyHelper {
private String codeSnippet; // groovy script code
public String evaluateSnippetToString(Binding binding) {
addMethodMissingHandler();
GroovyShell shell = createGroovyShell(binding);
Object result = shell.evaluate(codeSnippet);
return result.toString();
}
// installs a patient in the binding - accesses the patient
// attribStore from groovy
// The missing method is used to create an "attribute" i.e.
// a (key,val) pair in the patient map
private void addMethodMissingHandler() {
codeSnippet = "def attribStore = p.getAttribStore();\n"
+ "Patient.metaClass.methodMissing = \n{"
+ " String methodName, args -> \n"
+ "methodName = methodName.replaceFirst(/^get/, '');\n"
+ "def attrib = methodName[0].toLowerCase() + methodName.substring(1);\n"
+ "if (!attribStore.containsKey(attrib)) { attribStore[attrib] = '0'; }\n"
+ "return attribStore[attrib]; \n" + "}\n" + codeSnippet;
}
}
//junit 测试代码
private Patient p;
private Binding binding;
private GroovyHelper gh;
@Before
public void init() {
p = new PatientBuilder().build();
binding = new Binding();
binding.setVariable("p", p);
gh = new GroovyHelper();
}
@Test //passes
public void testPopulatePatientAttribStore() throws Exception {
p.addAttribute("xyz", "4");
gh.setCodeSnippet("p.getXyz()");
gh.evaluateSnippetToString(binding);
}
@Test
public void testGroovy() throws Exception {
Binding binding = new Binding();
binding.setVariable("p", new Patient()); // new patient
p.addAttribute("xyz", "9");
GroovyShell gs1 = new GroovyShell(binding);
assertEquals("9", gs1.evaluate("p.getXyz()")); // fails??? - expected: <[9]> but was: <[4]>
}
我的问题是 - 闭包是否保留了早期绑定的属性存储?
这里到底发生了什么?
为冗长的代码道歉 - 我正在研究它
- 减少不相关的代码
以将其缩小到最低限度 - 任何指针,“更多阅读要做”提示?