Skip to content

Commit e35e628

Browse files
committed
more refactoring
1 parent 8e468c4 commit e35e628

11 files changed

Lines changed: 108 additions & 86 deletions

File tree

codegen/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
<version>4.8.2</version>
5353
<scope>test</scope>
5454
</dependency>
55+
<dependency>
56+
<groupId>org.jruby</groupId>
57+
<artifactId>jruby</artifactId>
58+
<version>1.6.7</version>
59+
<scope>test</scope>
60+
</dependency>
5561

5662
</dependencies>
5763
</project>
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
11
package com.github.mustachejava.codegen;
22

33
import com.github.mustachejava.*;
4-
import com.github.mustachejava.codes.DefaultMustache;
54

65
import java.io.File;
7-
import java.io.Writer;
86

97
/**
108
* Codegen mustache code execution.
119
*/
1210
public class CodegenMustacheFactory extends DefaultMustacheFactory {
1311
@Override
1412
public MustacheVisitor createMustacheVisitor() {
15-
return new DefaultMustacheVisitor(this) {
16-
@Override
17-
public Mustache mustache(TemplateContext templateContext) {
18-
return new DefaultMustache(templateContext, cf, list.toArray(new Code[list.size()]), templateContext.file()) {
19-
20-
private CompiledCodes compiledCodes;
21-
22-
@Override
23-
public Writer run(Writer writer, Object[] scopes) {
24-
if (compiledCodes != null) {
25-
return compiledCodes.runCodes(writer, scopes);
26-
}
27-
return super.run(writer, scopes);
28-
}
29-
30-
@Override
31-
public void setCodes(Code[] newcodes) {
32-
super.setCodes(newcodes);
33-
if (newcodes != null) {
34-
compiledCodes = CodeCompiler.compile(newcodes);
35-
}
36-
}
37-
};
38-
}
39-
};
13+
return new CodegenMustacheVisitor(this);
4014
}
4115

4216
public CodegenMustacheFactory() {
@@ -53,4 +27,5 @@ public CodegenMustacheFactory(String resourceRoot) {
5327
super(resourceRoot);
5428
setObjectHandler(new CodegenObjectHandler());
5529
}
30+
5631
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.mustachejava.codegen;
2+
3+
import com.github.mustachejava.*;
4+
import com.github.mustachejava.codes.DefaultMustache;
5+
6+
import java.io.Writer;
7+
8+
public class CodegenMustacheVisitor extends DefaultMustacheVisitor {
9+
public CodegenMustacheVisitor(DefaultMustacheFactory mf) {
10+
super(mf);
11+
}
12+
13+
@Override
14+
public Mustache mustache(TemplateContext templateContext) {
15+
return new DefaultMustache(templateContext, cf, list.toArray(new Code[list.size()]), templateContext.file()) {
16+
17+
private CompiledCodes compiledCodes;
18+
19+
@Override
20+
public Writer run(Writer writer, Object[] scopes) {
21+
if (compiledCodes != null) {
22+
return compiledCodes.runCodes(writer, scopes);
23+
}
24+
return super.run(writer, scopes);
25+
}
26+
27+
@Override
28+
public void setCodes(Code[] newcodes) {
29+
super.setCodes(newcodes);
30+
if (newcodes != null) {
31+
compiledCodes = CodeCompiler.compile(newcodes);
32+
}
33+
}
34+
};
35+
}
36+
}

codegen/src/main/java/com/github/mustachejava/codegen/CodegenObjectHandler.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.mustachejava.codegen;
22

3-
import com.github.mustachejava.MustacheException;
43
import com.github.mustachejava.codegen.guards.*;
5-
import com.github.mustachejava.reflect.*;
4+
import com.github.mustachejava.reflect.Guard;
5+
import com.github.mustachejava.reflect.MissingWrapper;
6+
import com.github.mustachejava.reflect.ReflectionObjectHandler;
67
import com.github.mustachejava.reflect.guards.*;
7-
import com.github.mustachejava.util.GuardException;
88
import com.github.mustachejava.util.Wrapper;
99

1010
import java.lang.reflect.AccessibleObject;
@@ -48,33 +48,12 @@ protected WrappedGuard createWrappedGuard(int i, List<Wrapper> wrappers, List<Gu
4848

4949
@Override
5050
protected MissingWrapper createMissingWrapper(List<Guard> guards) {
51-
final Guard compiledGuards = compile(guards);
52-
return new MissingWrapper(guards.toArray(new Guard[guards.size()])) {
53-
@Override
54-
protected void guardCall(Object[] scopes) throws GuardException {
55-
if (!compiledGuards.apply(scopes)) {
56-
throw guardException;
57-
}
58-
}
59-
};
51+
return new CompiledMissingWrapper(guards);
6052
}
6153

6254
@Override
6355
protected Wrapper createWrapper(int scopeIndex, Wrapper[] wrappers, List<? extends Guard> guards, AccessibleObject member, Object[] arguments) {
64-
final Guard compiledGuards = compile(guards);
65-
return new ReflectionWrapper(scopeIndex, wrappers, guards.toArray(new Guard[guards.size()]), member, arguments, this) {
66-
@Override
67-
protected void guardCall(Object[] scopes) throws GuardException {
68-
if (!compiledGuards.apply(scopes)) {
69-
throw guardException;
70-
}
71-
}
72-
};
56+
return new CompiledReflectionWrapper(this, scopeIndex, wrappers, guards, member, arguments);
7357
}
7458

75-
private Guard compile(List<? extends Guard> guard) {
76-
Guard[] compiled = GuardCompiler.compile(guard.toArray(new Guard[guard.size()]));
77-
if (compiled.length != 1) throw new MustacheException("Failed to compile all guards: " + guard);
78-
return compiled[0];
79-
}
8059
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.mustachejava.codegen;
2+
3+
import com.github.mustachejava.reflect.Guard;
4+
import com.github.mustachejava.reflect.MissingWrapper;
5+
import com.github.mustachejava.util.GuardException;
6+
7+
import java.util.List;
8+
9+
public class CompiledMissingWrapper extends MissingWrapper {
10+
final Guard compiledGuards;
11+
12+
public CompiledMissingWrapper(List<Guard> guards) {
13+
super(guards.toArray(new Guard[guards.size()]));
14+
compiledGuards = GuardCompiler.compile(guards);
15+
}
16+
17+
@Override
18+
protected void guardCall(Object[] scopes) throws GuardException {
19+
if (!compiledGuards.apply(scopes)) {
20+
throw guardException;
21+
}
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.mustachejava.codegen;
2+
3+
import com.github.mustachejava.reflect.Guard;
4+
import com.github.mustachejava.reflect.ReflectionWrapper;
5+
import com.github.mustachejava.util.GuardException;
6+
import com.github.mustachejava.util.Wrapper;
7+
8+
import java.lang.reflect.AccessibleObject;
9+
import java.util.List;
10+
11+
public class CompiledReflectionWrapper extends ReflectionWrapper {
12+
final Guard compiledGuards;
13+
14+
public CompiledReflectionWrapper(CodegenObjectHandler codegenObjectHandler, int scopeIndex, Wrapper[] wrappers, List<? extends Guard> guards, AccessibleObject member, Object[] arguments) {
15+
super(scopeIndex, wrappers, guards.toArray(new Guard[guards.size()]), member, arguments, codegenObjectHandler);
16+
compiledGuards = GuardCompiler.compile(guards);
17+
}
18+
19+
@Override
20+
protected void guardCall(Object[] scopes) throws GuardException {
21+
if (!compiledGuards.apply(scopes)) {
22+
throw guardException;
23+
}
24+
}
25+
}

codegen/src/main/java/com/github/mustachejava/codegen/GuardCompiler.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.mustachejava.codegen;
22

3+
import com.github.mustachejava.MustacheException;
34
import com.github.mustachejava.reflect.Guard;
45
import org.objectweb.asm.ClassWriter;
56
import org.objectweb.asm.Label;
@@ -20,27 +21,23 @@
2021
public class GuardCompiler {
2122
private static AtomicInteger id = new AtomicInteger(0);
2223

23-
public static Guard[] compile(Guard[] guards) {
24-
List<Guard> finalGuards = new ArrayList<Guard>();
24+
public static Guard compile(List<? extends Guard> guards) {
2525
List<CompilableGuard> compilableGuards = new ArrayList<CompilableGuard>();
2626
for (Guard guard : guards) {
2727
if (guard instanceof CompilableGuard) {
2828
compilableGuards.add((CompilableGuard) guard);
2929
} else {
30-
finalGuards.add(guard);
30+
throw new MustacheException("Invalid guard for compilation: " + guard);
3131
}
3232
}
3333
try {
34-
Guard compiledGuard = compile("compiledguards", compilableGuards);
35-
finalGuards.add(0, compiledGuard);
36-
return finalGuards.toArray(new Guard[finalGuards.size()]);
34+
return compile("compiledguards:" + guards.size(), compilableGuards);
3735
} catch (Exception e) {
38-
e.printStackTrace();
39-
return guards;
36+
throw new MustacheException("Compilation failure", e);
4037
}
4138
}
4239

43-
public static Guard compile(String source, Iterable<CompilableGuard> guards) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
40+
private static Guard compile(String source, Iterable<CompilableGuard> guards) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
4441
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
4542
int classId = id.incrementAndGet();
4643
String className = "com.github.mustachejava.codegen.CompiledGuards" + classId;

codegen/src/main/java/com/github/mustachejava/codegen/guards/CompilableWrappedGuard.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.mustachejava.codegen.guards;
22

3-
import com.github.mustachejava.MustacheException;
43
import com.github.mustachejava.ObjectHandler;
54
import com.github.mustachejava.codegen.CompilableGuard;
65
import com.github.mustachejava.codegen.GuardCompiler;
@@ -26,12 +25,7 @@ public class CompilableWrappedGuard extends WrappedGuard implements CompilableGu
2625
@SuppressWarnings("unchecked")
2726
public CompilableWrappedGuard(ObjectHandler oh, int index, List<Wrapper> wrappers, List<Guard> wrapperGuard) {
2827
super(oh, index, wrappers, wrapperGuard);
29-
Guard[] guards = GuardCompiler.compile(wrapperGuard.toArray(new Guard[wrapperGuard.size()]));
30-
if (guards.length != 1) {
31-
throw new MustacheException("Invalid guard in wrappers: " + wrapperGuard);
32-
} else {
33-
guard = guards[0];
34-
}
28+
guard = GuardCompiler.compile(wrapperGuard);
3529
}
3630

3731
@Override

codegen/src/test/java/com/github/mustachejava/codegen/CompiledGuardTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testCompiledGuard() throws NoSuchMethodException, InvocationTargetEx
3333
List<CompilableGuard> guards = new ArrayList<CompilableGuard>();
3434
guards.add(stringClassGuard);
3535

36-
Guard testGuard = compile(source, guards);
36+
Guard testGuard = compile(guards);
3737
assertTrue("string is ok", testGuard.apply(new Object[]{"test", 1}));
3838
assertFalse("integer is not ok", testGuard.apply(new Object[]{1, "test"}));
3939
}

indy/pom.xml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,10 @@
1919
<dependencies>
2020
<dependency>
2121
<groupId>com.github.spullara.mustache.java</groupId>
22-
<artifactId>compiler</artifactId>
22+
<artifactId>codegen</artifactId>
2323
<version>0.8.5-SNAPSHOT</version>
2424
</dependency>
2525

26-
<!-- ASM -->
27-
<dependency>
28-
<groupId>org.ow2.asm</groupId>
29-
<artifactId>asm-commons</artifactId>
30-
<version>4.0</version>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.ow2.asm</groupId>
34-
<artifactId>asm-util</artifactId>
35-
<version>4.0</version>
36-
</dependency>
37-
3826
<!-- JRuby -->
3927
<dependency>
4028
<groupId>org.jruby</groupId>

0 commit comments

Comments
 (0)