use of com.google.template.soy.jbcsrc.internal.MemoryClassLoader in project closure-templates by google.
the class ExpressionTester method load.
private static <T> T load(Class<T> clazz, ClassData data) {
MemoryClassLoader loader = new MemoryClassLoader(ImmutableList.of(data));
Class<?> generatedClass;
try {
generatedClass = loader.loadClass(data.type().className());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
return generatedClass.asSubclass(clazz).getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
use of com.google.template.soy.jbcsrc.internal.MemoryClassLoader in project closure-templates by google.
the class BytecodeCompiler method compile.
/**
* Compiles all the templates in the given registry.
*
* @param registry All the templates to compile
* @param developmentMode Whether or not we are in development mode. In development mode we
* compile classes lazily
* @param reporter The error reporter
* @return CompiledTemplates or {@code absent()} if compilation fails, in which case errors will
* have been reported to the error reporter.
*/
public static Optional<CompiledTemplates> compile(final TemplateRegistry registry, boolean developmentMode, ErrorReporter reporter) {
final Stopwatch stopwatch = Stopwatch.createStarted();
ErrorReporter.Checkpoint checkpoint = reporter.checkpoint();
if (reporter.errorsSince(checkpoint)) {
return Optional.absent();
}
CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry);
if (developmentMode) {
CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new CompilingClassLoader(compilerRegistry));
// TODO(lukes): consider spawning a thread to load all the generated classes in the background
return Optional.of(templates);
}
// TODO(lukes): once most internal users have moved to precompilation eliminate this and just
// use the 'developmentMode' path above. This hybrid only makes sense for production services
// that are doing runtime compilation. Hopefully, this will become an anomaly.
List<ClassData> classes = compileTemplates(compilerRegistry, reporter, new CompilerListener<List<ClassData>>() {
final List<ClassData> compiledClasses = new ArrayList<>();
int numBytes = 0;
int numFields = 0;
int numDetachStates = 0;
@Override
public void onCompile(ClassData clazz) {
numBytes += clazz.data().length;
numFields += clazz.numberOfFields();
numDetachStates += clazz.numberOfDetachStates();
compiledClasses.add(clazz);
}
@Override
public List<ClassData> getResult() {
logger.log(Level.INFO, "Compilation took {0}\n" + " templates: {1}\n" + " classes: {2}\n" + " bytes: {3}\n" + " fields: {4}\n" + " detachStates: {5}", new Object[] { stopwatch.toString(), registry.getAllTemplates().size(), compiledClasses.size(), numBytes, numFields, numDetachStates });
return compiledClasses;
}
});
if (reporter.errorsSince(checkpoint)) {
return Optional.absent();
}
CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new MemoryClassLoader(classes));
stopwatch.reset().start();
templates.loadAll(compilerRegistry.getTemplateNames());
logger.log(Level.INFO, "Loaded all classes in {0}", stopwatch);
return Optional.of(templates);
}
use of com.google.template.soy.jbcsrc.internal.MemoryClassLoader 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);
}
}
Aggregations