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

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

Introduction

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

Prototype

public void accept(final MethodVisitor methodVisitor) 

Source Link

Document

Makes the given method visitor visit this method.

Usage

From source file:pxb.android.dex2jar.v3.V3MethodAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
public void visitEnd() {
    build();//w ww .ja v a 2  s .c o m
    MethodNode methodNode = this.methodNode;

    try {
        if (methodNode.instructions.size() > 2) {
            List<? extends MethodTransformer> trs = Arrays.asList(new B(), new C(method));
            for (MethodTransformer tr : trs) {
                // TraceMethodVisitor tmv = new TraceMethodVisitor();
                // methodNode.instructions.accept(tmv);
                // StringBuilder sb=new StringBuilder();
                // int i=0;
                // for(Object o:tmv.text){
                // sb.append(i++).append(o);
                // }
                // System.out.println(sb);
                tr.transform(methodNode);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error transform method:" + this.method, e);
    }

    MethodVisitor mv = cv.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    if (mv != null) {
        try {
            methodNode.accept(new LdcOptimizeAdapter(mv));
        } catch (Exception e) {
            throw new RuntimeException("Error visit method:" + this.method, e);
        }
    }
}

From source file:rubah.bytecode.transformers.ProcessUpdateClass.java

License:Open Source License

public static void generateConversionMethod(Version v1, Clazz c1, ConvertMethodGenerator[] generators) {
    Clazz c0 = v1.getUpdate().getV0(c1);

    for (ConvertMethodGenerator generator : generators) {
        MethodVisitor mv = generator.getMethodVisitor();

        if (mv == null) {
            continue;
        }// w  w  w .  j a v  a2s  .  c o  m

        mv.visitCode();

        if (c1.getParent().getNamespace().equals(c1.getNamespace())) {
            // Convert parent, if updatable
            generator.callSuper(mv, c1.getParent());
        } else {
            // Copy all fields from parent, if non-updatable
            generator.copyAllFields(mv, c1.getParent());
        }

        HashSet<Field> interestingFields = new HashSet<Field>();
        for (Field f : c1.getFields()) {
            Change<Field> change = v1.getUpdate().getChanges(c0).getFieldChanges().get(f);
            if (generator.isFieldInteresting(f, c1) && (change == null || change.getChangeSet().isEmpty())) {
                interestingFields.add(f);
                // Copy unchanged fields
                generator.convertField(mv, f);
            }
        }

        MethodNode mn = generator.getMethodNode();

        if (mn != null) {
            // Add custom conversion code to end of conversion method
            mn.instructions.resetLabels();
            mn.accept(new ProcessUpdateMethod(v1, generator, interestingFields, mv));
            continue;
        }

        mv.visitInsn(RETURN);
        mv.visitEnd();
        mv.visitMaxs(0, 0);
    }
}

From source file:sidechecker.SideCheckerTransformer.java

public void CheckClassForProperSiding(String s, String s2, byte[] bytes) {
    if (needsInit) {
        init();/*from  w  w w  .j  a v a2s .  c o m*/
    }

    if (s == null || bytes == null) //should never happen
        return;

    for (String exception : exceptions) {
        if (s.startsWith(exception))
            return;
    }

    if (filter != null) {
        if (!s.startsWith(filter)) // basic hack to allow users to filter out other mods
            return;
    } else {
        boolean flag = false; // is the class from a 'directory' class
        final String replace = s.replace('.', '\\') + ".class";
        for (File f : files) {
            File t = new File(f, replace);
            if (t.exists()) {
                flag = true;
                break;
            }
        }

        if (!flag)
            return;
    }

    ClassNode classNode = new ClassNode();
    ClassReader reader = new ClassReader(bytes);

    reader.accept(classNode, 0);

    curClass = s;
    curFile = classNode.sourceFile;
    curMethod = "";

    ClassInfo.registerClass(s, bytes);

    if (hasClientAnnotation(classNode.visibleAnnotations))
        return; // class is client-side and has a license to be so

    // inner class handling
    if (classNode.outerClass != null) {
        if (classNode.outerMethod != null && classNode.outerMethodDesc != null) {
            if (ClassInfo.hasClientMethod(classNode.outerClass, classNode.outerMethod,
                    classNode.outerMethodDesc))
                return;
        } else if (ClassInfo.isClientClass(classNode.outerClass)) {
            return;
        }
    }

    if (classNode.superName != null && ClassInfo.isClientClass(classNode.superName)) {
        logger.info("----------------------------------------------------------------");
        logger.info("Error: Class " + s + " extends client-side class " + classNode.superName
                + " but does not include annotation");
        logger.info(log("Class: " + s, "class", 1));
        logger.info("----------------------------------------------------------------");
        if (crashOnSeriousError)
            throwException();

        return;
    }

    for (String interfaces : classNode.interfaces) {
        if (ClassInfo.isClientClass(interfaces)) {
            logger.info("----------------------------------------------------------------");
            logger.info("Error: Class " + s + " extends client-side class " + classNode.superName
                    + " but does not include annotation");
            logger.info(log("Class: " + s, "class", 1));
            logger.info("----------------------------------------------------------------");
            if (crashOnSeriousError)
                throwException();

            return;
        }
    }

    for (MethodNode method : classNode.methods) {
        curMethod = method.name;
        if (shouldProcess(method.visibleAnnotations)) {
            if (ClassInfo.hasClientMethod(classNode.superName, method.name, method.desc)) {
                int line = -1;
                for (AbstractInsnNode instruction : method.instructions.toArray()) {
                    if (instruction.getType() == AbstractInsnNode.LINE) {
                        line = ((LineNumberNode) instruction).line;
                        break;
                    }
                }
                warnings.add(log("Method: ", method.name, line));
            }

            method.accept(ClientCheckerMethodVisitor.instance);
        }
    }

    for (FieldNode fieldNode : classNode.fields) {
        if (!hasClientAnnotation(fieldNode.visibleAnnotations)) {
            if (fieldNode.desc.startsWith("L"))
                if (ClassInfo.isClientClass(stripToType(fieldNode.desc))) {
                    errors.add(log("Field Type: ", fieldNode.desc, 1));
                }
        }
    }

    boolean crash = false;

    if (!warnings.isEmpty() || !errors.isEmpty()) {
        logger.info("----------------------------------------------------------------");
        if (!warnings.isEmpty()) {
            logger.info("Warning: Class " + s
                    + " overrides client-side methods and does not include the SideOnly annotation");
            for (String method : warnings) {
                logger.info(method);
            }

            if (crashOnWarning)
                crash = true;
            warnings.clear();
        }

        if (!errors.isEmpty()) {
            logger.info("Error: Class " + s
                    + " has references to client-side code in non-client-side fields/methods");

            for (String problem : errors) {
                logger.info(problem);
            }
            errors.clear();

            if (crashOnSeriousError)
                crash = true;
        }
        logger.info("----------------------------------------------------------------");
    }

    if (crash)
        throwException();
}