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.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    if (ClassStringField.STRING_DESC.equals(desc) && name != null && !mIgnoreClass) {
        // static final, in this condition, the value is null or not null.
        if ((access & Opcodes.ACC_STATIC) != 0 && (access & Opcodes.ACC_FINAL) != 0) {
            mStaticFinalFields.add(new ClassStringField(name, (String) value));
            value = null;/*from   w  ww  . j a va 2 s  .  co m*/
        }
        // static, in this condition, the value is null.
        if ((access & Opcodes.ACC_STATIC) != 0 && (access & Opcodes.ACC_FINAL) == 0) {
            mStaticFields.add(new ClassStringField(name, (String) value));
            value = null;
        }

        // final, in this condition, the value is null or not null.
        if ((access & Opcodes.ACC_STATIC) == 0 && (access & Opcodes.ACC_FINAL) != 0) {
            mFinalFields.add(new ClassStringField(name, (String) value));
            value = null;
        }

        // normal, in this condition, the value is null.
        if ((access & Opcodes.ACC_STATIC) != 0 && (access & Opcodes.ACC_FINAL) != 0) {
            mFields.add(new ClassStringField(name, (String) value));
            value = null;
        }
    }
    return super.visitField(access, name, desc, signature, value);
}

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

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    final MethodContext info = methods.get(MethodContext.keyOf(name, desc));
    if (info == null) {
        return cv.visitMethod(access, name, desc, signature, exceptions);
    }/*from  w  w w  . j ava2  s.c o m*/

    // method will create computation and return it
    new TramplineMethodGenerator(info).accept(cv);

    // implementation of Computation<>
    new ComputationClassGenerator(info).accept(context);

    // implementation of Continuation<>
    new ContinuationClassGenerator(info).accept(context);

    // Rename our method and transform it
    MethodVisitor mv = cv.visitMethod(access | Opcodes.ACC_FINAL /*| Opcodes.ACC_SYNTHETIC*/, name + "$async",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE }), null,
            exceptions);

    MethodVisitor methodVisitor = new AsyncMethodAdapter(info, mv) {
        @Override
        public void visitEnd() {
            super.visitEnd();
            // Now we have all information about used locals to generate
            // state class body
            new StateClassGenerator(info).accept(context);
        }
    };
    if (info.isCoroutine()) {
        methodVisitor = new CoroutineMethodAdapter(info, methodVisitor);
    }
    return methodVisitor;
}

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

License:Apache License

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();/*from w w w.  j  a  v  a 2 s . co  m*/

    // extends Object implements Computation<ValueType>
    SignatureWriter sign = new SignatureWriter();
    SignatureVisitor supsign = sign.visitSuperclass();
    supsign.visitClassType(OBJECT_NAME);
    supsign.visitEnd();
    SignatureVisitor iface = sign.visitInterface();
    iface.visitClassType(COMPUTATION_NAME);
    SignatureVisitor argsign = iface.visitTypeArgument('=');
    new SignatureReader(info.valueSignature).acceptType(argsign);
    argsign.visitEnd();

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL, info.computationClassName, sign.toString(), OBJECT_NAME,
            new String[] { COMPUTATION_NAME });

    cv.visitSource(info.ownerSource, null);
    cv.visitInnerClass(info.stateClassName, info.owner, info.stateSimpleName, 0);
    cv.visitInnerClass(info.computationClassName, info.owner, info.computationSimpleName, 0);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "state", stateDesc, null, null);

    generateConstructor(cv);
    generateExecute(cv);

    cv.visitEnd();
}

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

License:Apache License

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

    // Constructor have form either <init>(OuterClass this$0, State state)
    // or <init>(State state)
    String ctorDesc;/*from ww  w .  j av a2s.  c  om*/
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType });
    } else {
        cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "this$0", 'L' + info.owner + ';', null, null);

        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { outerType, info.stateType });
    }

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, ctorDesc, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);

    // Save state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "state", stateDesc);

    // Save outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, name, "this$0", outerType.getDescriptor());
    }
    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("state", stateDesc, null, start, end, 1 + info.thisOffset);

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

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.j  av  a 2s  .  c  o m*/
    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

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();/*from w ww.  j a v  a2 s  .c  om*/

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL, info.continuationClassName, signature, OBJECT_NAME,
            new String[] { CONTINUATION_NAME });

    cv.visitSource(info.ownerSource, null);
    cv.visitInnerClass(info.stateClassName, info.owner, info.stateSimpleName, 0);
    cv.visitInnerClass(info.continuationClassName, info.owner, info.continuationSimpleName, 0);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "state", stateDesc, null, null);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "index", "I", null, null);

    generateConstructor(cv);
    generateExecute(cv, true);
    generateExecute(cv, false);

    cv.visitEnd();
}

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

License:Apache License

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

    // Constructor have form either <init>(OuterClass this$0, State state,
    // int index)
    // or <init>(State state, int index)
    String ctorDesc;//w  w  w  .j  av  a  2 s  .  c  om
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE });
    } else {
        cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "this$0", 'L' + info.owner + ';', null, null);

        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { outerType, info.stateType, Type.INT_TYPE });
    }

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, ctorDesc, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);

    // Save state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "state", stateDesc);

    // Save index field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "index", "I");

    // Save outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, name, "this$0", outerType.getDescriptor());
    }
    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("state", stateDesc, null, start, end, 1 + info.thisOffset);
    mv.visitLocalVariable("index", "I", null, start, end, 2 + info.thisOffset);

    mv.visitMaxs(2, 3 + 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;/*w  w w  .  ja  v  a 2s. com*/
    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.jconts.instrument.gen.StateClassGenerator.java

License:Apache License

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();//ww w. j a va  2 s . co  m

    final String name = info.stateClassName;

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL /*| Opcodes.ACC_SYNTHETIC*/, name, null, OBJECT_NAME, null);

    cv.visitSource(info.ownerSource, null);
    cv.visitOuterClass(info.owner, null, null);

    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, CONTINUATION_FIELD, CONTINUATION_DESC,
            'L' + CONTINUATION_NAME + '<' + info.valueSignature + ">;", null);

    // Local variables state
    List<String> names = info.tracker.getFieldNames();
    List<Type> types = info.tracker.getFieldTypes();
    for (int i = 0; i < names.size(); ++i) {
        cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, names.get(i), types.get(i).getDescriptor(), null, null);
    }

    // Return value variable
    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, "result", OBJECT_DESC, null, null);
    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, "exception", THROWABLE_DESC, null, null);

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, DEFAULT_CTOR_DESC, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);
    mv.visitLineNumber(0, start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', null, start, end, 0);

    mv.visitMaxs(1, 1);
    mv.visitEnd();

    cv.visitEnd();
}

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

License:Apache License

/**
 * Constructor. Starts generating the Java class code.
 * /*from   w w  w . j a v  a  2 s . c  o  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);
}