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

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

Introduction

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

Prototype

public void accept(final ClassVisitor classVisitor) 

Source Link

Document

Makes the given class visitor visit this class.

Usage

From source file:Test1.java

License:Apache License

private static void implementInterface(ClassNode cn) throws Exception {

    // MethodNode fromMethod = new MethodNode(ACC_PUBLIC, "fromData",
    // "(Ljava/io/DataInput;)V", null, new String[] {
    // "java/io/IOException",
    // "java/lang/ClassNotFoundException" });
    // mv = cw.visitMethod(ACC_PUBLIC, "toData", "(Ljava/io/DataOutput;)V",
    // null, new String[] { "java/io/IOException" });
    MethodNode toMethod = new MethodNode(ACC_PUBLIC, "toData", "(Ljava/io/DataOutput;)V", null,
            new String[] { "java/io/IOException" });
    InsnList instToMethod = toMethod.instructions;
    for (int i = 0; i < cn.fields.size(); i++) {
        FieldNode fn = (FieldNode) cn.fields.get(i);
        toMethod(cn.name, fn, instToMethod);
    }/*from www . j  a v a 2  s.  c  o m*/
    instToMethod.add(new InsnNode(RETURN));
    cn.methods.add(toMethod);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cn.accept(cw);
    byte[] bt = cw.toByteArray();

    ClassReader cr = new ClassReader(bt);
    CheckClassAdapter ca = new CheckClassAdapter(cw);
    FileOutputStream fo = new FileOutputStream("d:/x1.log");
    PrintWriter pw = new PrintWriter(fo);
    ca.verify(cr, true, pw);

    ByteArrayClassLoader bacl = new ByteArrayClassLoader(bt);

    Class cls = bacl.loadClass("ynd.test.Ac01");
    DataSerializable di = (DataSerializable) cls.newInstance();
    di.toData(null);
}

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);//from  ww w.j av a 2s.co  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:appeng.core.api.ApiPart.java

License:Open Source License

public Class getClassByDesc(String addendum, String fullPath, String root, String next) {
    if (this.roots.get(fullPath) != null) {
        return this.roots.get(fullPath);
    }//from  w w  w  . java  2  s  .co  m

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassNode n = this.getReader(next);
    String originalName = n.name;

    try {
        n.name = n.name + '_' + addendum;
        n.superName = Class.forName(root).getName().replace(".", "/");
    } catch (Throwable t) {
        AELog.error(t);
    }

    for (MethodNode mn : n.methods) {
        Iterator<AbstractInsnNode> i = mn.instructions.iterator();
        while (i.hasNext()) {
            this.processNode(i.next(), n.superName);
        }
    }

    DefaultPackageClassNameRemapper remapper = new DefaultPackageClassNameRemapper();
    remapper.inputOutput.put("appeng/api/parts/LayerBase", n.superName);
    remapper.inputOutput.put(originalName, n.name);
    n.accept(new RemappingClassAdapter(cw, remapper));
    // n.accept( cw );

    // n.accept( new TraceClassVisitor( new PrintWriter( System.out ) ) );
    byte[] byteArray = cw.toByteArray();
    int size = byteArray.length;
    Class clazz = this.loadClass(n.name.replace("/", "."), byteArray);

    try {
        Object fish = clazz.newInstance();
        Class rootC = Class.forName(root);

        boolean hasError = false;

        if (!rootC.isInstance(fish)) {
            hasError = true;
            AELog.severe("Error, Expected layer to implement " + root + " did not.");
        }

        if (fish instanceof LayerBase) {
            hasError = true;
            AELog.severe("Error, Expected layer to NOT implement LayerBase but it DID.");
        }

        if (!fullPath.contains(".fmp.")) {
            if (!(fish instanceof TileCableBus)) {
                hasError = true;
                AELog.severe("Error, Expected layer to implement TileCableBus did not.");
            }

            if (!(fish instanceof TileEntity)) {
                hasError = true;
                AELog.severe("Error, Expected layer to implement TileEntity did not.");
            }
        }

        if (!hasError) {
            AELog.info("Layer: " + n.name + " loaded successfully - " + size + " bytes");
        }
    } catch (Throwable t) {
        AELog.severe("Layer: " + n.name + " Failed.");
        AELog.error(t);
    }

    this.roots.put(fullPath, clazz);
    return clazz;
}

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

License:Open Source License

