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.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

public static byte[] compileBasic(TokenSource<RPNToken> source) throws ParsingError {
    // initialize class
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null,
            CompiledClassBase.class.getName().replace('.', '/'),
            new String[] { Evaluator.Basic.class.getName().replace('.', '/') });

    // create constructor
    MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    construct.visitCode();/*from  w w  w. ja  v  a  2  s.c  om*/
    construct.visitVarInsn(Opcodes.ALOAD, 0);
    construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'),
            "<init>", "()V");
    construct.visitInsn(Opcodes.RETURN);
    construct.visitMaxs(0, 0);
    construct.visitEnd();

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "()D", null,
            new String[] { "com/github/rgcjonas/kuemmelgtr/core/ParsingError",
                    "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" });

    compileCommon(source, method, null);

    writer.visitEnd();
    return writer.toByteArray();
}

From source file:com.github.veithen.phos.enforcer.ClassProcessor.java

License:Apache License

private static boolean isPublic(int access) {
    return (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0;
}

From source file:com.google.code.jconts.instrument.gen.ComputationClassGenerator.java

License:Apache License

private void generateExecute(ClassVisitor cv) {
    final String name = info.computationClassName;
    final Type outerType = Type.getObjectType(info.owner);

    // Generate execute(Continuation<T> cont);
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC, COMPUTATION_EXECUTE_NAME,
            COMPUTATION_EXECUTE_DESC, executeSignature(), null);
    mv.visitCode();// w w w. jav  a2 s .c  om
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    // Load outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, name, "this$0", outerType.getDescriptor());
    }

    // Load state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc);

    // state.continuation = continuation
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);

    // Initial state (0)
    mv.visitIntInsn(Opcodes.BIPUSH, 0);
    mv.visitMethodInsn(info.isStatic() ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, info.owner,
            info.name + "$async",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE }));

    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0);
    if (!info.isStatic()) {
        mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1);
    }

    SignatureWriter sign = new SignatureWriter();
    contSignature(sign);
    mv.visitLocalVariable("continuation", CONTINUATION_DESC, sign.toString(), start, end, 1 + info.thisOffset);

    mv.visitMaxs(3 + info.thisOffset, 2 + info.thisOffset);
    mv.visitEnd();
}

From source file:com.google.code.jconts.instrument.gen.ContinuationClassGenerator.java

License:Apache License

private void generateExecute(ClassVisitor cv, boolean execute) {
    final String name = info.continuationClassName;
    final Type outerType = Type.getObjectType(info.owner);

    // Generate invoke(T result);
    String signature = null;//  www.  j  a v  a 2s  . co m
    if (execute) {
        SignatureWriter sign = new SignatureWriter();
        sign.visitParameterType().visitTypeVariable("T");
        sign.visitReturnType().visitBaseType('V'); // void
        signature = sign.toString();
    }
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC,
            execute ? CONTINUATION_INVOKE_NAME : CONTINUATION_SET_EXCEPTION_NAME,
            execute ? CONTINUATION_INVOKE_DESC : CONTINUATION_SET_EXCEPTION_DESC, signature, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    // Load outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, name, "this$0", outerType.getDescriptor());
    }

    // state.result = result or state.exception = throwable
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, execute ? "result" : "exception",
            execute ? OBJECT_DESC : THROWABLE_DESC);

    // Load state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc);

    // Continue from this index or index+1 (for exception)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, name, "index", "I");
    if (!execute) {
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IADD);
    }

    mv.visitMethodInsn(info.isStatic() ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, info.owner,
            info.name + "$async",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE }));

    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0);
    if (!info.isStatic()) {
        mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1);
    }
    mv.visitLocalVariable("result", OBJECT_DESC, "TT;", start, end, 1 + info.thisOffset);

    mv.visitMaxs(3 + info.thisOffset + (execute ? 0 : 1), 2 + info.thisOffset);
    mv.visitEnd();
}

From source file:com.google.code.jtracert.instrument.impl.asm.JTracertClassAdapter.java

License:Open Source License

/**
 *
 */// w  ww.  ja va 2  s . c  o  m
