Search in sources :

Example 81 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class MethodInterfaceTest method addInterceptor.

@Test
public void addInterceptor() throws Exception {
    final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
    final String targetClassName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterfaceClass";
    logger.debug("Add interceptor interface={}, class={}", targetInterfaceName, targetClassName);
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new SimpleInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(SimpleInterceptor.class);
    final List<String> methodNameList = Arrays.asList("currentTimeMillis", "foo");
    TestClassLoader classLoader = new TestClassLoader();
    classLoader.addTargetClassName(targetClassName);
    classLoader.addTargetClassName(targetInterfaceName);
    classLoader.setCallbackHandler(new CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                logger.debug("Handle class={}, method={}", classNode.name, methodNode.name);
                if (methodNode.name.equals("<clinit>")) {
                    continue;
                }
                ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
                    continue;
                }
                if (!methodNameList.contains(methodNode.name)) {
                    continue;
                }
                methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
                logger.debug("Add before interceptor in method={}", methodNode.name);
                methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
                logger.debug("Add after interceptor in method={}", methodNode.name);
            }
        }
    });
    // static method
    Assert.assertFalse(SimpleInterceptor.before);
    logger.debug("Interface static method");
    Class<?> clazz = classLoader.loadClass(targetInterfaceName);
    Method method = clazz.getDeclaredMethod("currentTimeMillis");
    method.invoke(null);
    assertTrue(SimpleInterceptor.before);
    // reset
    SimpleInterceptor.before = false;
    // default method
    Assert.assertFalse(SimpleInterceptor.before);
    logger.debug("Interface default method");
    clazz = classLoader.loadClass(targetClassName);
    method = clazz.getDeclaredMethod("bar");
    method.invoke(clazz.newInstance());
    assertTrue(SimpleInterceptor.before);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ASMMethodNodeAdapter(com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter) Method(java.lang.reflect.Method) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory) InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) MethodNode(org.objectweb.asm.tree.MethodNode) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 82 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMClassNodeAdapter method addDelegatorMethod.

public ASMMethodNodeAdapter addDelegatorMethod(final ASMMethodNodeAdapter superMethodNode) {
    Objects.requireNonNull(superMethodNode, "superMethodNode");
    final String[] exceptions = getSuperMethodExceptions(superMethodNode);
    final MethodNode rawMethodNode = new MethodNode(superMethodNode.getAccess(), superMethodNode.getName(), superMethodNode.getDesc(), superMethodNode.getSignature(), exceptions);
    final ASMMethodNodeAdapter methodNode = new ASMMethodNodeAdapter(getInternalName(), rawMethodNode);
    methodNode.addDelegator(superMethodNode.getDeclaringClassInternalName());
    addMethodNode0(methodNode.getMethodNode());
    return methodNode;
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 83 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMClassNodeAdapter method addSetterMethod.

public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    Objects.requireNonNull(methodName, "methodName");
    Objects.requireNonNull(fieldNode, "fieldNode");
    // void is V.
    final String desc = "(" + fieldNode.getDesc() + ")V";
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    final InsnList instructions = getInsnList(methodNode);
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type type = Type.getType(fieldNode.getDesc());
    // put field.
    instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
    instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return.
    instructions.add(new InsnNode(Opcodes.RETURN));
    addMethodNode0(methodNode);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 84 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project Engine by VoltzEngine-Project.

the class InjectionTemplate method init.

// Called late to avoid loading class data in the constructor
private boolean init() {
    if (!init) {
        init = true;
        try {
            final ClassNode cnode = ASMHelper.createClassNode(((LaunchClassLoader) InjectionTemplate.class.getClassLoader()).getClassBytes(className.replace('/', '.')));
            for (MethodNode method : cnode.methods) {
                this.methodImplementations.add(method);
                method.desc = new ObfMapping(cnode.name, method.name, method.desc).toRuntime().s_desc;
            }
        } catch (IOException e) {
            // TODO error out in dev mode
            // TODO make notation to users that this injector failed
            e.printStackTrace();
            failedToLoadClass = true;
        }
    }
    return failedToLoadClass;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ObfMapping(com.builtbroken.mc.lib.asm.ObfMapping) MethodNode(org.objectweb.asm.tree.MethodNode) IOException(java.io.IOException)

Example 85 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project atlas by alibaba.

the class TBIncrementalChangeVisitor method visitMethod.

/**
 * Generates new delegates for all 'patchable' methods in the visited class. Delegates
 * are static methods that do the same thing the visited code does, but from outside the class.
 * For instance methods, the instance is passed as the first argument. Note that:
 * <ul>
 *   <li>We ignore the class constructor as we don't support it right now</li>
 *   <li>We skip abstract methods.</li>
 *   <li>For constructors split the method body into super arguments and the rest of
 *   the method body, see {@link ConstructorBuilder}</li>
 * </ul>
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (instantRunDisabled || !isAccessCompatibleWithInstantRun(access)) {
        // Nothing to generate.
        return null;
    }
    if (name.equals(ByteCodeUtils.CLASS_INITIALIZER)) {
        // we skip the class init as it can reset static fields which we don't support right now
        return null;
    }
    boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
    String newDesc = computeOverrideMethodDesc(desc, isStatic);
    if (DEBUG) {
        System.out.println(">>> Visiting method " + visitedClassName + ":" + name + ":" + desc);
        if (exceptions != null) {
            for (String exception : exceptions) {
                System.out.println("> Exception thrown : " + exception);
            }
        }
    }
    if (DEBUG) {
        System.out.println("New Desc is " + newDesc + ":" + isStatic);
    }
    // Do not carry on any access flags from the original method. For example synchronized
    // on the original method would translate into a static synchronized method here.
    access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
    MethodNode method = getMethodByNameInClass(name, desc, classNode);
    if (name.equals(ByteCodeUtils.CONSTRUCTOR)) {
        Constructor constructor = ConstructorBuilder.build(visitedClassName, method);
        MethodVisitor mv = createMethodAdapter(access, constructor.args.name, constructor.args.desc, constructor.args.desc, constructor.args.signature, exceptions, isStatic, true);
        constructor.args.accept(mv);
        mv = createMethodAdapter(access, constructor.body.name, constructor.body.desc, newDesc, constructor.body.signature, exceptions, isStatic, true);
        constructor.body.accept(mv);
        // Remember our created methods so we can generated the access$dispatch for them.
        addedMethods.add(constructor.args);
        addedMethods.add(constructor.body);
        return null;
    } else {
        String newName = isStatic ? computeOverrideMethodName(name, desc) : name;
        return createMethodAdapter(access, newName, newDesc, newDesc, signature, exceptions, isStatic, false);
    }
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17