Search in sources :

Example 91 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)

Example 92 with ClassNode

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

the class ClassInfoLoader method createClassInfoLoader.

public static ClassInfoLoader createClassInfoLoader(Iterable<ClassDefinition> classDefinitions, ClassLoader classLoader) {
    ImmutableMap.Builder<ParameterizedType, ClassNode> classNodes = ImmutableMap.builder();
    for (ClassDefinition classDefinition : classDefinitions) {
        ClassNode classNode = new ClassNode();
        classDefinition.visit(classNode);
        classNodes.put(classDefinition.getType(), classNode);
    }
    return new ClassInfoLoader(classNodes.build(), ImmutableMap.of(), classLoader, true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 93 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 94 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project retrolambda by orfjackal.

the class LambdaClassesTest method readClass.

private static ClassNode readClass(String name) throws IOException {
    ClassReader cr = new ClassReader(name);
    ClassNode cls = new ClassNode();
    cr.accept(cls, ClassReader.SKIP_CODE);
    return cls;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader)

Example 95 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project neo4j by neo4j.

the class ByteCodeVerifier method classNode.

private static ClassNode classNode(ByteBuffer bytecode) {
    byte[] bytes;
    if (bytecode.hasArray()) {
        bytes = bytecode.array();
    } else {
        bytes = new byte[bytecode.limit()];
        bytecode.get(bytes);
    }
    ClassNode classNode = new ClassNode();
    new ClassReader(bytes).accept(new CheckClassAdapter(classNode, false), SKIP_DEBUG);
    return classNode;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)323 ClassReader (org.objectweb.asm.ClassReader)148 MethodNode (org.objectweb.asm.tree.MethodNode)120 ClassWriter (org.objectweb.asm.ClassWriter)70 IOException (java.io.IOException)44 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)37 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)34 InsnList (org.objectweb.asm.tree.InsnList)29 FieldNode (org.objectweb.asm.tree.FieldNode)28 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)26 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)26 Label (org.objectweb.asm.Label)21 InputStream (java.io.InputStream)20 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)18 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)18 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)17 InsnNode (org.objectweb.asm.tree.InsnNode)17 Method (java.lang.reflect.Method)16 List (java.util.List)16