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:TestMapper.java

License:Apache License

@SuppressWarnings("rawtypes")
public final static Map<String, Object> checkInstrument(String classpath, String className) {
    Map<String, Object> map = new HashMap<>();
    try {/* www. j  a  v  a2 s  .c  om*/
        System.setProperty("current-project-name", "checkInstrument");
        System.setProperty("current-project-path", classpath);
        String targetDir = classpath;

        File f = new File(classpath);
        URLClassLoader ucl = new URLClassLoader(new URL[] { f.toURI().toURL() },
                Thread.currentThread().getContextClassLoader());

        String classFileName = className.replace('.', '/') + ".class";
        // set Domain class file
        File file = new File(targetDir + "/" + classFileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = new byte[in.available()];
        in.read(bytes);
        in.close();

        // --------------------Mapper start-------------------------------
        ClassReader cr = new ClassReader(bytes);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);
        DomainMojoHelper.setLoader(ucl);
        MapperToolProcessor proc = new MapperToolProcessor();
        ProcessResult result = proc.process(file, bytes, cn);
        proc.getMapperRegister().endProcess();

        // mapper check
        byte[] mapperClassBytes = result.newBytes;
        ClassNode mapper = result.node;
        String mapperClassName = mapper.name.replaceAll("\\/", "\\.");

        System.out.println("------------ Dump bytecode ------------");

        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(bo);
        GemliteHelper.dumpBytecode(mapper, pw);
        pw.flush();
        byte[] dumpBytes = bo.toByteArray();
        map.put("dumpBytes", new String(dumpBytes));
        bo.reset();

        System.out.println("------------ Dump bytecode end ------------");
        GemliteHelper.checkAsmBytes(mapperClassBytes, pw);
        byte[] verifyBytes = bo.toByteArray();
        map.put("verifyBytes", new String(verifyBytes));
        // Test gen mapper class
        ByteArrayClassLoader ba = new ByteArrayClassLoader(mapperClassBytes, ucl);
        Class cls = ba.loadClass(mapperClassName);
        map.put("cls", cls);
        System.out.println("Class -> " + cls);

        // Test gen reg class
        ClassNode reg = proc.getMapperRegister().getReg();
        String regClassName = reg.name.replaceAll("\\/", "\\.");
        bo.reset();
        GemliteHelper.dumpBytecode(reg, pw);
        byte[] regDump = bo.toByteArray();
        map.put("regDump", new String(regDump));
        bo.reset();
        GemliteHelper.checkAsmBytes(proc.getMapperRegister().getFileBytes(), pw);
        byte[] regVerify = bo.toByteArray();
        map.put("regVerify", new String(regVerify));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;

}

From source file:Test2.java

License:Apache License

public static void main(String[] args) throws Exception {

    String name = "abcd";
    System.out.println(name.substring(0));
    File f = new File("D:/springsource/gemlite_prod/ynd.test.domain/target/classes/");
    URLClassLoader ucl = new URLClassLoader(new URL[] { f.toURI().toURL() });
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
            "D:/springsource/gemlite_prod/ynd.test.domain/target/classes/ynd/test/Ac01.class"));
    byte[] bytes = new byte[in.available()];
    in.read(bytes);//ww  w .j  av  a2  s  .  c  o m
    in.close();

    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);

    System.out.println(cn.sourceFile + " " + cn.outerClass);
    //    for (Object o : cn.methods)
    //    {
    //      MethodNode mn = (MethodNode) o;
    //      System.out.println(mn.name + " " + mn.desc);
    //    }
    //    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    //    for (Object o : cn.visibleAnnotations)
    //    {
    //      AnnotationNode an = (AnnotationNode) o;
    //      if (DomainMojoConstant.AN_Key.equals(an.desc))
    //      {
    //        Type t = (Type) an.values.get(1);
    //        System.out.println(t.getClassName()+" "+t.getInternalName());
    //        InputStream x1 =  ucl.getResourceAsStream(t.getInternalName()+".class");
    //        System.out.println("&&"+x1.toString());
    //      }
    //      System.out.println(an.desc + " " + an.values);
    //    }

}

From source file:Test1.java

License:Apache License

public static void main(String[] args) throws Exception {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
            "D:/springsource/gemlite_prod/ynd.test.domain/target/classes/ynd/test/Ac01.class"));
    byte[] bytes = new byte[in.available()];
    in.read(bytes);//www.j  a va 2  s. c om
    in.close();

    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);

    cn.interfaces.add(DATAS_NAME);
    implementInterface(cn);
}

From source file:Asm.java

License:Apache License

private static void modify(String clazz) throws Exception {
    ClassNode classNode = new ClassNode();
    ClassReader cr = new ClassReader(clazz);
    cr.accept(classNode, 0);/*  w ww . j  a  v  a 2 s. c  o  m*/

    modify(classNode);

    ClassWriter cw = new ClassWriter(0);
    classNode.accept(cw);
    byte[] b = cw.toByteArray();
    OutputStream out = new FileOutputStream(clazz + ".out");
    out.write(b, 0, b.length);
    out.close();
}

From source file:analysis.ReferenceGenerator.java

License:Open Source License

