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.lion328.thaifixes.coremod.patcher.GuiChatBytecodePatcher.java

License:Open Source License

@Override
public byte[] patchClass(byte[] source) {
    if (ThaiFixesConfiguration.getFontStyle() != ThaiFixesFontStyle.MCPX)
        return source;

    ClassReader classReader = new ClassReader(source);
    ClassNode classNode = new ClassNode();
    classReader.accept(classNode, 0);//  w ww .  j  av  a  2 s  .  c om

    for (MethodNode method : classNode.methods) {
        boolean drawScreenFlag;
        if ((drawScreenFlag = (method.name.equals(CLASSMAP.getMethod("drawScreen"))
                && method.desc.equals("(IIF)V")))
                || (method.name.equals(CLASSMAP.getMethod("initGui")) && method.desc.equals("()V"))) {
            for (int i = 0; i < method.instructions.size(); i++) {
                if ((method.instructions.get(i).getOpcode() == Opcodes.BIPUSH)
                        && (method.instructions.get(i + 1).getOpcode() == Opcodes.ISUB)) {
                    IntInsnNode node = (IntInsnNode) method.instructions.get(i);
                    if (node.operand == (drawScreenFlag ? 14 : 12))
                        method.instructions.set(node,
                                new VarInsnNode(Opcodes.BIPUSH,
                                        (drawScreenFlag ? ThaiFixesFontRenderer.MCPX_CHATBLOCK_HEIGHT + 2
                                                : ThaiFixesFontRenderer.MCPX_CHATBLOCK_TEXT_YPOS + 2)));
                }
            }
        }
    }

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

From source file:com.lion328.thaifixes.coremod.patcher.GuiNewChatBytecodePatcher.java

License:Open Source License

@Override
public byte[] patchClass(byte[] source) {
    if (ThaiFixesConfiguration.getFontStyle() != ThaiFixesFontStyle.MCPX)
        return source;

    ClassReader classReader = new ClassReader(source);
    ClassNode classNode = new ClassNode();
    classReader.accept(classNode, 0);/*from  w  ww.  ja v  a  2 s .c o  m*/

    for (MethodNode method : classNode.methods) {
        if (method.name.equals(CLASSMAP.getMethod("drawChat")) && method.desc.equals("(I)V")) {
            AbstractInsnNode currentNode = null;
            for (int i = 0; i < method.instructions.size(); i++) {
                currentNode = method.instructions.get(i);
                if (currentNode.getOpcode() == Opcodes.BIPUSH) {
                    if (method.instructions.get(i + 1).getOpcode() == Opcodes.IMUL)
                        method.instructions.set(currentNode,
                                new VarInsnNode(Opcodes.BIPUSH, ThaiFixesFontRenderer.MCPX_CHATBLOCK_HEIGHT));
                    else if (method.instructions.get(i + 1).getOpcode() == Opcodes.ISUB
                            && method.instructions.get(i - 1).getOpcode() == Opcodes.ILOAD) {
                        IntInsnNode node = (IntInsnNode) currentNode;
                        if (node.operand == 9)
                            method.instructions.set(currentNode, new VarInsnNode(Opcodes.BIPUSH,
                                    ThaiFixesFontRenderer.MCPX_CHATBLOCK_HEIGHT));
                        else if (node.operand == 8)
                            method.instructions.set(currentNode, new VarInsnNode(Opcodes.BIPUSH,
                                    ThaiFixesFontRenderer.MCPX_CHATBLOCK_TEXT_YPOS));
                    }
                }
            }
        } else if (method.name.equals(CLASSMAP.getMethod("func_146236_a"))
                && method.desc.equals("(II)L" + ClassMap.getClassMap("net.minecraft.util.IChatComponent")
                        .getClassInfo().getProductionClassName().replace('.', '/') + ";")) {
            for (int i = 0; i < method.instructions.size(); i++) {
                if (method.instructions.get(i).getOpcode() == Opcodes.GETFIELD) {
                    FieldInsnNode node = (FieldInsnNode) method.instructions.get(i);
                    if (node.owner.equals(ClassMap.getClassMap("net.minecraft.client.gui.FontRenderer")
                            .getClassInfo().getProductionClassName().replace('.', '/'))
                            && node.name.equals(ClassMap.getClassMap("net.minecraft.client.gui.FontRenderer")
                                    .getField("FONT_HEIGHT"))) {
                        method.instructions.set(node,
                                new VarInsnNode(Opcodes.BIPUSH, ThaiFixesFontRenderer.MCPX_CHATBLOCK_HEIGHT));
                        method.instructions.remove(method.instructions.get(i - 1)); // GETFIELD Minecraft.mc
                        method.instructions.remove(method.instructions.get(i - 2)); // GETFIELD GuiNewChat.mc
                        method.instructions.remove(method.instructions.get(i - 3)); // ALOAD 0
                    }
                }
            }
        }
    }

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

From source file:com.lion328.xenonlauncher.patcher.StringReplaceFilePatcher.java

License:Open Source License

@Override
public byte[] patchFile(String name, byte[] original) {
    if (!name.endsWith(".class")) {
        return original;
    }/*from  w ww  . jav  a 2 s. c  o  m*/

    ClassReader reader = new ClassReader(original);
    ClassNode node = new ClassNode();
    reader.accept(node, 0);

    List fields = node.fields;
    FieldNode field;
    for (Object obj : fields) {
        if (!(obj instanceof FieldNode)) {
            continue;
        }

        field = (FieldNode) obj;

        if (field.value instanceof String) {
            field.value = ((String) field.value).replace(find, replace);
        }
    }

    List methods = node.methods;
    MethodNode method;
    for (Object obj : methods) {
        if (!(obj instanceof MethodNode)) {
            continue;
        }

        method = (MethodNode) obj;

        InsnList insns = method.instructions;

        for (int i = 0; i < insns.size(); i++) {
            AbstractInsnNode insn = insns.get(i);

            if (!(insn instanceof LdcInsnNode)) {
                continue;
            }

            LdcInsnNode ldc = (LdcInsnNode) insn;

            if (!(ldc.cst instanceof String)) {
                continue;
            }

            ldc.cst = ((String) ldc.cst).replace(find, replace);
        }
    }

    ClassWriter writer = new ClassWriter(0);
    node.accept(writer);

    return writer.toByteArray();
}

From source file:com.mebigfatguy.junitflood.generator.simple.SimpleGenerator.java

License:Apache License

private ClassNode parseClass(ClassPathItem item) throws IOException {
    try (InputStream is = item.getInputStream()) {
        ClassReader cr = new ClassReader(is);
        ClassNode node = new ClassNode();
        cr.accept(node, ClassReader.SKIP_CODE);
        return node;
    }/*from w ww . j  a va 2s .  c om*/
}

From source file:com.microsoft.Malmo.OverclockingClassTransformer.java

License:Open Source License

private static byte[] transform(byte[] serverClass, boolean isObfuscated, transformType type) {
    System.out.println("MALMO: Attempting to transform MinecraftServer");
    try {/*w  w  w.jav  a 2s  .  c  o m*/
        ClassNode cnode = new ClassNode();
        ClassReader creader = new ClassReader(serverClass);
        creader.accept(cnode, 0);

        switch (type) {
        case SERVER:
            overclockServer(cnode, isObfuscated);
            break;
        case RENDERER:
            overclockRenderer(cnode, isObfuscated);
            break;
        }

        ClassWriter cwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        cnode.accept(cwriter);
        return cwriter.toByteArray();
    } catch (Exception e) {
        System.out.println("MALMO FAILED to transform MinecraftServer - overclocking not available!");
    }
    return serverClass;
}

From source file:com.mogujie.instantrun.IncrementalVisitor.java

License:Apache License

private static List<ClassNode> parseParents(ZipFile zipFile, final ClassNode classNode) throws IOException {
    List<ClassNode> parentNodes = new ArrayList<ClassNode>();
    String currentParentName = classNode.superName;

    while (currentParentName != null) {
        ZipEntry zipEntry = zipFile.getEntry(currentParentName + ".class");
        if (zipEntry != null) {
            InputStream parentFileClassReader = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            ClassReader parentClassReader = new ClassReader(parentFileClassReader);
            ClassNode parentNode = new TransformAccessClassNode();
            parentClassReader.accept(parentNode, ClassReader.EXPAND_FRAMES);
            parentNodes.add(parentNode);
            currentParentName = parentNode.superName;
        } else {//  w ww  .ja  v  a 2 s .c  o  m
            // May need method information from outside of the current project. Thread local class reader
            // should be the one
            try {
                ClassReader parentClassReader = new ClassReader(Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(currentParentName + ".class"));
                ClassNode parentNode = new ClassNode();
                parentClassReader.accept(parentNode, ClassReader.EXPAND_FRAMES);
                parentNodes.add(parentNode);
                currentParentName = parentNode.superName;

            } catch (IOException e) {
                // Could not locate parent class. This is as far as we can go locating parents.
                return ImmutableList.of();
            }
        }
    }
    return parentNodes;
}

From source file:com.mtso.blazer.JavaUtil.java

License:Open Source License

public static String retrieveCanonicalNameFromClass(File classFile) throws FileNotFoundException, IOException {

    ClassNode classNode = new ClassNode();
    InputStream classFileInputStream = new FileInputStream(classFile);
    try {/* w w w . j  av  a2 s .  co  m*/
        ClassReader classReader = new ClassReader(classFileInputStream);
        classReader.accept((ClassVisitor) classNode, 0);
    } finally {
        classFileInputStream.close();
    }

    Type classType = Type.getObjectType(classNode.name);
    return classType.getClassName();
}

From source file:com.mtso.blazer.JavaUtil.java

License:Open Source License

public static ArrayList retrieveSignaturesFromClass(InputStream classFileIS, int signId) throws Exception {

    /*//www  . j  a  v  a  2 s.  c  om
     * Dynamic array containing multiple signatures arrays
     */
    ArrayList signatures = new ArrayList();

    Object[] signature = new Object[7];
    signature[0] = new Boolean(false); //used for signature selection

    ClassNode classNode = new ClassNode();
    InputStream classFileInputStream = classFileIS;
    try {
        ClassReader classReader = new ClassReader(classFileInputStream);
        classReader.accept((ClassVisitor) classNode, 0);
    } finally {
        classFileInputStream.close();
    }

    Type classType = Type.getObjectType(classNode.name);

    if ((classNode.access & Opcodes.ACC_PUBLIC) != 0) {

        if ((classNode.access & Opcodes.ACC_INTERFACE) != 0) {
            signature[2] = "Interface"; //interface
        } else {
            signature[2] = "Class"; //class
        }

        String name = classType.getClassName();
        name = name.substring(name.lastIndexOf('.') + 1); //packages are not required
        signature[3] = name.substring(0, 1).toLowerCase() + name.substring(1); //convert to javaCase
        @SuppressWarnings("unchecked")
        List<MethodNode> methodNodes = classNode.methods;

        for (MethodNode methodNode : methodNodes) {

            Type[] argumentTypes = Type.getArgumentTypes(methodNode.desc);

            if ((methodNode.access & Opcodes.ACC_PUBLIC) != 0) {

                signature[4] = methodNode.name;

                if (methodNode.visibleAnnotations != null) {

                    Iterator c = methodNode.visibleAnnotations.iterator();
                    while (c.hasNext()) {
                        AnnotationNode anode = (AnnotationNode) c.next();
                        String annotations = anode.desc;
                        annotations = annotations.substring(annotations.lastIndexOf('/') + 1,
                                annotations.lastIndexOf(';'));
                        signature[6] = "@".concat(annotations); //convert to standard format
                    }
                }

                StringBuilder pars = new StringBuilder();

                for (int i = 0; i < argumentTypes.length; i++) {

                    Type argumentType = argumentTypes[i];
                    if (i > 0) {
                        pars.append(", ");
                    }
                    pars.append(argumentType.getClassName());
                }

                signature[5] = pars.toString();

                /* list here all exceptions */
                if (!signature[4].equals("<init>")) {
                    signature[1] = new Integer(signId);
                    signatures.add(signature.clone());
                    signId++;
                }
                signature[5] = "";
            }
            signature[4] = "";
            signature[6] = "";
        }
    }
    return signatures;
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMAspectWeaverTest.java

License:Apache License

private Object getInstnace(final String originalName, final String aspectName) throws Exception {
    final ClassLoader defaultClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader classLoader = new ClassLoader() {
        @Override/*from   www.j  ava2 s  .c  o m*/
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (name.equals(originalName)) {
                try {
                    final ClassReader cr = new ClassReader(
                            getClass().getResourceAsStream("/" + name.replace('.', '/') + ".class"));
                    final ClassNode classNode = new ClassNode();
                    cr.accept(classNode, 0);

                    final ASMClassNodeAdapter sourceClassNode = new ASMClassNodeAdapter(defaultClassLoader,
                            classNode);
                    final ASMClassNodeAdapter adviceClassNode = ASMClassNodeAdapter.get(defaultClassLoader,
                            aspectName.replace('.', '/'));

                    final ASMAspectWeaver aspectWeaver = new ASMAspectWeaver();
                    aspectWeaver.weaving(sourceClassNode, adviceClassNode);

                    final ClassWriter cw = new ClassWriter(0);
                    classNode.accept(cw);
                    final byte[] bytecode = cw.toByteArray();
                    CheckClassAdapter.verify(new ClassReader(bytecode), false, new PrintWriter(System.out));

                    return super.defineClass(name, bytecode, 0, bytecode.length);
                } catch (Exception ex) {
                    throw new ClassNotFoundException("Load error: " + ex.toString(), ex);
                }
            } else {
                return super.loadClass(name);
            }
        }
    };
    Class clazz = classLoader.loadClass(originalName);
    return clazz.newInstance();
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java

License:Apache License

public static ASMClassNodeAdapter get(final ClassLoader classLoader, final String className,
        final boolean skipCode) {
    if (className == null) {
        throw new IllegalArgumentException("class name must not be null.");
    }//from   w  ww . j  a va  2  s  . c  o m

    ClassLoader aClassLoader = classLoader;
    if (aClassLoader == null) {
        // bootstrap class loader.
        aClassLoader = ClassLoader.getSystemClassLoader();
    }

    if (aClassLoader == null) {
        // not initialized system classloader.
        return null;
    }

    InputStream in = null;
    try {
        in = aClassLoader.getResourceAsStream(className + ".class");
        if (in != null) {
            final ClassReader classReader = new ClassReader(in);
            final ClassNode classNode = new ClassNode();
            if (skipCode) {
                classReader.accept(classNode, ClassReader.SKIP_CODE);
            } else {
                classReader.accept(classNode, 0);
            }

            return new ASMClassNodeAdapter(classLoader, classNode, skipCode);
        }
    } catch (IOException ignored) {
        // not found class.
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }

    return null;
}