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;
}
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()));
}
});
}
}
Aggregations