Example usage for org.objectweb.asm Opcodes ASM5

List of usage examples for org.objectweb.asm Opcodes ASM5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM5.

Prototype

int ASM5

To view the source code for org.objectweb.asm Opcodes ASM5.

Click Source Link

Usage

From source file:com.android.builder.shrinker.ClassStructureVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    final T method = mGraph.addMember(mClass, name, desc, access);

    MethodVisitor superVisitor = super.visitMethod(access, name, desc, signature, exceptions);
    return new MethodVisitor(Opcodes.ASM5, superVisitor) {
        @Override/*  w w  w.  j a  v  a 2 s . c  o  m*/
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            mGraph.addAnnotation(method, desc);
            return super.visitAnnotation(desc, visible);
        }
    };
}

From source file:com.android.builder.shrinker.ClassStructureVisitor.java

License:Apache License

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    final T field = mGraph.addMember(mClass, name, desc, access);

    FieldVisitor superVisitor = super.visitField(access, name, desc, signature, value);
    return new FieldVisitor(Opcodes.ASM5, superVisitor) {
        @Override//from   w ww .  j  a va 2 s  . c om
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            mGraph.addAnnotation(field, desc);
            return super.visitAnnotation(desc, visible);
        }
    };
}

From source file:com.android.builder.shrinker.DependencyFinderVisitor.java

License:Apache License

public DependencyFinderVisitor(ShrinkerGraph<T> graph, ClassVisitor cv, Set<T> virtualMethods,
        Set<Shrinker.UnresolvedReference<T>> unresolvedReferences, Set<T> multipleInheritance) {
    super(Opcodes.ASM5, cv);
    mGraph = graph;/*from w ww . ja  v  a  2s  .  com*/
    mVirtualMethods = virtualMethods;
    mUnresolvedReferences = unresolvedReferences;
    mMultipleInheritance = multipleInheritance;
}

From source file:com.android.builder.shrinker.FilterMembersVisitor.java

License:Apache License

public FilterMembersVisitor(Set<String> members, Predicate<String> keepInterface, ClassVisitor cv) {
    super(Opcodes.ASM5, cv);
    mMembers = members;//  w w  w. jav a  2  s.  co  m
    mKeepInterface = keepInterface;
}

From source file:com.android.builder.shrinker.Shrinker.java

License:Apache License

@NonNull
private static ClassNode readClassNode(File classFile) throws IOException {
    ClassReader classReader = new ClassReader(Files.toByteArray(classFile));
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    classReader.accept(classNode, 0);/*from  w ww .  ja va  2s.  c om*/
    return classNode;
}

From source file:com.android.builder.shrinker.Shrinker.java

License:Apache License

private void processNewClassFile(File classFile, Set<T> virtualMethods, Set<T> multipleInheritance,
        Set<UnresolvedReference<T>> unresolvedReferences) throws IOException {
    // TODO: Can we run keep rules in a visitor?
    // TODO: See if computing all these things on the class nodes is faster (on big projects).
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    ClassVisitor depsFinder = new DependencyFinderVisitor<T>(mGraph, classNode, virtualMethods,
            unresolvedReferences, multipleInheritance) {
        @Override//from www.  j  a v a  2  s.c  o  m
        protected void handleDependency(T source, T target, DependencyType type) {
            mGraph.addDependency(source, target, type);
        }
    };
    ClassVisitor structureVisitor = new ClassStructureVisitor<T>(mGraph, classFile, depsFinder);
    ClassReader classReader = new ClassReader(Files.toByteArray(classFile));
    classReader.accept(structureVisitor, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
}

From source file:com.android.builder.shrinker.ShrinkerTest.java

License:Apache License

private static Set<String> getMembers(File classFile) throws IOException {
    ClassReader classReader = new ClassReader(Files.toByteArray(classFile));
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    classReader.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    Set<String> methods = Sets.newHashSet();
    //noinspection unchecked - ASM API
    for (MethodNode methodNode : (List<MethodNode>) classNode.methods) {
        methods.add(methodNode.name + ":" + methodNode.desc);
    }/*  w w  w .jav a  2s  . co m*/

    //noinspection unchecked - ASM API
    for (FieldNode fieldNode : (List<FieldNode>) classNode.fields) {
        methods.add(fieldNode.name + ":" + fieldNode.desc);
    }

    return methods;
}

From source file:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

/**
 * Writes a modified *.class file to the output JAR file.
 *///from  ww w . j  a  v a 2s .  c om
private void rewriteClass(JarEntry entry, InputStream inputStream, JarOutputStream outputStream)
        throws IOException {
    ClassReader classReader = new ClassReader(inputStream);
    ClassNode classNode = new ClassNode(Opcodes.ASM5);

    classReader.accept(classNode, EMPTY_FLAGS);

    modifyClass(classNode);

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(classWriter);

    outputStream.putNextEntry(new ZipEntry(entry.getName()));
    outputStream.write(classWriter.toByteArray());
}

From source file:com.android.ide.eclipse.adt.internal.resources.manager.ProjectClassLoader.java

License:Open Source License

/**
 * Rewrites the given class to the given target class file version.
 *//*from   w w  w.  j av  a2  s.c om*/
public static byte[] rewriteClass(byte[] classData, final int maxVersion, final int minVersion) {
    assert maxVersion >= minVersion;
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5, classWriter) {
        @Override
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            if (version > maxVersion) {
                version = maxVersion;
            }
            if (version < minVersion) {
                version = minVersion;
            }
            super.visit(version, access, name, signature, superName, interfaces);
        }
    };
    ClassReader reader = new ClassReader(classData);
    reader.accept(classVisitor, 0);
    return classWriter.toByteArray();
}

