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:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

public PortableTypeGenerator(InputStream in) throws IOException {
    ClassReader reader = new ClassReader(in);
    cn = new ClassNode();
    reader.accept(cn, 0);// w w w  .j  a  va  2 s . c o m
}

From source file:com.seovic.pof.util.AsmUtils.java

License:Apache License

public static Set<String> getVisibleAnnotations(InputStream in) throws IOException {
    ClassReader reader = new ClassReader(in);
    ClassNode cn = new ClassNode();
    reader.accept(cn, ClassReader.SKIP_FRAMES);

    HashSet<String> annotations = new HashSet<String>();
    if (cn.visibleAnnotations != null) {
        for (AnnotationNode an : (List<AnnotationNode>) cn.visibleAnnotations) {
            annotations.add(an.desc);//from   ww  w.java 2  s  . c o  m
        }
    }
    return annotations;
}

From source file:com.speed.ob.api.ClassStore.java

License:Open Source License

public void init(File[] classFiles, boolean recursive) throws IOException {
    for (File file : classFiles) {
        if (file.getName().endsWith(".class")) {
            try {
                FileInputStream in = new FileInputStream(file);
                ClassReader reader = new ClassReader(in);
                ClassNode cn = new ClassNode();
                reader.accept(cn, ClassReader.EXPAND_FRAMES);
                store.put(cn.name, cn);//from ww w.  ja  va2s.  c  o m
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else if (recursive && file.isDirectory()) {
            init(file.listFiles(), true);
        }
    }
}

From source file:com.speed.ob.api.ClassStore.java

License:Open Source License

public void init(JarInputStream jarIn, File output, File in) throws IOException {
    ZipEntry entry;//from   w ww  .  j  a v a 2s  .com
    JarOutputStream out = new JarOutputStream(new FileOutputStream(new File(output, in.getName())));
    while ((entry = jarIn.getNextEntry()) != null) {
        byte[] data = IOUtils.toByteArray(jarIn);
        if (entry.getName().endsWith(".class")) {
            ClassReader reader = new ClassReader(data);
            ClassNode cn = new ClassNode();
            reader.accept(cn, ClassReader.EXPAND_FRAMES);
            store.put(cn.name, cn);
        } else if (!entry.isDirectory()) {
            Logger.getLogger(getClass().getName()).finer("Storing " + entry.getName() + " in output file");
            JarEntry je = new JarEntry(entry.getName());
            out.putNextEntry(je);
            out.write(data);
            out.closeEntry();
        }
    }
    out.close();
}

From source file:com.speed.ob.transforms.ClassNameTransform.java

License:Open Source License

public void run(ClassStore store, Config config) {
    //generate map of old names -> new names
    for (ClassNode node : store.nodes()) {
        LOGGER.fine("Processing class: " + node.name);
        boolean hasMain = false;
        if (excludeMains) {
            for (MethodNode mn : (List<MethodNode>) node.methods) {
                if (mn.name.equals("main") && mn.desc.equals("([Ljava/lang/String;)V")) {
                    LOGGER.fine("Not renaming " + node.name + ", has a main method");
                    //continue outer;
                    hasMain = true;/*from  w  w  w  .ja  va  2 s.  co  m*/
                    break;
                }
            }
        }
        if (excludedClasses != null && excludedClasses.contains(node.name)) {
            LOGGER.fine("Not renaming " + node.name + ", is excluded");
            continue;
        }
        String newName;
        int ind = node.name.lastIndexOf('/');
        if (excludeMains && hasMain) {
            if (ind > -1) {
                newName = node.name.substring(ind + 1);
            } else {
                newName = node.name;
            }
        } else {
            if (keepPackages && ind > -1) {
                newName = node.name.substring(0, ind) + '/' + nameGenerator.next();
            } else {
                newName = nameGenerator.next();
            }
        }

        LOGGER.finer("Renaming " + node.name + " to " + newName);
        namesMap.put(node.name, newName);
    }
    Iterator<ClassNode> iterator = store.nodes().iterator();
    HashMap<String, ClassNode> newClasses = new HashMap<>();
    while (iterator.hasNext()) {
        ClassNode cn = iterator.next();
        ClassNode node = new ClassNode();
        RemappingClassAdapter remapping = new RemappingClassAdapter(node, new ClassRemapper());
        cn.accept(remapping);
        newClasses.put(node.name, node);
    }
    store.set(newClasses);
}

From source file:com.spotify.missinglink.ClassLoader.java

License:Apache License

private static ClassNode readClassNode(InputStream in) throws IOException {
    final ClassNode classNode = new ClassNode();
    ClassReader reader = new ClassReader(in);
    reader.accept(classNode, 0);/*  w w w  . java 2 s  .c  o  m*/
    return classNode;
}

From source file:com.sun.fortress.repository.ForeignJava.java

License:Open Source License

static ClassNode findClass(String s) throws IOException {
    ClassReader cr = new ClassReader(s);
    ClassNode cn = new ClassNode();
    cr.accept(cn, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
    return cn;//w  ww  .ja v a  2  s  . co  m
}

From source file:com.triage.bytecodemaster.BaseWeaverTest.java

protected ClassNode loadLocalClass(String className) throws Exception {
    //Class groovyClass = makeGroovyScriptClass("int z=0;z=x+y");
    ClassNode classNode = new ClassNode();

    ClassReader classReader = new ClassReader(className);
    classReader.accept(classNode, 0);//from  ww w .  ja  va  2  s  . c  o m
    return classNode;
}

From source file:com.triage.bytecodemaster.BaseWeaverTest.java

protected ClassNode loadGroovyTestClassAsBytecode(String classSource) throws Exception {
    ClassNode classNode = new ClassNode();
    String scriptName = "ScriptTestClass.groovy";

    Class groovyClass = groovyClassLoader.parseClass(classSource, scriptName);

    String className = groovyClass.getName() + ".class";
    byte[] classBytes = groovyClassLoader.getClassBytes(className);
    ClassReader classReader = new ClassReader(classBytes);
    classReader.accept(classNode, 0);/*from  w w w .jav a 2 s .com*/
    return classNode;
}

From source file:com.triage.bytecodemaster.GroovyShardTransformer.java

protected byte[] transformClass(String tapScript, String className, ClassLoader cl, byte[] classFileBuffer)
        throws Exception {
    Timer timer = new Timer();
    timer.start();//from w w  w .  java  2 s  .c  o  m
    //so that we can reference classes available in the caller when we compile the script

    //MAKES A KEY ASSUMPTION: that all classloaders we will have a path back to the system
    //classloader, and thus will have our groovy stuff in them!
    //if this isnt the case, we may want to have a dual-parent classloader where
    //we can search both the target classloader and the system
    CachingGroovyClassLoader groovyClassLoader = new CachingGroovyClassLoader(cl);

    //load the script in ASM
    ClassNode shimClassNode = new ClassNode();
    String scriptName = className + "-Tap.groovy";

    timer.mark("startParse");
    Class groovyClass = groovyClassLoader.parseClass(tapScript, scriptName);
    timer.mark("endParse");

    String generatedClassName = groovyClass.getName() + ".class";
    byte[] classBytes = groovyClassLoader.getClassBytes(generatedClassName);
    timer.mark("getClassBytes");
    ClassReader shimClassReader = new ClassReader(classBytes);
    shimClassReader.accept(shimClassNode, 0);
    timer.mark("readShimClass");

    ClassNode targetClassNode = new ClassNode();
    ClassReader targetClassReader = new ClassReader(classFileBuffer);
    targetClassReader.accept(targetClassNode, 0);
    timer.mark("readTargetClass");
    //copy instructions        
    //TODO: this is just a POC-- of course all this hardcoded stuff needs
    //to be replaced with real code
    MethodNode targetMethod = null;
    MethodNode sourceMethod = null;
    InsnList instructionsToInject = null;

    if (className.contains("TestFriend")) {
        targetMethod = findMethod(targetClassNode, "whatDoIThinkAbout");
        sourceMethod = findMethod(shimClassNode, "beforeWhatDoIThinkAbout");
        instructionsToInject = sourceMethod.instructions;
    } else if (className.contains("Calculator")) {
        targetMethod = findMethod(targetClassNode, "add");
        sourceMethod = findMethod(shimClassNode, "beforeAdd");

        //HACK: in the calculator script, we do not want a premature
        //return, so lets remove RETURNs from the source.
        //that DOESNT work in gneeral because sometimes we do want a return
        //but this will just see if removing the returns works
        instructionsToInject = sourceMethod.instructions;
        ListIterator li = instructionsToInject.iterator();
        while (li.hasNext()) {
            AbstractInsnNode node = (AbstractInsnNode) li.next();

            if (isReturnOpCode(node.getOpcode())) {
                li.remove();
            }
        }
    }
    timer.mark("gotInstructionsToInject");
    System.out.println("Transforming Target Method:");
    printMethodNode(targetMethod);
    System.out.println("Transforming Source Method:");
    printMethodNode(sourceMethod);

    //insert source instructions in target
    targetMethod.instructions.insert(sourceMethod.instructions);
    timer.mark("injectedInstructions");

    //write a class 

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    targetClassNode.accept(cw);
    timer.mark("finishedWrite");
    System.out.println("Successfully transformed class" + className);
    timer.stop();
    System.out.println("Timings:" + timer);
    return cw.toByteArray();

}