Example usage for org.objectweb.asm.tree ClassNode ClassNode

List of usage examples for org.objectweb.asm.tree ClassNode ClassNode

Introduction

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

Prototype

public ClassNode() 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:org.spoofax.interpreter.adapter.asm.ASMFactory.java

License:LGPL

public IStrategoTerm parseFromStream(InputStream inputStream) throws IOException {
    ClassReader cr = new ClassReader(inputStream);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);// ww w .  j  ava  2s.co m
    return wrap(cn);
}

From source file:org.spoofax.interpreter.adapter.asm.TestASMLibrary.java

License:LGPL

private IStrategoTerm parse(InputStream stream) throws IOException {
    ClassNode cn = null;/* www .  j  a  va2  s.  co  m*/
    try {
        ClassReader cr = new ClassReader(stream);
        cn = new ClassNode();
        cr.accept(cn, ClassReader.SKIP_FRAMES); //EXPAND_FRAMES);
    } catch (RuntimeException e) {
        e.printStackTrace();
        return null;
    }
    return ASMFactory.genericWrap(cn);
}

From source file:org.springsource.loaded.test.SpringLoadedTests.java

License:Apache License

@SuppressWarnings("unchecked")
private MethodNode getMethod(byte[] classbytes, String methodName) {
    ClassReader super_cr = new ClassReader(classbytes);
    ClassNode cn = new ClassNode();
    super_cr.accept(cn, 0);
    List<MethodNode> methods = cn.methods;
    if (methods != null) {
        for (MethodNode mn : methods) {
            if (mn.name.equals(methodName)) {
                return mn;
            }/*w ww . jav  a  2s  . c  o m*/
        }
    }
    return null;
}

From source file:org.springsource.loaded.test.SpringLoadedTests.java

License:Apache License

@SuppressWarnings("unchecked")
private FieldNode getField(byte[] classbytes, String fieldName) {
    ClassReader super_cr = new ClassReader(classbytes);
    ClassNode cn = new ClassNode();
    super_cr.accept(cn, 0);
    List<FieldNode> fields = cn.fields;
    if (fields != null) {
        for (FieldNode fn : fields) {
            if (fn.name.equals(fieldName)) {
                return fn;
            }//  www . j  a  v  a 2s  .co m
        }
    }
    return null;
}

From source file:org.springsource.loaded.test.SpringLoadedTests.java

License:Apache License

protected ClassNode getClassNode(byte[] classdata) {
    ClassNode cn = new ClassNode();
    ClassReader cr = new ClassReader(classdata);
    cr.accept(cn, 0);// w w  w.java  2  s .c  o  m
    return cn;
}

From source file:org.springsource.loaded.test.SpringLoadedTests.java

License:Apache License

@SuppressWarnings("unchecked")
protected void checkLocalVariables(byte[] bytes, String methodNameAndDescriptor, String... expected) {
    ClassNode cn = new ClassNode();
    ClassReader cr = new ClassReader(bytes);
    cr.accept(cn, 0);/*from www.  j a  va  2  s.  c om*/

    boolean checked = false;
    List<MethodNode> methods = cn.methods;
    for (MethodNode mn : methods) {
        if (methodNameAndDescriptor.equals(mn.name + mn.desc)) {
            List<LocalVariableNode> localVariables = mn.localVariables;
            Assert.assertEquals(expected.length, localVariables.size());
            for (int i = 0; i < expected.length; i++) {
                StringTokenizer tokenizer = new StringTokenizer(expected[i], ":");
                String expectedName = tokenizer.nextToken();
                String expectedDesc = tokenizer.nextToken();
                LocalVariableNode localVariable = localVariables.get(i);
                Assert.assertEquals(i, localVariable.index);
                Assert.assertEquals(expectedName, localVariable.name);
                Assert.assertEquals(expectedDesc, localVariable.desc);
            }
            checked = true;
        }
    }
    if (!checked) {
        for (MethodNode mn : methods) {
            System.out.println(mn.name + mn.desc);
        }
        Assert.fail("Unable to find method " + methodNameAndDescriptor);
    }
}

From source file:org.springsource.loaded.test.SpringLoadedTests.java

License:Apache License

@SuppressWarnings("unchecked")
protected void checkAnnotations(byte[] bytes, String methodNameAndDescriptor, String... expected) {
    ClassNode cn = new ClassNode();
    ClassReader cr = new ClassReader(bytes);
    cr.accept(cn, 0);//from  w  w w.  j ava 2 s.co m
    if (expected == null) {
        expected = new String[0];
    }

    boolean checked = false;
    List<MethodNode> methods = cn.methods;
    for (MethodNode mn : methods) {
        if (methodNameAndDescriptor.equals(mn.name + mn.desc)) {
            List<AnnotationNode> annotations = mn.visibleAnnotations;
            if (annotations == null) {
                annotations = Collections.emptyList();
            }
            Assert.assertEquals(expected.length, annotations.size());
            for (int i = 0; i < expected.length; i++) {
                //               StringTokenizer tokenizer = new StringTokenizer(expected[i], ":");
                //               String expectedName = tokenizer.nextToken();
                //               String expectedDesc = tokenizer.nextToken();
                AnnotationNode annotation = annotations.get(i);
                Assert.assertEquals(expected[i], toString(annotation));
            }
            checked = true;
        }
    }
    if (!checked) {
        for (MethodNode mn : methods) {
            System.out.println(mn.name + mn.desc);
        }
        Assert.fail("Unable to find method " + methodNameAndDescriptor);
    }
}

