Search in sources :

Example 1 with SoyClassWriter

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;
}
Also used : Statement.returnExpression(com.google.template.soy.jbcsrc.restricted.Statement.returnExpression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 2 with SoyClassWriter

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());
}
Also used : SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 3 with SoyClassWriter

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;
}
Also used : Statement(com.google.template.soy.jbcsrc.restricted.Statement) Label(org.objectweb.asm.Label) Method(org.objectweb.asm.commons.Method) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo) CodeBuilder(com.google.template.soy.jbcsrc.restricted.CodeBuilder) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter)

Example 4 with SoyClassWriter

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);
    }
}
Also used : ClassData(com.google.template.soy.jbcsrc.internal.ClassData) MemoryClassLoader(com.google.template.soy.jbcsrc.internal.MemoryClassLoader) SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter)

Example 5 with SoyClassWriter

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;
}
Also used : Statement.returnExpression(com.google.template.soy.jbcsrc.restricted.Statement.returnExpression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Aggregations

SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)5 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)4 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement.returnExpression (com.google.template.soy.jbcsrc.restricted.Statement.returnExpression)2 MemoryClassLoader (com.google.template.soy.jbcsrc.internal.MemoryClassLoader)1 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 Label (org.objectweb.asm.Label)1 Method (org.objectweb.asm.commons.Method)1