Skip to content

Commit

Permalink
Merge pull request playframework#453 from hongrich/lighthouse-1423-patch
Browse files Browse the repository at this point in the history
[playframework#1423] Fix initializing an inner class inside groovy templates
  • Loading branch information
pepite committed Feb 21, 2012
2 parents 75e2883 + 95cc9cd commit da9bb15
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions framework/src/play/templates/GroovyTemplateCompiler.java
Expand Up @@ -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");
}
}

Expand All @@ -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("$", "\\$") + "')");

}
}
Expand Down
@@ -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.