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:travis.model.project.Builder.java

License:Open Source License

private void getFiles(File rootDirectory, StructPackage parent) throws IOException {
    File[] files = rootDirectory.listFiles();
    if (files == null) {
        return;/*from  ww  w. jav a 2s .  c om*/
    }

    for (File file : files) {
        if (file.isHidden()) {
            continue;
        }

        if (file.isDirectory()) {
            StructPackage sPackage;
            if (parent.isDefaultPackage()) {
                sPackage = new StructPackage(file.getName(), parent);
            } else {
                sPackage = new StructPackage(parent.getName() + "." + file.getName(), parent);
            }
            parent.addStructComponent(sPackage);
            getFiles(file, sPackage);
        } else if (file.getName().endsWith(".class")) {
            ClassReader cr = new ClassReader(new FileInputStream(file));
            ClassNode cn = new ClassNode();
            cr.accept(cn, ClassReader.SKIP_DEBUG);
            parent.addStructComponent(new StructClass(cn, parent));
        }
    }
}

From source file:uk.co.mysterymayhem.tessellatorfix.Transformer.java

private static byte[] patchTessellatorClass(byte[] bytes) {
    String targetMethodName;//w ww .  jav a2 s  .c  o  m

    if (Plugin.runtimeDeobfEnabled) {
        targetMethodName = "func_147564_a";
    } else {
        targetMethodName = "getVertexState";
    }

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

    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        MethodNode m = methods.next();
        if ((m.name.equals(targetMethodName)
                && m.desc.equals("(FFF)Lnet/minecraft/client/shader/TesselatorVertexState;"))) {
            FMLLog.info("Inside target Tessellator method");

            InsnList toInject = new InsnList();

            // Insertion of "if (this.rawBufferIndex < 1) return"
            LabelNode labelNode = new LabelNode();

            toInject.add(new VarInsnNode(Opcodes.ALOAD, 0));
            String fieldName;
            if (Plugin.runtimeDeobfEnabled) {
                fieldName = "field_147569_p";
            } else {
                fieldName = "rawBufferIndex";
            }
            toInject.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/client/renderer/Tessellator",
                    fieldName, "I"));
            toInject.add(new InsnNode(Opcodes.ICONST_1));
            toInject.add(new JumpInsnNode(Opcodes.IF_ICMPGE, labelNode));
            toInject.add(new InsnNode(Opcodes.ACONST_NULL));
            toInject.add(new InsnNode(Opcodes.ARETURN));
            toInject.add(labelNode);

            // Insert after
            m.instructions.insert(toInject);

            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            classNode.accept(writer);
            FMLLog.info("Exiting target Tessellator method");
            return writer.toByteArray();
        }
    }

    FMLLog.warning("Could not find Tessellator method out of:");
    StringBuilder builder = new StringBuilder();
    for (MethodNode methodNode : classNode.methods) {
        builder.append(methodNode.name).append(":").append(methodNode.desc).append("\n");
    }
    FMLLog.info(builder.toString());

    return bytes;
}

From source file:util.BytecodeChecker.java

License:Apache License

