use of com.google.template.soy.jbcsrc.internal.SoyClassWriter in project closure-templates by google.
the class LazyClosureCompiler method compileLazyExpression.
Expression compileLazyExpression(String namePrefix, SoyNode declaringNode, String varName, ExprNode exprNode) {
Optional<Expression> asSoyValueProvider = expressionToSoyValueProviderCompiler.compileAvoidingDetaches(exprNode);
if (asSoyValueProvider.isPresent()) {
return asSoyValueProvider.get();
}
TypeInfo type = innerClasses.registerInnerClassWithGeneratedName(getProposedName(namePrefix, varName), LAZY_CLOSURE_ACCESS);
SoyClassWriter writer = SoyClassWriter.builder(type).setAccess(LAZY_CLOSURE_ACCESS).extending(DETACHABLE_VALUE_PROVIDER_TYPE).sourceFileName(declaringNode.getSourceLocation().getFileName()).build();
Expression expr = new CompilationUnit(writer, type, DETACHABLE_VALUE_PROVIDER_TYPE, declaringNode).compileExpression(exprNode);
innerClasses.registerAsInnerClass(writer, type);
writer.visitEnd();
innerClasses.add(writer.toClassData());
return expr;
}
use of com.google.template.soy.jbcsrc.internal.SoyClassWriter in project closure-templates by google.
the class TemplateFactoryCompiler method compile.
/**
* Compiles the factory.
*/
void compile() {
TypeInfo factoryType = innerClasses.registerInnerClass(FACTORY_CLASS, FACTORY_ACCESS);
SoyClassWriter cw = SoyClassWriter.builder(factoryType).implementing(FACTORY_TYPE).setAccess(FACTORY_ACCESS).sourceFileName(template.node().getSourceLocation().getFileName()).build();
innerClasses.registerAsInnerClass(cw, factoryType);
generateStaticInitializer(cw);
defineDefaultConstructor(cw, factoryType);
generateCreateMethod(cw, factoryType);
cw.visitEnd();
innerClasses.add(cw.toClassData());
}
use of com.google.template.soy.jbcsrc.internal.SoyClassWriter in project closure-templates by google.
the class ExpressionTester method createClass.
public static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) {
java.lang.reflect.Method invokeMethod;
try {
invokeMethod = targetInterface.getMethod("invoke");
} catch (NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
Class<?> returnType = invokeMethod.getReturnType();
if (!Type.getType(returnType).equals(expr.resultType())) {
if (!returnType.equals(Object.class) || expr.resultType().getSort() != Type.OBJECT) {
throw new IllegalArgumentException(targetInterface + " is not appropriate for this expression");
}
}
TypeInfo generatedType = TypeInfo.create(Names.CLASS_PREFIX + targetInterface.getSimpleName() + "Impl");
SoyClassWriter cw = SoyClassWriter.builder(generatedType).setAccess(Opcodes.ACC_FINAL | Opcodes.ACC_SUPER | Opcodes.ACC_PUBLIC).implementing(TypeInfo.create(targetInterface)).build();
BytecodeUtils.defineDefaultConstructor(cw, generatedType);
Method invoke = Method.getMethod(invokeMethod);
Statement.returnExpression(expr).writeMethod(Opcodes.ACC_PUBLIC, invoke, cw);
Method voidInvoke;
try {
voidInvoke = Method.getMethod(Invoker.class.getMethod("voidInvoke"));
} catch (NoSuchMethodException | SecurityException e) {
// this method definitely exists
throw new RuntimeException(e);
}
Statement.concat(LocalVariable.createThisVar(generatedType, new Label(), new Label()).invoke(MethodRef.create(invokeMethod)).toStatement(), new Statement() {
@Override
protected void doGen(CodeBuilder adapter) {
adapter.visitInsn(Opcodes.RETURN);
}
}).writeMethod(Opcodes.ACC_PUBLIC, voidInvoke, cw);
ClassData data = cw.toClassData();
checkClassData(data);
return data;
}
use of com.google.template.soy.jbcsrc.internal.SoyClassWriter in project closure-templates by google.
the class AnnotationRefTest method createClassWithAnnotation.
@SuppressWarnings("unchecked")
private static <T extends Annotation> Class<?> createClassWithAnnotation(T ann) {
TypeInfo generatedType = TypeInfo.create(AnnotationRefTest.class.getPackage().getName() + ".Tmp");
SoyClassWriter cw = SoyClassWriter.builder(generatedType).setAccess(Opcodes.ACC_FINAL | Opcodes.ACC_SUPER | Opcodes.ACC_PUBLIC).build();
AnnotationRef.forType((Class<T>) ann.annotationType()).write(ann, cw);
cw.visitEnd();
ClassData cd = cw.toClassData();
try {
return new MemoryClassLoader(ImmutableList.of(cd)).loadClass(cd.type().className());
} catch (ClassNotFoundException e) {
// this should be impossible
throw new RuntimeException(e);
}
}
use of com.google.template.soy.jbcsrc.internal.SoyClassWriter in project closure-templates by google.
the class LazyClosureCompiler method compileLazyContent.
Expression compileLazyContent(String namePrefix, RenderUnitNode renderUnit, String varName) {
Optional<Expression> asRawText = asRawTextOnly(renderUnit);
if (asRawText.isPresent()) {
return asRawText.get();
}
TypeInfo type = innerClasses.registerInnerClassWithGeneratedName(getProposedName(namePrefix, varName), LAZY_CLOSURE_ACCESS);
SoyClassWriter writer = SoyClassWriter.builder(type).setAccess(LAZY_CLOSURE_ACCESS).extending(DETACHABLE_CONTENT_PROVIDER_TYPE).sourceFileName(renderUnit.getSourceLocation().getFileName()).build();
Expression expr = new CompilationUnit(writer, type, DETACHABLE_CONTENT_PROVIDER_TYPE, renderUnit).compileRenderable(renderUnit);
innerClasses.registerAsInnerClass(writer, type);
writer.visitEnd();
innerClasses.add(writer.toClassData());
return expr;
}
Aggregations