From source file:com.appfour.codestripper.Main.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage: java " + Main.class.getCanonicalName() + " <source dir> <dest file>");
        System.exit(1);//from  w w w . ja  v  a 2 s .  c o  m
    }

    File outFile = new File(args[1]);
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFile));
    zipOut.setLevel(9);
    Set<String> writtenEntries = new HashSet<String>();
    for (File file : new File(args[0]).listFiles()) {
        if (file.getName().toLowerCase(Locale.US).endsWith(".jar")) {
            System.err.println("Processing JAR: " + file.getPath());
            ZipInputStream zipIn = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null) {
                if (entry.isDirectory())
                    continue;
                if (entry.getName().toLowerCase(Locale.US).endsWith(".class")) {
                    if (writtenEntries.contains(entry.getName())) {
                        System.out.println("\tIgnoring duplicate CLASS: " + entry.getName());
                        continue;
                    }
                    writtenEntries.add(entry.getName());
                    System.out.println("\tProcessing CLASS: " + entry.getName());
                    ClassReader cr = new ClassReader(zipIn);
                    ClassWriter cw = new ClassWriter(0);
                    final boolean[] skip = new boolean[1];
                    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
                        @Override
                        public void visit(int version, int access, String name, String signature,
                                String superName, String[] interfaces) {
                            if (!INCLUDE_PACKAGE_PRIVATE_CLASSES
                                    && (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
                                skip[0] = true;
                            }
                            super.visit(version, access, name, signature, superName, interfaces);
                        }

                        @Override
                        public void visitSource(String source, String debug) {
                            // ignore
                        }

                        @Override
                        public FieldVisitor visitField(int access, String name, String desc, String signature,
                                Object value) {
                            if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0)
                                return null;
                            return super.visitField(access, name, desc, signature, value);
                        }

                        @Override
                        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                                String[] exceptions) {
                            if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0)
                                return null;
                            return new MethodVisitor(Opcodes.ASM5,
                                    super.visitMethod(access, name, desc, signature, exceptions)) {
                                @Override
                                public void visitCode() {
                                    // ignore
                                    super.visitCode();
                                    super.visitInsn(Opcodes.NOP);
                                }

                                @Override
                                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                                    // ignore
                                }

                                @Override
                                public void visitIincInsn(int var, int increment) {
                                    // ignore
                                }

                                @Override
                                public void visitFrame(int type, int nLocal, Object[] local, int nStack,
                                        Object[] stack) {
                                    // ignore
                                }

                                @Override
                                public void visitInsn(int opcode) {
                                    // ignore
                                }

                                @Override
                                public void visitJumpInsn(int opcode, Label label) {
                                    // ignore
                                }

                                @Override
                                public void visitLabel(Label label) {
                                    // ignore
                                }

                                @Override
                                public void visitLdcInsn(Object cst) {
                                    // ignore
                                }

                                @Override
                                public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
                                    // ignore
                                }

                                @Override
                                public void visitIntInsn(int opcode, int operand) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath,
                                        String desc, boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitInvokeDynamicInsn(String name, String desc, Handle bsm,
                                        Object... bsmArgs) {
                                    // ignore
                                }

                                @Override
                                public void visitMethodInsn(int opcode, String owner, String name,
                                        String desc) {
                                    // ignore
                                }

                                @Override
                                public void visitMultiANewArrayInsn(String desc, int dims) {
                                    // ignore
                                }

                                @Override
                                public void visitTableSwitchInsn(int min, int max, Label dflt,
                                        Label... labels) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath,
                                        String desc, boolean visible) {
                                    // ignore
                                    return null;
                                };

                                @Override
                                public void visitTryCatchBlock(Label start, Label end, Label handler,
                                        String type) {
                                    // ignore
                                }

                                @Override
                                public void visitLineNumber(int line, Label start) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
                                        TypePath typePath, Label[] start, Label[] end, int[] index, String desc,
                                        boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitParameter(String name, int access) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
                                        boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitTypeInsn(int opcode, String type) {
                                    // ignore
                                }

                                @Override
                                public void visitVarInsn(int opcode, int var) {
                                    // ignore
                                }

                            };
                        }

                        @Override
                        public void visitAttribute(Attribute attr) {
                            System.out.println("\t\tProcessing attr " + attr.type);
                            if (attr.isCodeAttribute())
                                return;

                            super.visitAttribute(attr);
                        }
                    };
                    cr.accept(cv, 0);
                    if (!skip[0]) {
                        byte[] b2 = cw.toByteArray(); // b2 represents the same class as b1
                        entry.setSize(b2.length);
                        entry.setCompressedSize(-1);
                        entry.setMethod(ZipEntry.DEFLATED);
                        zipOut.putNextEntry(entry);
                        new DataOutputStream(zipOut).write(b2);
                    }
                } else {
                    if (writtenEntries.contains(entry.getName())) {
                        System.out.println("\tIgnoring duplicate RESOURCE: " + entry.getName());
                        continue;
                    }
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    transfer(zipIn, bos, false);
                    writtenEntries.add(entry.getName());
                    System.out.println("\tProcessing RESOURCE: " + entry.getName());
                    entry.setSize(bos.size());
                    entry.setCompressedSize(-1);
                    entry.setMethod(ZipEntry.DEFLATED);
                    zipOut.putNextEntry(entry);
                    transfer(new ByteArrayInputStream(bos.toByteArray()), zipOut, false);
                }
            }
            zipIn.close();
        }
    }
    zipOut.close();
}