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:net.doubledoordev.inventorylock.asm.Transformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, READER_FLAGS);

    boolean isPlayer = transformedName.equals(ENTITY_PLAYER_OWNER_NAME);
    if (isPlayer)
        LOGGER.info("Found EntityPlayer");

    for (MethodNode method : classNode.methods) {
        InsnList list = method.instructions;
        if (isPlayer && INSTANCE.mapMethodDesc(method.desc).equals(ENTITY_PLAYER_DESC)
                && INSTANCE.mapMethodName(name, method.name, method.desc).equals(ENTITY_PLAYER_TARGET)) {
            final LabelNode newLabel = new LabelNode();
            LOGGER.info("Found canOpen");
            AbstractInsnNode node = list.getFirst();
            while (node.getOpcode() != IRETURN && node != list.getLast()) {
                if (node.getOpcode() == IFEQ)
                    ((JumpInsnNode) node).label = newLabel;
                node = node.getNext();// www. j  a  v a 2s.com
            }
            if (node.getOpcode() != IRETURN)
                throw new RuntimeException("ASM failed. (return not found)");
            final AbstractInsnNode target = node;
            while (node.getType() != LABEL && node != list.getLast())
                node = node.getNext();
            if (node.getType() != LABEL)
                throw new RuntimeException("ASM failed. (label not found)");
            final LabelNode label = ((LabelNode) node);

            //Adding "else if (code instanceof BetterLockCode) return ((BetterLockCode) code).contains(this.getUniqueID());"
            InsnList inject = new InsnList();

            inject.add(newLabel);
            inject.add(new VarInsnNode(ALOAD, 1));
            inject.add(new TypeInsnNode(INSTANCEOF, BETTER_LOCK_TYPE));
            inject.add(new JumpInsnNode(IFEQ, label));
            inject.add(new VarInsnNode(ALOAD, 1));
            inject.add(new TypeInsnNode(CHECKCAST, BETTER_LOCK_TYPE));
            inject.add(new VarInsnNode(ALOAD, 0));
            //inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ENTITY_PLAYER_OWNER, ENTITY_PLAYER_GET_UUID, ENTITY_PLATER_GET_UUID_DESC, false));
            inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, BETTER_LOCK_TYPE, BETTER_LOCK_CONTAINS,
                    BETTER_LOCK_CONTAINS_DESC, false));
            inject.add(new InsnNode(IRETURN));

            list.insert(target, inject);
            LOGGER.info("Injected elseif into EntityPlayer's canOpen");
        }
        for (AbstractInsnNode node = list.getFirst(); node != list.getLast(); node = node.getNext()) {
            if (node.getOpcode() != INVOKESTATIC)
                continue;
            MethodInsnNode methodInsnNode = ((MethodInsnNode) node);
            //                if (transformedName.equals("net.minecraft.tileentity.TileEntityLockable"))
            //                    LOGGER.info("In {} ({}) Method {}.{}{} Translated {}.{}{}", name, transformedName,
            //                            methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc,
            //                            INSTANCE.map(methodInsnNode.owner), INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc), INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC));
            if (INSTANCE.map(methodInsnNode.owner).equals(LOCK_CODE_OWNER)
                    && INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC)
                    && INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc)
                            .equals(LOCK_CODE_TARGET)) {
                methodInsnNode.owner = LOCK_CODE_OWNER_REPLACE;
                methodInsnNode.name = LOCK_CODE_NAME;
                LOGGER.info("Replaced call in class {} ({}), method {}{}", name, transformedName, method.name,
                        method.desc);
            }
        }
    }

    final ClassWriter writer = new ClassWriter(WRITER_FLAGS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.dries007.tfctweaks.asm.FluidContainerRegistryCT.java

License:Open Source License

private byte[] magic(byte[] bytes) {
    FMLLog.info("Found the FluidContainerRegistry class...");
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);//ww w  .  j a  va2  s.co  m
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("<clinit>") && m.desc.equals("()V")) {
            FMLLog.info("Found the <clinit> method...");

            ListIterator<AbstractInsnNode> i = m.instructions.iterator();
            while (i.hasNext()) {
                AbstractInsnNode node = i.next();
                if (!(node instanceof FieldInsnNode) || node.getOpcode() != GETSTATIC)
                    continue;
                FieldInsnNode fieldInsnNode = ((FieldInsnNode) node);
                if (!fieldInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!fieldInsnNode.name.equals("WATER") && !fieldInsnNode.name.equals("LAVA"))
                    continue;
                if (!fieldInsnNode.desc.equals("Lnet/minecraftforge/fluids/Fluid;"))
                    continue;
                do {
                    i.remove();
                    node = i.next();
                } while (node.getOpcode() != POP);
                i.remove(); // remove last pop
                FMLLog.info("[FluidContainerRegistryCT] Removed the " + fieldInsnNode.name + " registration.");
                done++;
            }
        }
    }

    if (done != DONE) {
        FMLLog.severe(
                "\n######################################################################################\n"
                        + "######################################################################################\n"
                        + "######################################################################################\n"
                        + "OUR ASM FLUID HACK FAILED! PLEASE MAKE AN ISSUE REPORT ON GITHUB WITH A COMPLETE MODLIST! https://github.com/dries007/TFC-Tweaks\n"
                        + "Done %d out of %d ASM tweaks on class FluidContainerRegistry\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n\n",
                done, DONE);
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.dries007.tfctweaks.asm.FluidRegistryCT.java

License:Open Source License

private byte[] magic(byte[] bytes) {
    FMLLog.info("Found the FluidRegistry class...");
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);//from   w w  w  .  ja va  2 s .c  o  m
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("<clinit>") && m.desc.equals("()V")) {
            FMLLog.info("Found the <clinit> method...");

            ListIterator<AbstractInsnNode> i = m.instructions.iterator();
            while (i.hasNext()) {
                AbstractInsnNode node = i.next();
                if (!(node instanceof FieldInsnNode) || node.getOpcode() != GETSTATIC)
                    continue;
                FieldInsnNode fieldInsnNode = ((FieldInsnNode) node);
                if (!fieldInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!fieldInsnNode.name.equals("WATER") && !fieldInsnNode.name.equals("LAVA"))
                    continue;
                if (!fieldInsnNode.desc.equals("Lnet/minecraftforge/fluids/Fluid;"))
                    continue;
                node = i.next();
                if (!(node instanceof MethodInsnNode) || node.getOpcode() != INVOKESTATIC)
                    continue;
                MethodInsnNode methodInsnNode = ((MethodInsnNode) node);
                if (!methodInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!methodInsnNode.name.equals("registerFluid"))
                    continue;
                if (!methodInsnNode.desc.equals("(Lnet/minecraftforge/fluids/Fluid;)Z"))
                    continue;
                node = i.next();
                if (!(node instanceof InsnNode) || node.getOpcode() != POP)
                    continue;
                InsnNode insnNode = ((InsnNode) node);
                m.instructions.remove(fieldInsnNode);
                m.instructions.remove(methodInsnNode);
                m.instructions.remove(insnNode);
                FMLLog.info("[FluidRegistryCT] Removed the " + fieldInsnNode.name + " registration.");
                done++;
            }
        } else if (m.name.equals("getFluid")
                && m.desc.equals("(Ljava/lang/String;)Lnet/minecraftforge/fluids/Fluid;")) {
            FMLLog.info("Found the getFluid method...");
            InsnList insnList = new InsnList();
            {
                LabelNode labelFirstIf = new LabelNode();
                insnList.add(new FieldInsnNode(GETSTATIC, "net/dries007/tfctweaks/util/FluidHacks",
                        "makeAllWaterFTCWater", "Z"));
                insnList.add(new JumpInsnNode(IFEQ, labelFirstIf));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new LdcInsnNode("water"));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, labelFirstIf));
                insnList.add(new FieldInsnNode(GETSTATIC, "com/bioxx/tfc/api/TFCFluids", "FRESHWATER",
                        "Lnet/minecraftforge/fluids/Fluid;"));
                insnList.add(new InsnNode(ARETURN));
                insnList.add(labelFirstIf);
            }
            {
                LabelNode lableSecondIf = new LabelNode();
                insnList.add(new FieldInsnNode(GETSTATIC, "net/dries007/tfctweaks/util/FluidHacks",
                        "makeAllLavaFTCLava", "Z"));
                insnList.add(new JumpInsnNode(IFEQ, lableSecondIf));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new LdcInsnNode("lava"));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, lableSecondIf));
                insnList.add(new FieldInsnNode(GETSTATIC, "com/bioxx/tfc/api/TFCFluids", "LAVA",
                        "Lnet/minecraftforge/fluids/Fluid;"));
                insnList.add(new InsnNode(ARETURN));
                insnList.add(lableSecondIf);
            }
            m.instructions.insertBefore(m.instructions.getFirst(), insnList);

            FMLLog.info("[FluidRegistryCT] Edited getFluid(String) : Fluid.");
            done++;
        } else if (m.name.equals("isFluidRegistered")) {
            if (m.desc.equals("(Lnet/minecraftforge/fluids/Fluid;)Z")) {
                InsnList insnList = new InsnList();
                LabelNode falseLabel = new LabelNode();
                LabelNode trueLabel = new LabelNode();
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new JumpInsnNode(IFNULL, falseLabel));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/fluids/Fluid", "getName",
                        "()Ljava/lang/String;", false));
                insnList.add(new MethodInsnNode(INVOKESTATIC, "net/minecraftforge/fluids/FluidRegistry",
                        "isFluidRegistered", "(Ljava/lang/String;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, falseLabel));
                insnList.add(new InsnNode(ICONST_1));
                insnList.add(new JumpInsnNode(GOTO, trueLabel));
                insnList.add(falseLabel);
                insnList.add(new InsnNode(ICONST_0));
                insnList.add(trueLabel);
                insnList.add(new InsnNode(IRETURN));
                // replace entire method
                m.instructions.clear();
                m.instructions.add(insnList);

                FMLLog.info("[FluidRegistryCT] Edited isFluidRegistered(Fluid) : bool.");
                done++;
            } else if (m.desc.equals("(Ljava/lang/String;)Z")) {
                InsnList insnList = new InsnList();
                LabelNode trueLabel = new LabelNode();
                insnList.add(new LdcInsnNode("water"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFNE, trueLabel));
                insnList.add(new LdcInsnNode("lava"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFNE, trueLabel));
                insnList.add(new FieldInsnNode(GETSTATIC, "net/minecraftforge/fluids/FluidRegistry", "fluids",
                        "Lcom/google/common/collect/BiMap;"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEINTERFACE, "com/google/common/collect/BiMap",
                        "containsKey", "(Ljava/lang/Object;)Z", true));
                LabelNode falseLabel = new LabelNode();
                insnList.add(new JumpInsnNode(IFEQ, falseLabel));
                insnList.add(trueLabel);
                insnList.add(new InsnNode(ICONST_1));
                LabelNode returnLabel = new LabelNode();
                insnList.add(new JumpInsnNode(GOTO, returnLabel));
                insnList.add(falseLabel);
                insnList.add(new InsnNode(ICONST_0));
                insnList.add(returnLabel);
                insnList.add(new InsnNode(IRETURN));

                // replace entire method
                m.instructions.clear();
                m.instructions.add(insnList);

                FMLLog.info("[FluidRegistryCT] Edited isFluidRegistered(String) : bool.");
                done++;
            }
        } else if (m.name.equals("getFluidStack")) {
            LabelNode notNullNode = null;
            { // Grab first jump node label
                ListIterator<AbstractInsnNode> i = m.instructions.iterator();
                while (i.hasNext()) {
                    AbstractInsnNode node = i.next();
                    if (node.getOpcode() == IFNE) {
                        notNullNode = ((JumpInsnNode) node).label;
                        break;
                    }
                }
            }

            InsnList insnList = new InsnList();
            insnList.add(new LdcInsnNode("water"));
            insnList.add(new VarInsnNode(ALOAD, 0));
            insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            insnList.add(new JumpInsnNode(IFNE, notNullNode));
            insnList.add(new LdcInsnNode("lava"));
            insnList.add(new VarInsnNode(ALOAD, 0));
            insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            insnList.add(new JumpInsnNode(IFNE, notNullNode));

            // add to the beginning of the list, leave the rest of the method in place.
            m.instructions.insert(insnList);

            FMLLog.info("[FluidRegistryCT] Edited getFluidStack(String, int) : FluidStack.");
            done++;
        }
    }

    if (done != DONE) {
        FMLLog.severe(
                "\n######################################################################################\n"
                        + "######################################################################################\n"
                        + "######################################################################################\n"
                        + "OUR ASM FLUID HACK FAILED! PLEASE MAKE AN ISSUE REPORT ON GITHUB WITH A COMPLETE MODLIST! https://github.com/dries007/TFC-Tweaks\n"
                        + "Done %d out of %d ASM tweaks on class FluidRegistry\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n\n",
                done, DONE);
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.epoxide.surge.asm.ASMUtils.java

License:Creative Commons License

/**
 * Converts a byte array into a ClassNode which can then easily be worked with and
 * manipulated.//ww w .  j  a v  a  2s  . c  o m
 *
 * @param classBytes: The byte array representation of the class.
 * @return ClassNode: A ClassNode representation of the class, built from the byte array.
 */
public static ClassNode createClassFromByteArray(byte[] classBytes) {

    final ClassNode classNode = new ClassNode();
    final ClassReader classReader = new ClassReader(classBytes);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    return classNode;
}

From source file:net.fabricmc.base.transformer.AccessTransformer.java

License:Apache License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (!name.startsWith("net.minecraft")) {
        return bytes;
    }/*from w  w w. j  a v a  2 s  .  co  m*/
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean isClassProtected = classNode.access == Opcodes.ACC_PROTECTED;
    boolean isClassPrivate = classNode.access == Opcodes.ACC_PRIVATE;
    if (isClassProtected || isClassPrivate) {
        classNode.access = Opcodes.ACC_PUBLIC;
    }
    for (MethodNode method : classNode.methods) {
        boolean isProtected = method.access == Opcodes.ACC_PROTECTED;
        boolean isPrivate = method.access == Opcodes.ACC_PRIVATE;
        if (isProtected || isPrivate) {
            method.access = Opcodes.ACC_PUBLIC;
        }
    }
    for (FieldNode field : classNode.fields) {
        boolean isProtected = field.access == Opcodes.ACC_PROTECTED;
        boolean isPrivate = field.access == Opcodes.ACC_PRIVATE;
        if (isProtected || isPrivate) {
            field.access = Opcodes.ACC_PUBLIC;
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.lyonlancer5.mcmp.karasu.asm.KarasuTransformer.java

License:Apache License

private static byte[] transform(int index, byte[] classBeingTransformed) {
    try {/*  w w  w.  j  ava 2s  .  c o m*/
        ClassNode e = new ClassNode();
        ClassReader classReader = new ClassReader(classBeingTransformed);
        classReader.accept(e, 0);
        switch (index) {
        case 0:
            transformEntityLivingBase(e);
        default:
            ClassWriter classWriter = new ClassWriter(1);
            e.accept(classWriter);
            return classWriter.toByteArray();
        }
    } catch (Exception var6) {
        var6.printStackTrace();
        return classBeingTransformed;
    }
}

From source file:net.malisis.core.asm.MalisisClassTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    ArrayList<AsmHook> hooks = listHooks.get(transformedName);
    if (hooks == null || hooks.size() == 0)
        return bytes;

    LogManager.getLogger(logString).info("Found hooks for {} ({})", transformedName, name);

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);/* w w  w.  j  a v a 2  s . co  m*/

    for (AsmHook hook : hooks) {
        MethodNode methodNode = AsmUtils.findMethod(classNode, hook.getMethodName(),
                hook.getMethodDescriptor());
        if (methodNode != null) {
            if (!hook.walkSteps(methodNode))
                LogManager.getLogger(logString).error("The instruction list was not found in {}:{}{}",
                        hook.getTargetClass(), hook.getMethodName(), hook.getMethodDescriptor());

            if (hook.isDebug() == true && !MalisisCore.isObfEnv) {
                System.err.println(AsmUtils.getMethodNodeAsString(methodNode));
            }
        } else {
            LogManager.getLogger(logString).error("Method not found : {}:{}{}", hook.getTargetClass(),
                    hook.getMethodName(), hook.getMethodDescriptor());
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS /* | ClassWriter.COMPUTE_FRAMES */);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*from w ww  . j av  a2 s.  c om*/

    if (DEBUG) {
        FMLRelaunchLog.fine("Considering all methods and fields on %s (%s)\n", transformedName, name);
    }
    if (!modifiers.containsKey(transformedName)) {
        return bytes;
    }

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

    Collection<Modifier> mods = modifiers.get(transformedName);
    for (Modifier m : mods) {
        if (m.modifyClassVisibility) {
            classNode.access = getFixedAccess(classNode.access, m);
            if (DEBUG) {
                System.out.println(String.format("Class: %s %s -> %s", name, toBinary(m.oldAccess),
                        toBinary(m.newAccess)));
            }
            continue;
        }
        if (m.desc.isEmpty()) {
            for (FieldNode n : classNode.fields) {
                if (n.name.equals(m.name) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);
                    if (DEBUG) {
                        System.out.println(String.format("Field: %s.%s %s -> %s", name, n.name,
                                toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }

                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }
        } else {
            List<MethodNode> nowOverridable = Lists.newArrayList();
            for (MethodNode n : classNode.methods) {
                if ((n.name.equals(m.name) && n.desc.equals(m.desc)) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);

                    // constructors always use INVOKESPECIAL
                    if (!n.name.equals("<init>")) {
                        // if we changed from private to something else we need to replace all INVOKESPECIAL calls to this method with INVOKEVIRTUAL
                        // so that overridden methods will be called. Only need to scan this class, because obviously the method was private.
                        boolean wasPrivate = (m.oldAccess & ACC_PRIVATE) == ACC_PRIVATE;
                        boolean isNowPrivate = (m.newAccess & ACC_PRIVATE) == ACC_PRIVATE;

                        if (wasPrivate && !isNowPrivate) {
                            nowOverridable.add(n);
                        }

                    }

                    if (DEBUG) {
                        System.out.println(String.format("Method: %s.%s%s %s -> %s", name, n.name, n.desc,
                                toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }

                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }

            replaceInvokeSpecial(classNode, nowOverridable);
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper.java

License:Open Source License

private String getFieldType(String owner, String name) {
    if (fieldDescriptions.containsKey(owner)) {
        return fieldDescriptions.get(owner).get(name);
    }/*from w  ww. j  a  v  a  2 s. c  om*/
    synchronized (fieldDescriptions) {
        try {
            byte[] classBytes = ClassPatchManager.INSTANCE.getPatchedResource(owner,
                    map(owner).replace('/', '.'), classLoader);
            if (classBytes == null) {
                return null;
            }
            ClassReader cr = new ClassReader(classBytes);
            ClassNode classNode = new ClassNode();
            cr.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
            Map<String, String> resMap = Maps.newHashMap();
            for (FieldNode fieldNode : (List<FieldNode>) classNode.fields) {
                resMap.put(fieldNode.name, fieldNode.desc);
            }
            fieldDescriptions.put(owner, resMap);
            return resMap.get(name);
        } catch (IOException e) {
            FMLRelaunchLog.log(Level.ERROR, e, "A critical exception occured reading a class file %s", owner);
        }
        return null;
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.FieldRedirectTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (!this.clsName.equals(transformedName))
        return basicClass;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);/*from  ww  w .  java  2s .  c om*/

    FieldNode fieldRef = null;
    for (FieldNode f : classNode.fields) {
        if (this.TYPE.equals(f.desc) && fieldRef == null) {
            fieldRef = f;
        } else if (this.TYPE.equals(f.desc)) {
            throw new RuntimeException("Error processing " + clsName + " - found a duplicate holder field");
        }
    }
    if (fieldRef == null) {
        throw new RuntimeException("Error processing " + clsName
                + " - no holder field declared (is the code somehow obfuscated?)");
    }

    MethodNode getMethod = null;
    for (MethodNode m : classNode.methods) {
        if (m.name.equals(this.bypass))
            continue;
        if (this.DESC.equals(m.desc) && getMethod == null) {
            getMethod = m;
        } else if (this.DESC.equals(m.desc)) {
            throw new RuntimeException("Error processing " + clsName + " - duplicate get method found");
        }
    }
    if (getMethod == null) {
        throw new RuntimeException(
                "Error processing " + clsName + " - no get method found (is the code somehow obfuscated?)");
    }

    for (MethodNode m : classNode.methods) {
        if (m.name.equals(this.bypass))
            continue;
        for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext();) {
            AbstractInsnNode insnNode = it.next();
            if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode fi = (FieldInsnNode) insnNode;
                if (fieldRef.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
                    it.remove();
                    MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name,
                            getMethod.name, getMethod.desc, false);
                    it.add(replace);
                }
            }
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}