/**
 * Create a {@link ClassNode} from an {@link Entry}
 * @param e entry/*from   w w w .ja va  2s.c om*/
 * @return
 */
private static ClassNode classNodeFromEntry(Entry e) {
    ClassReader cr;
    ClassNode node;

    cr = new ClassReader(e.getData());
    node = new ClassNode();
    cr.accept(node, ClassReader.SKIP_FRAMES);
    return node;
}

From source file:appeng.core.api.ApiPart.java

License:Open Source License

public ClassNode getReader(String name) {
    ClassReader cr;/*  www  .j  av a  2 s  .  c o m*/
    String path = '/' + name.replace(".", "/") + ".class";
    InputStream is = this.getClass().getResourceAsStream(path);
    try {
        cr = new ClassReader(is);

        ClassNode cn = new ClassNode();
        cr.accept(cn, ClassReader.EXPAND_FRAMES);

        return cn;
    } catch (IOException e) {
        throw new IllegalStateException("Error loading " + name, e);
    }
}

From source file:appeng.coremod.asm.ASMIntegration.java

License:Open Source License

@Nullable
@Override//from w  w w.  j  ava  2s. com
public byte[] transform(final String name, final String transformedName, final byte[] basicClass) {
    if (basicClass == null || transformedName.startsWith("appeng.coremod")) {
        return basicClass;
    }

    if (transformedName.startsWith("appeng.")) {
        final ClassNode classNode = new ClassNode();
        final ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        try {
            final boolean reWrite = this.removeOptionals(classNode);

            if (reWrite) {
                final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                classNode.accept(writer);
                return writer.toByteArray();
            }
        } catch (final Throwable t) {
            t.printStackTrace();
        }
    }
    return basicClass;
}

From source file:appeng.coremod.transformer.IntegrationTransformer.java

License:Open Source License

@Nullable
@Override//w ww. j  a va2s .  c o m
public byte[] transform(final String name, final String transformedName, final byte[] basicClass) {
    if (basicClass == null) {
        return basicClass;
    }

    if (transformedName.startsWith("appeng.")) {
        final ClassNode classNode = new ClassNode();
        final ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        try {
            final boolean reWrite = this.removeOptionals(classNode);

            if (reWrite) {
                final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                classNode.accept(writer);
                return writer.toByteArray();
            }
        } catch (final Throwable t) {
            t.printStackTrace();
        }
    }
    return basicClass;
}

From source file:appeng.transformer.asm.ASMIntegration.java

License:Open Source License

@Nullable
@Override// ww  w.java 2  s.c  o  m
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (basicClass == null || transformedName.startsWith("appeng.transformer")) {
        return basicClass;
    }

    if (transformedName.startsWith("appeng.")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        try {
            boolean reWrite = this.removeOptionals(classNode);

            if (reWrite) {
                ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                classNode.accept(writer);
                return writer.toByteArray();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    return basicClass;
}

From source file:appeng.transformer.asm.ASMTweaker.java

License:Open Source License

@Nullable
@Override/* ww  w . j a  v  a2s .  c  o  m*/
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (basicClass == null) {
        return null;
    }

    try {
        if (transformedName != null && this.privateToPublicMethods.containsKey(transformedName)) {
            ClassNode classNode = new ClassNode();
            ClassReader classReader = new ClassReader(basicClass);
            classReader.accept(classNode, 0);

            for (PublicLine set : this.privateToPublicMethods.get(transformedName)) {
                this.makePublic(classNode, set);
            }

            // CALL VIRTUAL!
            if (transformedName.equals("net.minecraft.client.gui.inventory.GuiContainer")) {
                for (MethodNode mn : classNode.methods) {
                    if (mn.name.equals("func_146977_a") || (mn.name.equals("a") && mn.desc.equals("(Lzk;)V"))) {
                        MethodNode newNode = new MethodNode(Opcodes.ACC_PUBLIC, "func_146977_a_original",
                                mn.desc, mn.signature, EXCEPTIONS);
                        newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
                        newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
                        newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name,
                                mn.name, mn.desc, false));
                        newNode.instructions.add(new InsnNode(Opcodes.RETURN));
                        this.log(newNode.name + newNode.desc + " - New Method");
                        classNode.methods.add(newNode);
                        break;
                    }
                }

                for (MethodNode mn : classNode.methods) {
                    if (mn.name.equals("func_73863_a") || mn.name.equals("drawScreen")
                            || (mn.name.equals("a") && mn.desc.equals("(IIF)V"))) {
                        Iterator<AbstractInsnNode> i = mn.instructions.iterator();
                        while (i.hasNext()) {
                            AbstractInsnNode in = i.next();
                            if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                                MethodInsnNode n = (MethodInsnNode) in;
                                if (n.name.equals("func_146977_a")
                                        || (n.name.equals("a") && n.desc.equals("(Lzk;)V"))) {
                                    this.log(n.name + n.desc + " - Invoke Virtual");
                                    mn.instructions.insertBefore(n, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                                            n.owner, n.name, n.desc, false));
                                    mn.instructions.remove(in);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            classNode.accept(writer);
            return writer.toByteArray();
        }
    } catch (Throwable ignored) {
    }

    return basicClass;
}