From source file:org.springsource.loaded.TypeDiffComputer.java

License:Apache License

public static TypeDelta computeDifferences(byte[] oldbytes, byte[] newbytes) {
    ClassNode oldClassNode = new ClassNode();
    new ClassReader(oldbytes).accept(oldClassNode, 0);
    ClassNode newClassNode = new ClassNode();
    new ClassReader(newbytes).accept(newClassNode, 0);
    TypeDelta delta = computeDelta(oldClassNode, newClassNode);
    return delta;
}

From source file:org.summer.aop.ltw.AspectWeaver.java

License:Open Source License

@SuppressWarnings("unchecked")
private ClassNode mergeClassHierarchy(String className, List<String> interfaces) {
    ClassNode classNode = null;//from  w w  w.j  a  v  a  2s.  c  o  m
    try {
        ClassReader cr = new ClassReader(className);
        cr.accept(classNode = new ClassNode(), ClassReader.SKIP_DEBUG + ClassReader.SKIP_FRAMES);
        if (!classNode.superName.equals("java/lang/Object")) {
            boolean flag;
            ClassNode superClassNode = mergeClassHierarchy(classNode.superName, interfaces);
            for (Object superMethodNode : superClassNode.methods) {
                flag = false;
                MethodNode smn = (MethodNode) superMethodNode;
                for (Object methodNode : classNode.methods) {
                    MethodNode mn = (MethodNode) methodNode;
                    if (mn.name.equals(smn.name) && mn.desc.equals(smn.desc)) {
                        flag = true;
                        switch (mn.name) {
                        case "<clinit>":
                            mergeInitializingInstructions(superClassNode.name, smn.maxStack, smn.maxLocals,
                                    classNode.name, mn, smn.instructions.getFirst());
                            break;
                        case "<init>":
                            AbstractInsnNode insn = smn.instructions.getFirst();
                            while (!(insn instanceof MethodInsnNode)
                                    || !((MethodInsnNode) insn).owner.equals(superClassNode.superName)
                                    || ((MethodInsnNode) insn).getOpcode() != INVOKESPECIAL
                                    || !((MethodInsnNode) insn).name.equals("<init>")) {
                                insn = insn.getNext();
                            }
                            mergeInitializingInstructions(superClassNode.name, smn.maxStack, smn.maxLocals,
                                    classNode.name, mn, insn.getNext());
                            break;
                        default:
                            break;
                        }
                        break;
                    }
                }
                if (!flag) {
                    reviseMethodNode(superClassNode, smn, classNode.name);
                    classNode.methods.add(smn);
                }
            }
        }
        for (Object inter : classNode.interfaces) {
            if (!interfaces.contains(inter))
                interfaces.add((String) inter);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return classNode;
}

From source file:org.tbot.core.bot.loader.JarConstruct.java

License:Open Source License

/**
 * Loads all classes from the given JAR file and stores them into a HashMap for future use.
 *
 * @return True if it was successful, False if it was not.
 *//*from w  ww. ja  v  a 2 s  .  com*/
public boolean loadClasses() {
    short count = 0;

    try {
        //Startup
        p("---------------------------------------------------------------------");
        p("--------------------      Jar Loader     ----------------------------");
        p("Version: " + VERSION);
        p("File: " + BotInfo.JAR_PATH);

        p("JC Hash: " + getClass().hashCode());

        //Referencing the JAR file.
        JarFile jf = new JarFile(jarPath);

        //Print out the size of the JarFile
        p("JarFile Size = " + jf.size());
        p("-----------------------------------------------------------------");

        //Referencing the entries.
        Enumeration<? extends JarEntry> en = jf.entries();

        //Looping through the elements (the entries).
        while (en.hasMoreElements()) {

            //The entry to work with.
            JarEntry entry = en.nextElement();

            //Grabbing solely the class files
            if (entry.getName().endsWith(".class")) {

                //Count out the entries
                ++count;

                //Print out the entry
                p("Entry " + count + ") " + entry.getName());
                p(" -> Decompressed Size = " + entry.getSize() + " Bytes" + "\n");

                //ClassReader retrieves the bytes from a given entry.
                ClassReader cr = new ClassReader(jf.getInputStream(entry));

                //Creating a new ClassNode to act as a representative of a class.
                ClassNode cn = new ClassNode();

                //Delegating all method calls and data from ClassReader to ClassNode.
                //Think of it as data from 'cr' are being entrusted or put into 'cn' (such as the class bytes).
                cr.accept(cn, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

                //Put it into the local package's HashMap as a ClassNode.
                loadedClassNodes.put(cn.name, cn);
            }

        }

        System.out.println(count + " classes were loaded and stored!");
        p("-----------------------------------------------------------------");
        p("-----------------------------------------------------------------");

        Logger.printlnInfo("Load successful.");

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}