Example usage for org.objectweb.asm MethodVisitor MethodVisitor

List of usage examples for org.objectweb.asm MethodVisitor MethodVisitor

Introduction

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

Prototype

public MethodVisitor(final int api, final MethodVisitor methodVisitor) 

Source Link

Document

Constructs a new MethodVisitor .

Usage

From source file:net.yrom.tools.PredicateClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (attemptToVisitR)
        return null;
    return new MethodVisitor(Opcodes.ASM5, null) {

        @Override//w ww  .  j  av a 2 s .co m
        public void visitFieldInsn(int opcode, String owner, String fieldName, String fieldDesc) {

            if (attemptToVisitR || opcode != Opcodes.GETSTATIC || owner.startsWith("java/lang/")) {
                return;
            }

            attemptToVisitR = isRClass(owner);
        }
    };
}

From source file:net.yrom.tools.ShrinkRClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {

        @Override/*  www  .j  a v a 2s  .  c  om*/
        public void visitFieldInsn(int opcode, String owner, String fieldName, String fieldDesc) {
            if (opcode != Opcodes.GETSTATIC || owner.startsWith("java/lang/")) {
                // skip!
                this.mv.visitFieldInsn(opcode, owner, fieldName, fieldDesc);
                return;
            }
            String typeName = owner.substring(owner.lastIndexOf('/') + 1);
            String key = typeName + '.' + fieldName;
            if (rSymbols.containsKey(key)) {
                Integer value = rSymbols.get(key);
                if (value == null)
                    throw new UnsupportedOperationException("value of " + key + " is null!");
                if (logger.isEnabled(LogLevel.DEBUG)) {
                    logger.debug("replace {}.{} to 0x{}", owner, fieldName, Integer.toHexString(value));
                }
                pushInt(this.mv, value);
            } else if (owner.endsWith("/R$styleable")) { // replace all */R$styleable ref!
                this.mv.visitFieldInsn(opcode, RSymbols.R_STYLEABLES_CLASS_NAME, fieldName, fieldDesc);
            } else {
                this.mv.visitFieldInsn(opcode, owner, fieldName, fieldDesc);
            }
        }
    };
}

From source file:org.apache.asterix.runtime.evaluators.staticcodegen.EvaluatorVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    if (!METHOD_IDENTIFIER.equals(new MethodIdentifier(name, desc, signature))) {
        return mv;
    }// www.j ava  2s  .co  m
    if (mv != null) {
        return new MethodVisitor(Opcodes.ASM5, mv) {
            private FieldInsnNode fieldAccessNode = null;
            private List<AbstractInsnNode> instructionsAfterFieldAccess = new ArrayList<>();

            @Override
            public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                fieldAccessNode = new FieldInsnNode(opcode, owner, name, desc);
                instructionsAfterFieldAccess.clear();
            }

            @Override
            public void visitIincInsn(int var, int increment) {
                if (fieldAccessNode != null) {
                    instructionsAfterFieldAccess.add(new IincInsnNode(var, increment));
                }
                super.visitIincInsn(var, increment);
            }

            @Override
            public void visitInsn(int opcode) {
                if (fieldAccessNode != null) {
                    instructionsAfterFieldAccess.add(new InsnNode(opcode));
                }
                super.visitInsn(opcode);
            }

            @Override
            public void visitIntInsn(int opcode, int operand) {
                if (fieldAccessNode != null) {
                    instructionsAfterFieldAccess.add(new IntInsnNode(opcode, operand));
                }
                super.visitIntInsn(opcode, operand);
            }

            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                mv.visitMethodInsn(opcode, owner, name, desc, itf);
                if (fieldAccessNode == null
                        || !METHOD_IDENTIFIER.equals(new MethodIdentifier(name, desc, signature))) {
                    return;
                }
                // Loads "this".
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                // Replays the field access instruction.
                fieldAccessNode.accept(mv);

                // Replays other instruction between the field access and the evaluator call.
                for (AbstractInsnNode instruction : instructionsAfterFieldAccess) {
                    instruction.accept(mv);
                }

                // Loads the result IPointable.
                mv.visitVarInsn(Opcodes.ALOAD, 2);

                // Invokes the null check method.
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, TYPECHECK_CLASS, IS_NULL, TYPECHECK_METHOD_DESC,
                        false);
                Label notNull = new Label();
                // Adds the if branch.
                mv.visitJumpInsn(Opcodes.IFEQ, notNull);
                mv.visitInsn(Opcodes.RETURN);
                mv.visitLabel(notNull);
                mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            }
        };
    }
    return null;
}

From source file:org.apache.asterix.runtime.evaluators.staticcodegen.GatherEvaluatorCreationVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!name.equals(METHOD_NAME)) {
        return null;
    }/*from  w  w w.  ja va2 s  . co  m*/
    return new MethodVisitor(Opcodes.ASM5, null) {

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            if (opcode != Opcodes.INVOKESPECIAL) {
                return;
            }
            if (owner.startsWith(ownerPrefix)) {
                createdEvaluatorClassNames.add(owner);
            }
        }
    };

}

