Search in sources :

Example 1 with MemoryClassLoader

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

Example 2 with MemoryClassLoader

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);
}
Also used : Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) ErrorReporter(com.google.template.soy.error.ErrorReporter) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) MemoryClassLoader(com.google.template.soy.jbcsrc.internal.MemoryClassLoader) ArrayList(java.util.ArrayList) List(java.util.List) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 3 with MemoryClassLoader

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

Aggregations

MemoryClassLoader (com.google.template.soy.jbcsrc.internal.MemoryClassLoader)3 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)2 Stopwatch (com.google.common.base.Stopwatch)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)1 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1