Skip to content

Commit

Permalink
* [playframework#1009] fix(templates): o not log error and exception …
Browse files Browse the repository at this point in the history
…when compiling a temaplate with a non GroovyInlineTag

** revert modif on GroovyTemplateCompiler.java don in 765dec2
xael-fry committed Oct 14, 2016
1 parent f2bb5a4 commit 75471a9
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions framework/src/play/templates/GroovyTemplateCompiler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package play.templates;

import groovy.lang.Closure;
import play.Logger;
import play.Play;
import play.exceptions.TemplateCompilationException;
import play.templates.GroovyInlineTags.CALL;

import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -16,6 +10,12 @@
import java.util.Map;
import java.util.regex.Pattern;

import groovy.lang.Closure;
import play.Logger;
import play.Play;
import play.exceptions.TemplateCompilationException;
import play.templates.GroovyInlineTags.CALL;

public class GroovyTemplateCompiler extends TemplateCompiler {

protected List<String> extensionsClassnames = new ArrayList<>();
@@ -24,12 +24,11 @@ public class GroovyTemplateCompiler extends TemplateCompiler {
// Have to split it if it is really that big
protected static final int maxPlainTextLength = 60000;


@Override
public BaseTemplate compile(BaseTemplate template) {
try {
extensionsClassnames.clear();
extensionsClassnames.addAll( Play.pluginCollection.addTemplateExtensions());
extensionsClassnames.addAll(Play.pluginCollection.addTemplateExtensions());
List<Class> extensionsClasses = Play.classloader.getAssignableClasses(JavaExtensions.class);
for (Class extensionsClass : extensionsClasses) {
extensionsClassnames.add(extensionsClass.getName());
@@ -46,23 +45,23 @@ protected String source() {

// If a plugin has something to change in the template before the compilation
source = Play.pluginCollection.overrideTemplateSource(template, source);
if(Boolean.parseBoolean(Play.configuration.getProperty("groovy.template.check.scala.comptatibility", "false"))){

if (Boolean.parseBoolean(Play.configuration.getProperty("groovy.template.check.scala.comptatibility", "false"))) {
source = this.checkScalaCompatibility(source);
}

return source;
}

@Deprecated
protected String checkScalaComptability(String source){
protected String checkScalaComptability(String source) {
return checkScalaCompatibility(source);
}

/**
* Makes the code scala compatible (for the scala module).
*/
protected String checkScalaCompatibility(String source){
protected String checkScalaCompatibility(String source) {
// Static access
List<String> names = new ArrayList<>();
Map<String, String> originalNames = new HashMap<>();
@@ -97,15 +96,15 @@ public int compare(String o1, String o2) {

if (names.size() <= 1 || source.contains("new ")) {
for (String cName : names) { // dynamic class binding
source = source.replaceAll("new " + Pattern.quote(cName) + "(\\([^)]*\\))", "_('"
+ originalNames.get(cName).replace("$", "\\$") + "').newInstance$1");
source = source.replaceAll("new " + Pattern.quote(cName) + "(\\([^)]*\\))",
"_('" + originalNames.get(cName).replace("$", "\\$") + "').newInstance$1");
}
}

if (names.size() <= 1 || source.contains("instanceof")) {
for (String cName : names) { // dynamic class binding
source = source.replaceAll("([a-zA-Z0-9.-_$]+)\\s+instanceof\\s+" + Pattern.quote(cName), "_('"
+ originalNames.get(cName).replace("$", "\\$") + "').isAssignableFrom($1.class)");
source = source.replaceAll("([a-zA-Z0-9.-_$]+)\\s+instanceof\\s+" + Pattern.quote(cName),
"_('" + originalNames.get(cName).replace("$", "\\$") + "').isAssignableFrom($1.class)");

}
}
@@ -122,8 +121,8 @@ public int compare(String o1, String o2) {
// quick indexOf-check for this one,
// so we have to run all the replaceAll-calls
for (String cName : names) { // dynamic class binding
source = source.replaceAll("([^'\".])" + Pattern.quote(cName) + "([.][^'\"])", "$1_('"
+ originalNames.get(cName).replace("$", "\\$") + "')$2");
source = source.replaceAll("([^'\".])" + Pattern.quote(cName) + "([.][^'\"])",
"$1_('" + originalNames.get(cName).replace("$", "\\$") + "')$2");

}

@@ -134,10 +133,10 @@ public int compare(String o1, String o2) {
@Override
protected void head() {
print("class ");
//This generated classname is parsed when creating cleanStackTrace.
//The part after "Template_" is used as key when
//looking up the file on disk this template-class is generated from.
//cleanStackTrace is looking in TemplateLoader.templates
// This generated classname is parsed when creating cleanStackTrace.
// The part after "Template_" is used as key when
// looking up the file on disk this template-class is generated from.
// cleanStackTrace is looking in TemplateLoader.templates

String uniqueNumberForTemplateFile = TemplateLoader.getUniqueNumberForTemplateFile(template.name);

@@ -160,12 +159,9 @@ protected void end() {
println("}");
}


/**
* Interesting performance observation:
* Calling print(); from java (in ExecutableTemplate) called from groovy is MUCH slower than
* java returning string to groovy
* which then prints with out.print();
* Interesting performance observation: Calling print(); from java (in ExecutableTemplate) called from groovy is
* MUCH slower than java returning string to groovy which then prints with out.print();
*/

@Override
@@ -180,27 +176,28 @@ protected void plain() {

// [#714] The groovy-compiler complaints if a line is more than 65535 unicode units long..
// Have to split it if it is really that big
if (text.length() <maxPlainTextLength) {
if (text.length() < maxPlainTextLength) {
// text is "short" - just print it
println("out.print(\""+text+"\");");
println("out.print(\"" + text + "\");");
} else {
// text is long - must split it
int offset = 0;
do {
int endPos = offset+maxPlainTextLength;
if (endPos>text.length()) {
int endPos = offset + maxPlainTextLength;
if (endPos > text.length()) {
endPos = text.length();
} else {
// #869 If the last char (at endPos-1) is \, we're dealing with escaped char - must include the next one also..
if ( text.charAt(endPos-1) == '\\') {
// #869 If the last char (at endPos-1) is \, we're dealing with escaped char - must include the next
// one also..
if (text.charAt(endPos - 1) == '\\') {
// use one more char so the escaping is not broken. Don't have to check length, since
// all '\' is used in escaping, ref replaceAll above..
endPos++;
}
}
println("out.print(\""+text.substring(offset, endPos)+"\");");
offset+= (endPos - offset);
}while(offset < text.length());
println("out.print(\"" + text.substring(offset, endPos) + "\");");
offset += (endPos - offset);
} while (offset < text.length());
}
}

@@ -225,15 +222,15 @@ protected void script() {
@Override
protected void expr() {
String expr = parser.getToken().trim();
print(";out.print(__safeFaster("+expr+"))");
print(";out.print(__safeFaster(" + expr + "))");
markLine(parser.getLine());
println();
}

@Override
protected void message() {
String expr = parser.getToken().trim();
print(";out.print(__getMessage("+expr+"))");
print(";out.print(__getMessage(" + expr + "))");
markLine(parser.getLine());
println();
}
@@ -243,9 +240,9 @@ protected void action(boolean absolute) {
String action = parser.getToken().trim();
if (action.trim().matches("^'.*'$")) {
if (absolute) {
print("\tout.print(__reverseWithCheck_absolute_true("+action+"));");
print("\tout.print(__reverseWithCheck_absolute_true(" + action + "));");
} else {
print("\tout.print(__reverseWithCheck_absolute_false("+action+"));");
print("\tout.print(__reverseWithCheck_absolute_false(" + action + "));");
}
} else {
if (!action.endsWith(")")) {
@@ -276,7 +273,7 @@ protected void startTag() {
}
// We only have to try to replace the following if we find at least one
// @ in tagArgs..
if (tagArgs.indexOf('@')>=0) {
if (tagArgs.indexOf('@') >= 0) {
tagArgs = tagArgs.replaceAll("[:]\\s*[@]{2}", ":actionBridge._abs().");
tagArgs = tagArgs.replaceAll("(\\s)[@]{2}", "$1actionBridge._abs().");
tagArgs = tagArgs.replaceAll("[:]\\s*[@]{1}", ":actionBridge.");
@@ -306,6 +303,8 @@ protected void startTag() {
println();
skipLineBreak = true;
return;
} catch (NoSuchMethodException e) {
// We find a tag that is not defined in GroovyInlineTags, lets see if it is defined somewhere else
} catch (Exception e) {
Logger.debug(e, "Failed to start tag %s in template %s", tag.name, template.getName());
}
@@ -319,7 +318,6 @@ protected void startTag() {
println();
}
skipLineBreak = true;

}

@Override
@@ -342,7 +340,8 @@ protected void endTag() {
print("attrs" + tagIndex + "['vars'].each() {");
print("if(toExecute.getProperty(it.key) == null) {toUnset.add(it.key);}; toExecute.setProperty(it.key, it.value);");
print("}};");
print("if(attrs" + tagIndex + "['as']) { setProperty(attrs" + tagIndex + "['as'], toExecute.toString()); } else { out.print(toExecute.toString()); }; toUnset.each() {toExecute.setProperty(it, null)} };");
print("if(attrs" + tagIndex + "['as']) { setProperty(attrs" + tagIndex
+ "['as'], toExecute.toString()); } else { out.print(toExecute.toString()); }; toUnset.each() {toExecute.setProperty(it, null)} };");
markLine(tag.startLine);
template.doBodyLines.add(currentLine);
println();
@@ -361,7 +360,7 @@ protected void endTag() {
List<Class> fastClasses = new ArrayList<>();
fastClasses.add(FastTags.class);
fastClasses.addAll(Play.classloader.getAssignableClasses(FastTags.class));

Method m = null;
String tName = tag.name;
String tSpace = "";
@@ -373,19 +372,22 @@ protected void endTag() {
if (!c.isAnnotationPresent(FastTags.Namespace.class) && tSpace.length() > 0) {
continue;
}
if (c.isAnnotationPresent(FastTags.Namespace.class) && !c.getAnnotation(FastTags.Namespace.class).value().equals(tSpace)) {
if (c.isAnnotationPresent(FastTags.Namespace.class)
&& !c.getAnnotation(FastTags.Namespace.class).value().equals(tSpace)) {
continue;
}
try {
m = c.getDeclaredMethod("_" + tName, Map.class, Closure.class, PrintWriter.class, GroovyTemplate.ExecutableTemplate.class, int.class);
m = c.getDeclaredMethod("_" + tName, Map.class, Closure.class, PrintWriter.class,
GroovyTemplate.ExecutableTemplate.class, int.class);
break;
} catch (NoSuchMethodException ex) {
// continue looking for this method in other *FastTags implementations
}
}
if (m != null) {
print("play.templates.TagContext.enterTag('" + tag.name + "');");
print("_('" + m.getDeclaringClass().getName() + "')._" + tName + "(attrs" + tagIndex + ",body" + tagIndex + ", out, this, " + tag.startLine + ");");
print("_('" + m.getDeclaringClass().getName() + "')._" + tName + "(attrs" + tagIndex + ",body" + tagIndex
+ ", out, this, " + tag.startLine + ");");
print("play.templates.TagContext.exitTag();");
} else {
print("invokeTag(" + tag.startLine + ",'" + tagName + "',attrs" + tagIndex + ",body" + tagIndex + ");");
@@ -398,5 +400,3 @@ protected void endTag() {
skipLineBreak = true;
}
}


0 comments on commit 75471a9

Please sign in to comment.