Example usage for org.objectweb.asm.commons InstructionAdapter InstructionAdapter

List of usage examples for org.objectweb.asm.commons InstructionAdapter InstructionAdapter

Introduction

In this page you can find the example usage for org.objectweb.asm.commons InstructionAdapter InstructionAdapter.

Prototype

protected InstructionAdapter(final int api, final MethodVisitor methodVisitor) 

Source Link

Document

Constructs a new InstructionAdapter .

Usage

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.LiftingParticipantAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (name.startsWith(LIFT_PREFIX)) {
        final MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, null, null);
        return new InstructionAdapter(this.api, methodVisitor) {
            private Label done = null;

            @Override/*from  w  w  w  .  j  a v a2 s . c  om*/
            public void visitTypeInsn(int opcode, String type) {
                if (isRelevantAllocation(opcode, type))
                    insertParticipantSequence(type);
                super.visitTypeInsn(opcode, type);
            }

            boolean isRelevantAllocation(int opcode, String type) {
                return opcode == NEW && !(type.equals(LIFTING_FAILED_EXCEPTION)
                        || type.equals(LIFTING_VETO_EXCEPTION) || type.equals(WRONG_ROLE_EXCEPTION));
            }

            void insertParticipantSequence(String roleType) {
                // o = Team._OT$liftingParticipant.createRole(aTeam, aBase, roleType);
                getstatic(TEAM_SLASH, LIFTING_PARTICIPANT_FIELD, 'L' + ILIFTING_PARTICIPANT + ';');
                visitVarInsn(ALOAD, 0); // team          : Team
                visitVarInsn(ALOAD, 1); // base            : Object
                visitLdcInsn(roleType); // role class name   : String
                invokeinterface(ILIFTING_PARTICIPANT, CREATE_ROLE_METHOD, CREATE_ROLE_DESC);

                // if (o != null)
                dup();
                Label doCreate = new Label();
                ifnull(doCreate);

                // { ...
                checkcast(Type.getObjectType(roleType));
                done = new Label();
                goTo(done);

                // } else {...
                visitLabel(doCreate);
                pop(); // discard unused DUP above
                // ... continue with original role allocation...
            }

            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                super.visitMethodInsn(opcode, owner, name, desc, itf);
                if (done != null && opcode == INVOKESPECIAL) { // is it the <init> invocation after the original "new"?
                    // }
                    visitLabel(done);
                    done = null;
                }
            }
        };
    }
    return null;
}

From source file:org.ehcache.impl.internal.store.offheap.AssertingOffHeapValueHolder.java

License:Apache License

private static boolean isLockedInFrame(StackTraceElement ste) {
    try {/*from  w  ww.  j  a  va 2 s .c  o  m*/
        ClassReader reader = new ClassReader(ste.getClassName());

        NavigableMap<Integer, Integer> lockLevels = new TreeMap<>();

        reader.accept(new ClassVisitor(ASM6) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                    String[] exceptions) {
                if (ste.getMethodName().equals(name)) {
                    return new InstructionAdapter(ASM6, new MethodVisitor(ASM6) {
                    }) {

                        private final Map<Label, Integer> levels = new HashMap<>();

                        private int lockLevel;

                        @Override
                        public void invokeinterface(String owner, String name, String descriptor) {
                            if (LOCK_CLASS.equals(getObjectType(owner))) {
                                if (LOCK_METHOD.equals(new Method(name, descriptor))) {
                                    lockLevel++;
                                } else if (UNLOCK_METHOD.equals(new Method(name, descriptor))) {
                                    lockLevel--;
                                }
                            }
                        }

                        @Override
                        public void visitJumpInsn(int opcode, Label label) {
                            levels.merge(label, lockLevel, Integer::max);
                        }

                        @Override
                        public void visitLabel(Label label) {
                            lockLevel = levels.merge(label, lockLevel, Integer::max);
                        }

                        @Override
                        public void visitLineNumber(int line, Label start) {
                            lockLevels.merge(line, levels.get(start), Integer::max);
                        }
                    };
                } else {
                    return null;
                }
            }
        }, 0);

        Map.Entry<Integer, Integer> entry = lockLevels.floorEntry(ste.getLineNumber());

        return entry.getValue() > 0;
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}

From source file:org.gaul.modernizer_maven_plugin.Modernizer.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, final String methodName, final String methodDescriptor,
        final String methodSignature, String[] exceptions) {
    MethodVisitor base = super.visitMethod(access, methodName, methodDescriptor, methodSignature, exceptions);
    MethodVisitor origVisitor = new MethodVisitor(Opcodes.ASM5, base) {
    };/*  w  w  w .  ja  v a2 s  . c  om*/
    InstructionAdapter adapter = new InstructionAdapter(Opcodes.ASM5, origVisitor) {
        private int lineNumber = -1;

        @Override
        public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            visitFieldOrMethod(owner, name, desc);
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
            if (name.equals("<init>")) {
                name = "\"<init>\"";
            }
            visitFieldOrMethod(owner, name, desc);
        }

        private void visitFieldOrMethod(String owner, String name, String desc) {
            String token = owner + "." + name + ":" + desc;
            Violation violation = violations.get(token);
            checkToken(token, violation, name, lineNumber);
        }

        @Override
        public void visitLineNumber(int lineNumber, Label start) {
            this.lineNumber = lineNumber;
        }
    };
    return adapter;
}