@Nullable
@Override/*from  w ww . ja v  a2 s . c om*/
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/*  ww w.  j  a  v a 2 s  . c om*/
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/*from  ww  w.  ja  va2  s. co  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//from w ww.j  a v  a 2  s  .co 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;
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformTileEntityPiston(byte[] bytes) {
    /*try//from   w  w w . j  ava  2  s  .  c  om
    {
        FileOutputStream fos = new FileOutputStream("d:/TileEntityPiston.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching TileEntityPiston.class ........");

    boolean ok = false, ok2 = false, ok3 = false, ok4 = false, ok5 = false;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();
        if (m.name.equals("<init>") && m.desc.equals("(IIIZZ)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ACONST_NULL));
                    toInject.add(new FieldInsnNode(PUTFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "ast", "bpmeta", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("f") && m.desc.equals("()V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abv")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("g")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(IIII)V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "l", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "m", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "n", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "bpmeta", "I"));
                    toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "setBlockBPdata",
                            "(Labv;IIII)Z"));
                    toInject.add(new InsnNode(POP));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    LabelNode l1 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFNULL, l1));
                    toInject.add(new FieldInsnNode(GETSTATIC, "aqw", "s", "[Laqw;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "a", "I"));
                    toInject.add(new InsnNode(AALOAD));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "b", "I"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "aqw", "createTileEntity", "(Labv;I)Lasm;"));
                    toInject.add(new VarInsnNode(ASTORE, 1));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "asm", "a", "(Lbx;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "l", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "m", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "n", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "abv", "a", "(IIILasm;)V"));
                    toInject.add(l1);

                    m.instructions.insert(m.instructions.get(index), toInject);

                    ok2 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lbx;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l1 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l1));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "c", "(Ljava/lang/String;)B"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "ast", "bpmeta", "I"));
                    toInject.add(l1);
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("TileEntityData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l0 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l0));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("TileEntityData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "l", "(Ljava/lang/String;)Lbx;"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    toInject.add(l0);

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok3 = true;
                    break;
                }
            }
        } else if (m.name.equals("b") && m.desc.equals("(Lbx;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "bpmeta", "I"));
                    toInject.add(new InsnNode(I2B));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;B)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    LabelNode l0 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFNULL, l0));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("TileEntityData"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;Lbx;)V"));
                    toInject.add(l0);

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok4 = true;
                    break;
                }
            }
        } else if (m.name.equals("h") && m.desc.equals("()V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abv")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("g")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(IIII)V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "l", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "m", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "n", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "bpmeta", "I"));
                    toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "setBlockBPdata",
                            "(Labv;IIII)Z"));
                    toInject.add(new InsnNode(POP));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    LabelNode l0 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFNULL, l0));
                    toInject.add(new FieldInsnNode(GETSTATIC, "aqw", "s", "[Laqw;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "a", "I"));
                    toInject.add(new InsnNode(AALOAD));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "b", "I"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "aqw", "createTileEntity", "(Labv;I)Lasm;"));
                    toInject.add(new VarInsnNode(ASTORE, 1));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "movingBlockTileEntityData", "Lbx;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "asm", "a", "(Lbx;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "k", "Labv;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "l", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "m", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "ast", "n", "I"));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "abv", "a", "(IIILasm;)V"));
                    toInject.add(l0);

                    m.instructions.insert(m.instructions.get(index), toInject);

                    ok5 = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2 && ok3 && ok4 && ok5)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3 + ok4 + ok5);

    FieldVisitor fv;

    fv = cw.visitField(ACC_PUBLIC, "movingBlockTileEntityData", "Lbx;", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "bpmeta", "I", null, null);
    fv.visitEnd();

    cw.visitEnd();

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/TileEntityPiston.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockFurnace(byte[] bytes) {
    /*try/*from   w w  w.ja va  2  s.  co  m*/
     {
         FileOutputStream fos = new FileOutputStream("d:/BlockFurnace.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching BlockFurnace.class ............");

    boolean ok = false;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();
        if (m.name.equals("a") && m.desc.equals("(Labv;III)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("aoh")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("k")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Labv;III)V")) {
                    index = index - 5;
                    for (int i = 0; i < 6; i++)
                        m.instructions.remove(m.instructions.get(index));

                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockFurnace.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockDispenser(byte[] bytes) {
    /*try/*ww  w. j  av a 2 s  .c  om*/
     {
         FileOutputStream fos = new FileOutputStream("d:/BlockDispenser.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching BlockDispenser.class ..........");

    boolean ok = false;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();
        if (m.name.equals("a") && m.desc.equals("(Labv;III)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("anv")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("k")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Labv;III)V")) {
                    index = index - 5;
                    for (int i = 0; i < 6; i++)
                        m.instructions.remove(m.instructions.get(index));

                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockDispenser.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}