public static void main(String[] args) {
    String className = args[0];//from ww  w . j  av a2  s  .c  om
    String methodName = args[1];

    try {
        ClassReader cr = new ClassReader(className);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);

        for (int i = 0; i < cn.methods.size(); i++) {
            MethodNode mn = (MethodNode) cn.methods.get(i);

            if (mn.name.equals(methodName)) {
                System.out.println("METHOD CODE");
                System.out.println("===========");

                for (int j = 0; j < mn.instructions.size(); j++) {
                    AbstractInsnNode insn = mn.instructions.get(j);
                    System.out.println(String.valueOf(j + 1) + ":" + insn);
                }

                System.out.println("ANALYSIS");
                System.out.println("========");

                Analyzer bca = new Analyzer(new SimpleVerifier());
                bca.analyze(className.replace('.', '/'), mn);
            }
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
    }
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transform(byte[] basicClass, Pair<MethodSignature, MethodAction>... methods) {
    ClassReader reader = new ClassReader(basicClass);
    ClassNode node = new ClassNode();
    reader.accept(node, 0);/*from   w  w  w  .ja v a2s.c  o  m*/

    boolean didAnything = false;

    for (Pair<MethodSignature, MethodAction> pair : methods) {
        log("Applying Transformation to method (" + pair.getLeft() + ")");
        didAnything |= findMethodAndTransform(node, pair.getLeft(), pair.getRight());
    }

    if (didAnything) {
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        node.accept(writer);
        return writer.toByteArray();
    }

    return basicClass;
}

From source file:works.cirno.mocha.parameter.name.ASMParameterAnalyzer.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public Parameter[] getParameters(InvokeTarget target, Parameter[] parameters) {
    if (!enabled) {
        return parameters;
    }//ww  w  .ja v a 2 s  . co  m
    Class<?> controllerClass = target.getControllerClass();
    Method method = target.getMethod();
    String methodName = method.getName();

    Class<?>[] parameterTypes = method.getParameterTypes();
    int parameterCount = parameterTypes.length;

    // /////////Resolve parameter names by asm
    {
        if (log.isDebugEnabled()) {
            log.debug("Analyzing method {}.{}", controllerClass.getName(), method.getName());
        }
        ClassNode node = classNodeCache.get(controllerClass);
        if (node == null) {
            String name = controllerClass.getName();
            {
                int idx = name.lastIndexOf('.');
                if (idx > 0) {
                    name = name.substring(idx + 1);
                }
            }

            try (InputStream is = controllerClass.getResourceAsStream(name + ".class")) {
                if (is == null) {
                    throw new FileNotFoundException(name + ".class");
                }
                ClassReader cr = new ClassReader(is);
                node = new ClassNode();
                cr.accept(node, ClassReader.SKIP_FRAMES);
                classNodeCache.put(controllerClass, node);
            } catch (IOException e) {
                log.warn("Can't read bytecode of class " + name);
                return null;
            }
        }

        if (parameters == null) {
            parameters = new Parameter[parameterCount];
        } else if (parameters.length != parameterCount) {
            throw new IllegalArgumentException(
                    "Input parameters' length is different than parameters of target");
        }

        boolean methodNodeGot = false;
        for (MethodNode m : (List<MethodNode>) node.methods) {
            if (methodName.equals(m.name)) {
                String[] names = new String[parameterCount];
                // do it
                {
                    IdentityHashMap<LabelNode, Integer> labelPosMap = new IdentityHashMap<>();
                    int pos = 0;
                    InsnList il = m.instructions;
                    for (int i = 0, max = il.size(); i < max; i++) {
                        AbstractInsnNode ain = il.get(i);
                        if (ain.getType() == AbstractInsnNode.LABEL) {
                            LabelNode label = (LabelNode) ain;
                            labelPosMap.put(label, pos++);
                        }
                    }
                    int[] poses = new int[parameterCount];
                    for (int i = 0; i < parameterCount; i++) {
                        poses[i] = Integer.MAX_VALUE;
                    }

                    for (LocalVariableNode lvn : (List<LocalVariableNode>) m.localVariables) {
                        if (lvn.index > 0 && lvn.index <= parameterCount) {
                            int idx = lvn.index - 1;
                            int startPos = labelPosMap.get(lvn.start);
                            if (poses[idx] > startPos) {
                                poses[idx] = startPos;
                                names[idx] = lvn.name;
                            }
                        }
                    }
                }

                if (log.isDebugEnabled()) {
                    log.debug("Method {}.{} has parameter: {}", controllerClass.getName(), method.getName(),
                            Arrays.toString(names));
                }
                for (int i = 0; i < parameterCount; i++) {
                    if (parameters[i] == null || parameters[i].getName() == null) {
                        parameters[i] = new Parameter(names[i], parameterTypes[i]);
                    }
                }

                methodNodeGot = true;
                break;
            }
        }
        if (!methodNodeGot) {
            throw new IllegalStateException("Can't get method " + method.getName() + " from ASM");
        }
        return parameters;
    }
}