Search in sources :

Example 36 with ClassNode

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

the class ASMClassNodeLoader method get.

public static MethodNode get(final String classInternalName, final String methodName) throws Exception {
    ClassNode classNode = get(classInternalName);
    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode methodNode : methodNodes) {
        if (methodNode.name.equals(methodName)) {
            return methodNode;
        }
    }
    return null;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode)

Example 37 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project CodeChickenLib by Chicken-Bones.

the class ClassHeirachyManager method declareASM.

private static SuperCache declareASM(byte[] bytes) {
    ClassNode node = ASMHelper.createClassNode(bytes);
    String name = toKey(node.name);
    SuperCache cache = getOrCreateCache(name);
    cache.superclass = toKey(node.superName.replace('/', '.'));
    cache.add(cache.superclass);
    for (String iclass : node.interfaces) cache.add(toKey(iclass.replace('/', '.')));
    return cache;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

Example 38 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 39 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project drill by apache.

the class AsmUtil method classFromBytes.

/**
   * Create a ClassNode from bytecode.
   *
   * @param classBytes the bytecode
   * @param asmReaderFlags flags for ASM; see {@link org.objectweb.asm.ClassReader#accept(org.objectweb.asm.ClassVisitor, int)}
   * @return the ClassNode
   */
public static ClassNode classFromBytes(final byte[] classBytes, final int asmReaderFlags) {
    final ClassNode classNode = new ClassNode(CompilationConfig.ASM_API_VERSION);
    final ClassReader classReader = new ClassReader(classBytes);
    classReader.accept(classNode, asmReaderFlags);
    return classNode;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader)

Example 40 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project presto by prestodb.

the class ClassInfoLoader method readClassInfoQuick.

private ClassInfo readClassInfoQuick(ParameterizedType type) {
    // check for user supplied class node
    ClassNode classNode = classNodes.get(type);
    if (classNode != null) {
        return new ClassInfo(this, classNode);
    }
    // check for user supplied byte code
    ClassReader classReader;
    byte[] bytecode = bytecodes.get(type);
    if (bytecode != null) {
        classReader = new ClassReader(bytecode);
    } else {
        // load class file from class loader
        String classFileName = type.getClassName() + ".class";
        try (InputStream is = classLoader.getResourceAsStream(classFileName)) {
            classReader = new ClassReader(is);
        } catch (IOException e) {
            // check if class is already loaded
            try {
                Class<?> aClass = classLoader.loadClass(type.getJavaClassName());
                return new ClassInfo(this, aClass);
            } catch (ClassNotFoundException e1) {
                throw new RuntimeException("Class not found " + type, e);
            }
        }
    }
    if (loadMethodNodes) {
        // slower version that loads all operations
        classNode = new ClassNode();
        classReader.accept(new CheckClassAdapter(classNode, false), ClassReader.SKIP_DEBUG);
        return new ClassInfo(this, classNode);
    } else {
        // optimized version
        int header = classReader.header;
        int access = classReader.readUnsignedShort(header);
        char[] buf = new char[2048];
        // read super class name
        int superClassIndex = classReader.getItem(classReader.readUnsignedShort(header + 4));
        ParameterizedType superClass;
        if (superClassIndex == 0) {
            superClass = null;
        } else {
            superClass = typeFromPathName(classReader.readUTF8(superClassIndex, buf));
        }
        // read each interface name
        int interfaceCount = classReader.readUnsignedShort(header + 6);
        ImmutableList.Builder<ParameterizedType> interfaces = ImmutableList.builder();
        header += 8;
        for (int i = 0; i < interfaceCount; ++i) {
            interfaces.add(typeFromPathName(classReader.readClass(header, buf)));
            header += 2;
        }
        return new ClassInfo(this, type, access, superClass, interfaces.build(), null);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InputStream(java.io.InputStream) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

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