|
| 1 | +package com.github.mustachejava.codegen; |
| 2 | + |
| 3 | +import com.github.mustachejava.MustacheException; |
| 4 | +import com.github.mustachejava.codegen.guards.*; |
| 5 | +import com.github.mustachejava.reflect.*; |
| 6 | +import com.github.mustachejava.reflect.guards.*; |
| 7 | +import com.github.mustachejava.util.GuardException; |
| 8 | +import com.github.mustachejava.util.Wrapper; |
| 9 | + |
| 10 | +import java.lang.reflect.AccessibleObject; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * Generates code when it can for higher performance. Make sure you have enough |
| 15 | + * PermGen to run your application. Each variable will generate at least one guard |
| 16 | + * class and each mustache section or partial will as well. |
| 17 | + */ |
| 18 | +public class CodegenObjectHandler extends ReflectionObjectHandler { |
| 19 | + @Override |
| 20 | + protected ClassGuard createClassGuard(int i, Object scope) { |
| 21 | + return new CompilableClassGuard(i, scope); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected DepthGuard createDepthGuard(int length) { |
| 26 | + return new CompilableDepthGuard(length); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected DotGuard createDotGuard(int i, Object scope, String lookup) { |
| 31 | + return new CompilableDotGuard(lookup, i, scope); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected MapGuard createMapGuard(int scopeIndex, Wrapper[] wrappers, String name, boolean contains) { |
| 36 | + return new CompilableMapGuard(this, scopeIndex, name, contains, wrappers); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected NullGuard createNullGuard() { |
| 41 | + return new CompilableNullGuard(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected WrappedGuard createWrappedGuard(int i, List<Wrapper> wrappers, List<Guard> wrapperGuard) { |
| 46 | + return new CompilableWrappedGuard(this, i, wrappers, wrapperGuard); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + 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 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + 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 | + }; |
| 73 | + } |
| 74 | + |
| 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 | + } |
| 80 | +} |
0 commit comments