Example usage for org.objectweb.asm Opcodes ACC_FINAL

List of usage examples for org.objectweb.asm Opcodes ACC_FINAL

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ACC_FINAL.

Prototype

int ACC_FINAL

To view the source code for org.objectweb.asm Opcodes ACC_FINAL.

Click Source Link

Usage

From source file:com.google.template.soy.jbcsrc.AnnotationRefTest.java

License:Apache License

@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();// w  ww . ja  v a2s.  c  o  m
    ClassData cd = cw.toClassData();
    try {
        return new MemoryClassLoader(ImmutableList.of(cd)).loadClass(cd.type().className());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e); // this should be impossible
    }
}

From source file:com.google.template.soy.jbcsrc.ExpressionTester.java

License:Apache License

private static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) {
    java.lang.reflect.Method invokeMethod;
    try {//from w  w  w  .j  a va  2 s.  c  o  m
        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(
            ExpressionTester.class.getPackage().getName() + "." + 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) {
        throw new RuntimeException(e); // this method definitely exists
    }
    Statement.concat(LocalVariable.createThisVar(generatedType, new Label(), new Label())
            .invoke(MethodRef.create(invokeMethod)).toStatement(), new Statement() {
                @Override
                void doGen(CodeBuilder adapter) {
                    adapter.visitInsn(Opcodes.RETURN);
                }
            }).writeMethod(Opcodes.ACC_PUBLIC, voidInvoke, cw);
    ClassData data = cw.toClassData();
    checkClassData(data);
    return data;
}

From source file:com.google.template.soy.jbcsrc.FieldRef.java

License:Apache License

static FieldRef createFinalField(TypeInfo owner, String name, Type type) {
    return new AutoValue_FieldRef(owner, name, type, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL,
            !BytecodeUtils.isPrimitive(type));
}

From source file:com.google.template.soy.jbcsrc.restricted.FieldRef.java

License:Apache License

public static FieldRef createFinalField(TypeInfo owner, String name, Type type) {
    return new AutoValue_FieldRef(owner, name, type, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL,
            !BytecodeUtils.isPrimitive(type));
}

From source file:com.google.template.soy.jbcsrc.restricted.FieldRef.java

License:Apache License

public static FieldRef createPublicStaticField(TypeInfo owner, String name, Type type) {
    return new AutoValue_FieldRef(owner, name, type,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, !BytecodeUtils.isPrimitive(type));
}

From source file:com.google.template.soy.jbcsrc.restricted.testing.ExpressionEvaluator.java

License:Apache License

public static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) {
    java.lang.reflect.Method invokeMethod;
    try {/*from ww  w.jav  a  2 s .  co m*/
        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) {
        throw new RuntimeException(e); // this method definitely exists
    }
    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;
}

From source file:com.google.template.soy.jbcsrc.TemplateCompiler.java

License:Apache License

/**
 * Returns the list of classes needed to implement this template.
 *
 * <p>For each template, we generate:
 * <ul>/*from w w  w.ja v a  2  s.c om*/
 *     <li>A {@link com.google.template.soy.jbcsrc.shared.CompiledTemplate.Factory}
 *     <li>A {@link CompiledTemplate}
 *     <li>A SoyAbstractCachingProvider subclass for each {@link LetValueNode} and 
 *         {@link CallParamValueNode}
 *     <li>A RenderableThunk subclass for each {@link LetContentNode} and 
 *         {@link CallParamContentNode}
 * </li>
 * 
 * <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
    // TODO(lukes): 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();

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

From source file:com.google.template.soy.jbcsrc.TemplateVariableManager.java

License:Apache License

/**
 * Adds a private static final field and returns a reference to it.
 *
 * @param proposedName  The proposed name,  the actual name will be this plus an optional suffix
 *     to ensure uniqueness/*  w w  w .j  av a  2s.c  o  m*/
 * @param initializer An expression to initialize the field.
 */
FieldRef addStaticField(String proposedName, Expression initializer) {
    String name = fieldNames.generateName(proposedName);
    FieldRef ref = new AutoValue_FieldRef(owner, name, initializer.resultType(),
            Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, !initializer.isNonNullable());
    staticFields.add(new AutoValue_TemplateVariableManager_StaticFieldVariable(ref, initializer));
    return ref;
}

From source file:com.google.template.soy.jbcsrc.VariableSet.java

License:Apache License

/**
 * Adds a private static final field and returns a reference to it.
 *
 * @param proposedName  The proposed name,  the actual name will be this plus an optional suffix
 *     to ensure uniqueness/*from   w  w  w . j av a2 s  .co  m*/
 * @param initializer An expression to initialize the field.
 */
FieldRef addStaticField(String proposedName, Expression initializer) {
    String name = fieldNames.generateName(proposedName);
    FieldRef ref = new AutoValue_FieldRef(owner, name, initializer.resultType(),
            Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, !initializer.isNonNullable());
    staticFields.add(new AutoValue_VariableSet_StaticFieldVariable(ref, initializer));
    return ref;
}

From source file:com.google.test.metric.asm.ClassInfoBuilderVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    boolean isStatic = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    boolean isFinal = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
    return new MethodVisitorBuilder(repository, classInfo, name, desc, signature, exceptions, isStatic, isFinal,
            JavaVisibility.valueFromJavaBytecode(access));
}