Skip to content

Commit e8039c5

Browse files
author
Sam Pullara
committed
Fix a bug with scala case classes causing debug to fail.
1 parent 078745b commit e8039c5

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

compiler/src/main/java/com/github/mustachejava/DefaultMustacheFactory.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
import com.google.common.util.concurrent.ListeningExecutorService;
1010
import com.google.common.util.concurrent.MoreExecutors;
1111

12-
import java.io.*;
12+
import java.io.BufferedReader;
13+
import java.io.File;
14+
import java.io.FileInputStream;
15+
import java.io.FileNotFoundException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.io.Reader;
19+
import java.io.StringReader;
20+
import java.io.Writer;
1321
import java.util.concurrent.ExecutionException;
1422
import java.util.concurrent.ExecutorService;
1523

@@ -36,8 +44,11 @@ public class DefaultMustacheFactory implements MustacheFactory {
3644
*/
3745
protected final MustacheParser mc = new MustacheParser(this);
3846

39-
// New templates that are generated at runtime are cached here. The String
40-
// key is the actual text of the templates, not a name.
47+
/**
48+
* New templates that are generated at runtime are cached here. The template key
49+
* includes the text of the template and the context so we get proper error
50+
* messages and debugging information.
51+
*/
4152
protected final LoadingCache<FragmentKey, Mustache> templateCache = createLambdaCache();
4253

4354
private final String resourceRoot;
@@ -197,9 +208,9 @@ public Mustache load(String key) throws Exception {
197208

198209
protected class FragmentCacheLoader extends CacheLoader<FragmentKey, Mustache> {
199210
@Override
200-
public Mustache load(FragmentKey templateKey) throws Exception {
201-
StringReader reader = new StringReader(templateKey.templateText);
202-
TemplateContext tc = templateKey.tc;
211+
public Mustache load(FragmentKey fragmentKey) throws Exception {
212+
StringReader reader = new StringReader(fragmentKey.templateText);
213+
TemplateContext tc = fragmentKey.tc;
203214
return compile(reader, tc.file(), tc.startChars(), tc.endChars());
204215
}
205216
}

compiler/src/main/java/com/github/mustachejava/reflect/GuardedBinding.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ protected synchronized Wrapper getWrapper(String name, Object[] scopes) {
112112
.append("in");
113113
for (Object scope : scopes) {
114114
if (scope != null) {
115-
sb.append(" ").append(scope.getClass().getSimpleName());
115+
Class aClass = scope.getClass();
116+
try {
117+
sb.append(" ").append(aClass.getSimpleName());
118+
} catch (Exception e) {
119+
// Some generated classes don't have simple names
120+
try {
121+
sb.append(" ").append(aClass.getName());
122+
} catch (Exception e1) {
123+
// Some generated classes have proper names at all
124+
}
125+
}
116126
}
117127
}
118128
logger.warning(sb.toString());

compiler/src/test/java/com/github/mustachejava/functions/TranslateBundleTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
public class TranslateBundleTest {
1717

1818
private static File root;
19+
private static final String BUNDLE = "com.github.mustachejava.functions.translatebundle";
1920

2021
@Test
2122
public void testTranslation() throws MustacheException, IOException, ExecutionException, InterruptedException {
2223
MustacheFactory c = new DefaultMustacheFactory(root);
2324
Mustache m = c.compile("translatebundle.html");
2425
StringWriter sw = new StringWriter();
25-
Map scope = new HashMap();
26-
scope.put("trans", new TranslateBundleFunction("com.github.mustachejava.functions.translatebundle", Locale.US));
26+
Map<String, Object> scope = new HashMap<String, Object>();
27+
scope.put("trans", new TranslateBundleFunction(BUNDLE, Locale.US));
2728
m.execute(sw, scope);
2829
assertEquals(getContents(root, "translatebundle.txt"), sw.toString());
2930
}

0 commit comments

Comments
 (0)