Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

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  w w w  . j ava 2s .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>/*w w w .j a  v a2 s .  c  o  m*/
 *     <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.TemplateCompiler.java

License:Apache License

private Statement generateRenderMethod() {
    final Label start = new Label();
    final Label end = new Label();
    final LocalVariable thisVar = createThisVar(template.typeInfo(), start, end);
    final LocalVariable appendableVar = createLocal("appendable", 1, ADVISING_APPENDABLE_TYPE, start, end)
            .asNonNullable();/*from   w  ww.  j av a  2  s. co  m*/
    final LocalVariable contextVar = createLocal("context", 2, RENDER_CONTEXT_TYPE, start, end).asNonNullable();
    final VariableSet variableSet = new VariableSet(fieldNames, template.typeInfo(), thisVar,
            template.renderMethod().method());
    TemplateNode node = template.node();
    TemplateVariables variables = new TemplateVariables(variableSet, thisVar, contextVar);
    final CompiledMethodBody methodBody = SoyNodeCompiler.create(registry, innerClasses, stateField, thisVar,
            AppendableExpression.forLocal(appendableVar), variableSet, variables).compile(node);
    final Statement returnDone = Statement.returnExpression(MethodRef.RENDER_RESULT_DONE.invoke());
    new Statement() {
        @Override
        void doGen(CodeBuilder adapter) {
            adapter.mark(start);
            methodBody.body().gen(adapter);
            adapter.mark(end);
            returnDone.gen(adapter);

            thisVar.tableEntry(adapter);
            appendableVar.tableEntry(adapter);
            contextVar.tableEntry(adapter);
            variableSet.generateTableEntries(adapter);
        }
    }.writeIOExceptionMethod(Opcodes.ACC_PUBLIC, template.renderMethod().method(), writer);
    writer.setNumDetachStates(methodBody.numberOfDetachStates());
    variableSet.defineStaticFields(writer);
    return variableSet.defineFields(writer);
}

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

License:Apache License

/** 
 * Generate a public constructor that assigns our final field and checks for missing required 
 * params.//from   w w w  . java  2 s  .c  o  m
 * 
 * <p>This constructor is called by the generate factory classes.
 *
 * @param fieldInitializers additional statements to initialize fields (other than params)
 */
private void generateConstructor(Statement fieldInitializers) {
    final Label start = new Label();
    final Label end = new Label();
    final LocalVariable thisVar = createThisVar(template.typeInfo(), start, end);
    final LocalVariable paramsVar = createLocal("params", 1, SOY_RECORD_TYPE, start, end);
    final LocalVariable ijVar = createLocal("ij", 2, SOY_RECORD_TYPE, start, end);
    final List<Statement> assignments = new ArrayList<>();
    assignments.add(fieldInitializers); // for other fields needed by the compiler.
    assignments.add(paramsField.putInstanceField(thisVar, paramsVar));
    assignments.add(ijField.putInstanceField(thisVar, ijVar));
    for (final TemplateParam param : template.node().getAllParams()) {
        Expression paramProvider = getParam(paramsVar, ijVar, param);
        assignments.add(paramFields.get(param.name()).putInstanceField(thisVar, paramProvider));
    }
    Statement constructorBody = new Statement() {
        @Override
        void doGen(CodeBuilder ga) {
            ga.mark(start);
            // call super()
            thisVar.gen(ga);
            ga.invokeConstructor(OBJECT.type(), NULLARY_INIT);
            for (Statement assignment : assignments) {
                assignment.gen(ga);
            }
            ga.visitInsn(Opcodes.RETURN);
            ga.visitLabel(end);
            thisVar.tableEntry(ga);
            paramsVar.tableEntry(ga);
            ijVar.tableEntry(ga);
        }
    };
    constructorBody.writeMethod(Opcodes.ACC_PUBLIC, template.constructor().method(), writer);
}

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

License:Apache License

/**
 * Writes the {@link CompiledTemplate.Factory#create} method, which directly delegates to the
 * constructor of the {@link #template}.
 *///from   w ww  .j  a  va  2 s. c o  m
private void generateCreateMethod(ClassVisitor cv, TypeInfo factoryType) {
    final Label start = new Label();
    final Label end = new Label();
    final LocalVariable thisVar = createThisVar(factoryType, start, end);
    final LocalVariable paramsVar = createLocal("params", 1, SOY_RECORD_TYPE, start, end);
    final LocalVariable ijVar = createLocal("ij", 2, SOY_RECORD_TYPE, start, end);
    final Statement returnTemplate = Statement
            .returnExpression(template.constructor().construct(paramsVar, ijVar));
    new Statement() {
        @Override
        void doGen(CodeBuilder ga) {
            ga.mark(start);
            returnTemplate.gen(ga);
            ga.mark(end);
            thisVar.tableEntry(ga);
            paramsVar.tableEntry(ga);
            ijVar.tableEntry(ga);
        }
    }.writeMethod(Opcodes.ACC_PUBLIC, CREATE_METHOD, cv);
}

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

License:Apache License

public static Visibility valueFromJavaBytecode(int access) {
    if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
        return Visibility.PUBLIC;
    } else if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
        return Visibility.PROTECTED;
    } else if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) {
        return Visibility.PRIVATE;
    } else {/* ww w  . j a va  2s .  c o  m*/
        return Visibility.PACKAGE;
    }
}

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

License:Apache License

public static Visibility valueOf(int access) {
    if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
        return PUBLIC;
    } else if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
        return PROTECTED;
    } else if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) {
        return PRIVATE;
    } else {/*from  w ww  .  j  av  a2s.  c om*/
        return PACKAGE;
    }
}

From source file:com.googlecode.ddom.weaver.compound.CompoundClassGenerator.java

License:Apache License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*w  w  w  . j ava 2 s  .c  o  m*/
    cv.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { ifaceName });
}

From source file:com.googlecode.ddom.weaver.compound.CompoundClassGenerator.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, name, desc, signature, exceptions);
    if (mv != null) {
        Type[] argumentTypes = Type.getArgumentTypes(desc);
        mv.visitCode();/*from  www  .  j a  va 2 s  . co  m*/
        Label l0 = new Label();
        mv.visitLabel(l0);
        for (int i = 0; i < componentClasses.length; i++) {
            String componentClass = Util.classNameToInternalName(componentClasses[i]);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, className, "c" + i, "L" + componentClass + ";");
            for (int j = 0; j < argumentTypes.length; j++) {
                mv.visitVarInsn(argumentTypes[j].getOpcode(Opcodes.ILOAD), j + 1);
            }
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, ifaceName, name, desc);
        }
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
        mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1);
        mv.visitEnd();
    }
    return null;
}