Search in sources :

Example 21 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project Minechem by iopleke.

the class MinechemTransformer method injectBytes.

private byte[] injectBytes(Method method, InstructionNode instructionNode, byte[] data) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(data);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    MethodNode methodNode = getMethodByName(classNode, method);
    AbstractInsnNode pos = findInstructionNode(instructionNode, methodNode);
    if (instructionNode.replace) {
        methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
        methodNode.instructions.remove(pos);
    } else {
        methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 22 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project enumerable by hraberg.

the class LambdaExpressionTrees method findMethodNode.

@SuppressWarnings("unchecked")
static MethodNode findMethodNode(Method method) throws IOException {
    String className = method.getDeclaringClass().getName();
    ClassReader cr;
    if (InMemoryCompiler.bytesByClassName.containsKey(className))
        cr = new ClassReader(InMemoryCompiler.bytesByClassName.get(className));
    else
        cr = new ClassReader(className);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    String descriptor = getMethodDescriptor(method);
    for (MethodNode mn : (List<MethodNode>) cn.methods) {
        if (method.getName().equals(mn.name) && descriptor.equals(mn.desc))
            return mn;
    }
    throw new IllegalStateException("Cannot find method which does exist");
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

Example 23 with ClassNode

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

the class ASMClassNodeAdapter method get.

public static ASMClassNodeAdapter get(final InstrumentContext pluginContext, final ClassLoader classLoader, final String classInternalName, final boolean skipCode) {
    if (pluginContext == null || classInternalName == null) {
        throw new IllegalArgumentException("plugin context or class name must not be null.");
    }
    InputStream in = null;
    try {
        in = pluginContext.getResourceAsStream(classLoader, classInternalName + ".class");
        if (in != null) {
            final ClassReader classReader = new ClassReader(in);
            final ClassNode classNode = new ClassNode();
            if (skipCode) {
                classReader.accept(classNode, ClassReader.SKIP_CODE);
            } else {
                classReader.accept(classNode, 0);
            }
            return new ASMClassNodeAdapter(pluginContext, classLoader, classNode, skipCode);
        }
    } catch (IOException ignored) {
    // not found class.
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    return null;
}
Also used : InnerClassNode(org.objectweb.asm.tree.InnerClassNode) ClassNode(org.objectweb.asm.tree.ClassNode) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException)

Example 24 with ClassNode

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

the class ASMEngine method getClass.

@Override
public InstrumentClass getClass(InstrumentContext instrumentContext, ClassLoader classLoader, String className, byte[] classFileBuffer) throws NotFoundInstrumentException {
    if (className == null) {
        throw new NullPointerException("class name must not be null.");
    }
    try {
        if (classFileBuffer == null) {
            ASMClassNodeAdapter classNode = ASMClassNodeAdapter.get(instrumentContext, classLoader, JavaAssistUtils.javaNameToJvmName(className));
            if (classNode == null) {
                return null;
            }
            ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
            return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
        }
        // Use ASM tree api.
        final ClassReader classReader = new ClassReader(classFileBuffer);
        final ClassNode classNode = new ClassNode();
        classReader.accept(classNode, 0);
        ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
        return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
    } catch (Exception e) {
        throw new NotFoundInstrumentException(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException) ClassReader(org.objectweb.asm.ClassReader) ApiMetaDataService(com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)

Example 25 with ClassNode

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

the class ASMMethodNodeAdapterRenameTest method rename.

@Test
public void rename() throws Exception {
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    classLoader.setTrace(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                if (!adapter.isConstructor()) {
                    adapter.rename(adapter.getName() + "_rename");
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum_rename", int.class);
    method.invoke(clazz.newInstance(), 10);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)117 ClassReader (org.objectweb.asm.ClassReader)58 MethodNode (org.objectweb.asm.tree.MethodNode)42 ClassWriter (org.objectweb.asm.ClassWriter)28 IOException (java.io.IOException)14 FieldNode (org.objectweb.asm.tree.FieldNode)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Test (org.junit.Test)10 ZipEntry (java.util.zip.ZipEntry)8 Method (java.lang.reflect.Method)7 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)6 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)6 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 Label (org.objectweb.asm.Label)5