Search in sources :

Example 6 with ClassData

use of com.google.template.soy.jbcsrc.internal.ClassData in project closure-templates by google.

the class TemplateCompiler method compile.

/**
 * Returns the list of classes needed to implement this template.
 *
 * <p>For each template, we generate:
 *
 * <ul>
 *   <li>A {@link com.google.template.soy.jbcsrc.shared.CompiledTemplate.Factory}
 *   <li>A {@link CompiledTemplate}
 *   <li>A DetachableSoyValueProvider subclass for each {@link LetValueNode} and {@link
 *       CallParamValueNode}
 *   <li>A DetachableContentProvider subclass for each {@link LetContentNode} and {@link
 *       CallParamContentNode}
 *       <p>Note: This will <em>not</em> generate classes for other templates, only the template
 *       configured in the constructor. But it will generate classes that <em>reference</em> the
 *       classes that are generated for other templates. It is the callers responsibility to
 *       ensure that all referenced templates are generated and available in the classloader that
 *       ultimately loads the returned classes.
 */
Iterable<ClassData> compile() {
    List<ClassData> classes = new ArrayList<>();
    // first generate the factory
    if (template.node().getVisibility() != Visibility.PRIVATE) {
        // Don't generate factory if the template is private.  The factories are only
        // useful to instantiate templates for calls from java.  Soy->Soy calls should invoke
        // constructors directly.
        new TemplateFactoryCompiler(template, innerClasses).compile();
    }
    writer = SoyClassWriter.builder(template.typeInfo()).setAccess(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER + Opcodes.ACC_FINAL).implementing(TEMPLATE_TYPE).sourceFileName(template.node().getSourceLocation().getFileName()).build();
    generateTemplateMetadata();
    generateKindMethod();
    stateField.defineField(writer);
    paramsField.defineField(writer);
    ijField.defineField(writer);
    for (FieldRef field : paramFields.values()) {
        field.defineField(writer);
    }
    Statement fieldInitializers = generateRenderMethod();
    generateConstructor(fieldInitializers);
    innerClasses.registerAllInnerClasses(writer);
    writer.visitEnd();
    classes.add(writer.toClassData());
    classes.addAll(innerClasses.getInnerClassData());
    writer = null;
    return classes;
}
Also used : FieldRef(com.google.template.soy.jbcsrc.restricted.FieldRef) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) Statement(com.google.template.soy.jbcsrc.restricted.Statement) ArrayList(java.util.ArrayList)

Example 7 with ClassData

use of com.google.template.soy.jbcsrc.internal.ClassData in project closure-templates by google.

the class BytecodeCompiler method compileToJar.

/**
 * Compiles all the templates in the given registry to a jar file written to the given output
 * stream.
 *
 * <p>If errors are encountered, the error reporter will be updated and we will return. The
 * contents of any data written to the sink at that point are undefined.
 *
 * @param registry All the templates to compile
 * @param reporter The error reporter
 * @param sink The output sink to write the JAR to.
 */
public static void compileToJar(TemplateRegistry registry, ErrorReporter reporter, ByteSink sink) throws IOException {
    ErrorReporter.Checkpoint checkpoint = reporter.checkpoint();
    if (reporter.errorsSince(checkpoint)) {
        return;
    }
    CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry);
    if (reporter.errorsSince(checkpoint)) {
        return;
    }
    try (final SoyJarFileWriter writer = new SoyJarFileWriter(sink.openStream())) {
        compileTemplates(compilerRegistry, reporter, new CompilerListener<Void>() {

            @Override
            void onCompile(ClassData clazz) throws IOException {
                writer.writeEntry(clazz.type().internalName() + ".class", ByteSource.wrap(clazz.data()));
            }
        });
    }
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) IOException(java.io.IOException) SoyJarFileWriter(com.google.template.soy.base.internal.SoyJarFileWriter)

Aggregations

ClassData (com.google.template.soy.jbcsrc.internal.ClassData)7 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 MemoryClassLoader (com.google.template.soy.jbcsrc.internal.MemoryClassLoader)2 SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)2 Statement (com.google.template.soy.jbcsrc.restricted.Statement)2 ArrayList (java.util.ArrayList)2 Stopwatch (com.google.common.base.Stopwatch)1 SoyJarFileWriter (com.google.template.soy.base.internal.SoyJarFileWriter)1 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)1 FieldRef (com.google.template.soy.jbcsrc.restricted.FieldRef)1 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)1 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)1 IOException (java.io.IOException)1 List (java.util.List)1 Label (org.objectweb.asm.Label)1 Method (org.objectweb.asm.commons.Method)1