From source file:org.apache.asterix.runtime.evaluators.staticcodegen.GatherEvaluatorFactoryCreationVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!name.equals(METHOD_NAME)) {
        return null;
    }//from   www  . jav  a 2 s  .  c o m
    return new MethodVisitor(Opcodes.ASM5, null) {

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            if (opcode != Opcodes.INVOKESPECIAL) {
                return;
            }
            if (owner.startsWith(ownerPrefix)) {
                createdEvaluatorFactoryClassNames.add(owner);
            }
        }
    };
}

From source file:org.apache.asterix.runtime.evaluators.staticcodegen.RenameClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = cv.visitMethod(access, name, applyMapping(desc), applyMapping(signature), exceptions);
    if (mv != null) {
        return new MethodVisitor(Opcodes.ASM5, mv) {

            @Override//w w  w  . j  av  a 2s  .  com
            public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                mv.visitFieldInsn(opcode, applyMapping(owner), applyMapping(name), applyMapping(desc));
            }

            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                mv.visitMethodInsn(opcode, applyMapping(owner), name, applyMapping(desc), itf);
            }

            @Override
            public void visitLocalVariable(String name, String desc, String signature, Label start, Label end,
                    int index) {
                mv.visitLocalVariable(name, applyMapping(desc), applyMapping(signature), start, end, index);
            }

            @Override
            public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
                if (local != null) {
                    for (int index = 0; index < local.length; ++index) {
                        if (local[index] instanceof String) {
                            local[index] = applyMapping((String) local[index]);
                        }
                    }
                }
                mv.visitFrame(type, nLocal, local, nStack, stack);
            }

            @Override
            public void visitTypeInsn(int opcode, String type) {
                mv.visitTypeInsn(opcode, applyMapping(type));
            }

        };
    }
    return null;
}

From source file:org.copperengine.core.instrument.ScottyClassAdapter.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (interruptableMethods.contains(name + desc) && ((access & ACC_ABSTRACT) == 0)) {
        logger.debug("Transforming {}.{}{}", new Object[] { currentClassName, name, desc });
        MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);

        String classDesc = Type.getObjectType(currentClassName).getDescriptor();
        BuildStackInfoAdapter stackInfo = new BuildStackInfoAdapter(classDesc, (access & ACC_STATIC) > 0, name,
                desc, signature);/*from  w  w  w.  j a va 2 s.  c om*/
        final ScottyMethodAdapter scotty = new ScottyMethodAdapter(mv, currentClassName, interruptableMethods,
                stackInfo, name, access, desc);
        MethodVisitor collectMethodInfo = new MethodVisitor(Opcodes.ASM4, stackInfo) {
            @Override
            public void visitEnd() {
                super.visitEnd();
                methodInfos.add(scotty.getMethodInfo());
            }
        };
        stackInfo.setMethodVisitor(scotty);
        // ScottyMethodAdapter stackInfo = new ScottyMethodAdapter(mv, currentClassName, interruptableMethods);
        return collectMethodInfo;
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:org.copperengine.core.instrument.ScottyFindInterruptableMethodsVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    method = name + desc;/* w  ww  .j a  v a 2s .co m*/
    if (exceptions != null && exceptions.length > 0) {
        for (String e : exceptions) {
            if ("org/copperengine/core/Interrupt".equals(e)) {
                interruptableMethods.add(method);
            }
        }
    }
    return new MethodVisitor(ASM4, super.visitMethod(access, name, desc, signature, exceptions)) {
        @Override
        public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
            if ("org/copperengine/core/Interrupt".equals(type)) {
                throw new RuntimeException("Interrupt must not be handled!");
            }
            super.visitTryCatchBlock(start, end, handler, type);
        }
    };
}

From source file:org.eclipse.pde.api.tools.internal.model.TypeStructureBuilder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    String[] names = null;//w w  w  .  j  a va 2  s  .c om
    int laccess = access;
    if ((laccess & Opcodes.ACC_DEPRECATED) != 0) {
        laccess &= ~Opcodes.ACC_DEPRECATED;
        laccess |= ClassFileConstants.AccDeprecated;
    }
    if (exceptions != null && exceptions.length > 0) {
        names = new String[exceptions.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = exceptions[i].replace('/', '.');
        }
    }
    final ApiMethod method = fType.addMethod(name, desc, signature, laccess, names);
    return new MethodVisitor(Opcodes.ASM5, super.visitMethod(laccess, name, desc, signature, exceptions)) {
        @Override
        public AnnotationVisitor visitAnnotation(String sig, boolean visible) {
            if (visible && "Ljava/lang/invoke/MethodHandle$PolymorphicSignature;".equals(sig)) { //$NON-NLS-1$
                method.isPolymorphic();
            }
            return super.visitAnnotation(sig, visible);
        }

        @Override
        public AnnotationVisitor visitAnnotationDefault() {
            return new AnnotationDefaultVisitor(method);
        }
    };
}

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) {
    };/*from   w  w  w.jav  a 2  s. c  o  m*/
    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;
}