@Override
public void visitEnd() {

    if (!isInterface) {
        // Apply class transformations
    }

    if ("java.lang.System".equals(getClassName()) || "java/lang/System".equals(getClassName())) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_SYNCHRONIZED,
                "constructor", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 1);
        mv.visitEnd();
    }

    super.visitEnd();
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * Constructor. Starts generating the Java class code.
 * //from   www .  ja  v  a2s.co m
 * @param name accessor class name
 * @param isSetter if we building accessor for setter.
 */
public AccessorBuilder(String name, boolean isSetter) {
    this.isSetter = isSetter;

    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    Type owner = Type.getType('L' + name + ';');
    String iface = isSetter ? "com/google/code/nanorm/internal/introspect/Setter"
            : "com/google/code/nanorm/internal/introspect/Getter";
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] { iface });

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "type", "Ljava/lang/reflect/Type;", null, null);

    visitConstructor(owner);
    visitGetType(owner);
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * Generate code for getter constructor.
 * @param owner type representing the class being generated
 *//*  w w w . j  a v a  2  s .  co  m*/
private void visitConstructor(Type owner) {
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, ACCESSOR_CTOR, null, null, cw);

    mg.loadThis();
    mg.invokeConstructor(OBJECT_TYPE, CTOR);
    mg.loadThis();
    mg.loadArg(0);
    mg.putField(owner, "type", JL_REFLECT_TYPE_TYPE);
    mg.returnValue();
    mg.endMethod();
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * Generate/*from   w  w  w  .j  a v  a 2s . c  o m*/
 * {@link com.google.code.nanorm.internal.introspect.Getter#getType()}
 * method.
 * 
 * @param owner type representing the class being generated
 */
private void visitGetType(Type owner) {
    Method getTypeMethod = Method.getMethod("java.lang.reflect.Type getType()");

    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getTypeMethod, null, null, cw);

    mg.loadThis();
    mg.getField(owner, "type", JL_REFLECT_TYPE_TYPE);
    mg.returnValue();
    mg.endMethod();
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * {@inheritDoc}/*from  www.j  a v a2 s  .c  om*/
 */
public void visitBegin(Class<?> beanClass, String path) {
    this.fullPath = path;
    this.initialBeanClass = beanClass;

    if (isSetter) {
        // void setValue(Object instance);
        accessormg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, SET_VALUE, null, null, cw);
    } else {
        // Object getValue(Object instance);
        accessormg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_VALUE, null, null, cw);
    }
    npeLocal = accessormg.newLocal(Type.INT_TYPE);
    accessormg.visitCode();

    // Load argument and remember type on the stack (which is Object),
    // see instance parameter of Getter#getValue/Setter#setValue
    accessormg.loadArg(0);
    declaredClass = Object.class;
}

From source file:com.google.code.nanorm.internal.introspect.asm.MapperBuilder.java

License:Apache License

/**
 * Build mapper.//from  w w w.  j  av  a  2  s  . c  om
 * 
 * @param name class name
 * @param mapper mapper interface or base class
 * @param configs method configurations
 * @return mapper byte-code
 */
public static byte[] buildMapper(String name, Class<?> mapper, MethodConfig[] configs) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    Type owner = Type.getType('L' + name + ';');

    if (mapper.isInterface()) {
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object",
                new String[] { mapper.getName().replace('.', '/') });
    } else {
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, mapper.getName().replace('.', '/'), null);
    }

    // Copy the annotations
    copyAnnotations(mapper, cw, null);

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegate", QUERY_DELEGATE_TYPE.getDescriptor(),
            null, null);

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "configs",
            STATEMENT_CONFIGS_ARR_TYPE.getDescriptor(), null, null);

    if (mapper.isInterface()) {
        visitConstructor(cw, owner, OBJECT_TYPE);
    } else {
        visitConstructor(cw, owner, Type.getType(mapper));
    }

    for (MethodConfig cfg : configs) {
        visitMethod(owner, cw, cfg);
    }

    cw.visitEnd();

    return cw.toByteArray();
}