Skip to content
Browse files

Merge pull request #453 from hongrich/lighthouse-1423-patch

[#1423] Fix initializing an inner class inside groovy templates
  • Loading branch information...
2 parents 75e2883 + 95cc9cd commit da9bb157f8bfe698eee25c22aa0b2afc4185e758 @pepite pepite committed Feb 21, 2012
View
4 framework/src/play/templates/GroovyTemplateCompiler.java
@@ -79,7 +79,7 @@ public int compare(String o1, String o2) {
if (names.size() <= 1 || source.indexOf("new ")>=0) {
for (String cName : names) { // dynamic class binding
- source = source.replaceAll("new " + Pattern.quote(cName) + "(\\([^)]*\\))", "_('" + originalNames.get(cName) + "').newInstance$1");
+ source = source.replaceAll("new " + Pattern.quote(cName) + "(\\([^)]*\\))", "_('" + originalNames.get(cName).replace("$", "\\$") + "').newInstance$1");
}
}
@@ -92,7 +92,7 @@ public int compare(String o1, String o2) {
if (names.size() <= 1 || source.indexOf(".class")>=0) {
for (String cName : names) { // dynamic class binding
- source = source.replaceAll("([^.])" + Pattern.quote(cName) + ".class", "$1_('" + originalNames.get(cName) + "')");
+ source = source.replaceAll("([^.])" + Pattern.quote(cName) + ".class", "$1_('" + originalNames.get(cName).replace("$", "\\$") + "')");
}
}
View
49 samples-and-tests/just-test-cases/test/TemplateClassBindingTest.java
@@ -0,0 +1,49 @@
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import play.templates.GroovyTemplate;
+import play.templates.GroovyTemplateCompiler;
+import play.test.UnitTest;
+
+public class TemplateClassBindingTest extends UnitTest {
+
+ public static class StaticInnerClass {
+ public final int val;
+ public StaticInnerClass() {
+ val = 42;
+ }
+ }
+
+ @Test
+ public void testDynamicClassBindingWithNew() {
+ final String source = "${ new TemplateClassBindingTest.StaticInnerClass().val }";
+ GroovyTemplate groovyTemplate = new GroovyTemplate("dynamic_class_binding_with_new", source);
+ new GroovyTemplateCompiler().compile(groovyTemplate);
+ assertEquals("42", groovyTemplate.render());
+ }
+
+ @Test
+ public void testDynamicClassBindingWithInstanceOf() {
+ StaticInnerClass staticInnerClass = new StaticInnerClass();
+ final String source = "${ staticInnerClass instanceof TemplateClassBindingTest.StaticInnerClass }";
+ GroovyTemplate groovyTemplate = new GroovyTemplate("dynamic_class_binding_with_instanceof", source);
+ new GroovyTemplateCompiler().compile(groovyTemplate);
+ Map<String, Object> args = new HashMap<String,Object>();
+ args.put("staticInnerClass", staticInnerClass);
+ assertEquals("true", groovyTemplate.render(args));
+ }
+
+ @Test
+ public void testDynamicClassBindingWithDotClass() {
+ StaticInnerClass staticInnerClass = new StaticInnerClass();
+ final String source = "${ staticInnerClass.getClass() == TemplateClassBindingTest.StaticInnerClass.class }";
+ GroovyTemplate groovyTemplate = new GroovyTemplate("dynamic_class_binding_with_dot_class", source);
+ new GroovyTemplateCompiler().compile(groovyTemplate);
+ Map<String, Object> args = new HashMap<String,Object>();
+ args.put("staticInnerClass", staticInnerClass);
+ assertEquals("true", groovyTemplate.render(args));
+ }
+
+}

0 comments on commit da9bb15

Please sign in to comment.
Something went wrong with that request. Please try again.