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(final int api) 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:br.usp.each.saeg.badua.cli.Report.java

License:Open Source License

public void run() throws IOException, ClassNotFoundException {

    data = read(inputFile);/*from  w w w  .j av a  2 s  .c o m*/

    final List<File> files = Files.listRecursive(classes, new FilenameFilter() {

        @Override
        public boolean accept(final File dir, final String name) {
            return new File(dir, name).isFile();
        }

    });

    for (final File file : files) {
        final InputStream input = new FileInputStream(file);
        final ContentTypeDetector detector = new ContentTypeDetector(input);
        try {
            if (detector.getType() == ContentTypeDetector.CLASSFILE) {
                final ClassReader cr = new ClassReader(detector.getInputStream());
                final ClassNode cn = new ClassNode(Opcodes.ASM5);
                cr.accept(cn, ClassReader.EXPAND_FRAMES);
                classId = CRC64.checksum(cr.b);
                analyze(cn);
            }
        } finally {
            input.close();
        }
    }
}

From source file:com.android.build.gradle.integration.common.utils.ZipHelper.java

License:Apache License

public static FieldNode checkClassFile(@NonNull File jarFile, @NonNull String entryName,
        @NonNull String fieldName) throws IOException {

    try (ZipFile zipFile = new ZipFile(jarFile)) {
        ZipEntry entry = zipFile.getEntry(entryName);
        assertThat(entry).named(entryName + " entry").isNotNull();
        ClassReader classReader = new ClassReader(zipFile.getInputStream(entry));
        ClassNode mainTestClassNode = new ClassNode(Opcodes.ASM5);
        classReader.accept(mainTestClassNode, 0);

        FieldNode fieldNode = null;/*  w  w  w.  j  a  v a2 s.  c o m*/
        for (Object o : mainTestClassNode.fields) {
            FieldNode fn = (FieldNode) o;
            if (fn.name.equals(fieldName)) {
                fieldNode = fn;
                break;
            }
        }

        assert fieldNode != null;
        return fieldNode;
    }
}

From source file:com.android.build.gradle.shrinker.AbstractShrinkerTest.java

License:Apache License

@NonNull
private static ClassNode getClassNode(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);
    return classNode;
}

From source file:com.android.build.gradle.shrinker.FullRunShrinker.java

License:Apache License

/** Updates the graph with nodes and edges based on the given class file. */
private void processProgramClassFile(byte[] bytes, @NonNull File classFile,
        @NonNull final PostProcessingData<T> postProcessingData) {
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    ClassVisitor depsFinder = new DependencyFinderVisitor<T>(mGraph, classNode) {
        @Override//from   w w w .j  a va  2  s .  c  o  m
        protected void handleDependency(T source, T target, DependencyType type) {
            mGraph.addDependency(source, target, type);
        }

        @Override
        protected void handleMultipleInheritance(T klass) {
            postProcessingData.getMultipleInheritance().add(klass);
        }

        @Override
        protected void handleVirtualMethod(T method) {
            postProcessingData.getVirtualMethods().add(method);
        }

        @Override
        protected void handleInterfaceInheritance(T klass) {
            postProcessingData.getInterfaceInheritance().add(klass);
        }

        @Override
        protected void handleUnresolvedReference(PostProcessingData.UnresolvedReference<T> reference) {
            postProcessingData.getUnresolvedReferences().add(reference);
        }
    };
    ClassVisitor structureVisitor = new ClassStructureVisitor<>(mGraph, classFile, depsFinder);
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(structureVisitor, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
}

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 ww  w .j  a  va 2 s .  c o m*/
    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/*  ww w. j av  a 2  s.com*/
        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);
    }/*from   w w w.  ja  v a  2  s .c  o  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 . java 2 s.  c o m*/
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.costlowcorp.eriktools.jardetails.JarNavigationController.java

private void readClass(String filename) {
    try (JarFile jar = new JarFile(this.file)) {
        final JarEntry entry = jar.getJarEntry(filename);
        final ClassNode node = new ClassNode(ASM5);
        final InputStream in = jar.getInputStream(entry);
        final ClassReader reader = new ClassReader(in);
        //reader.accept(node, ClassReader.EXPAND_FRAMES);

        final Printer asmifier = new ASMifier();
        //final InputStream in2 = jar.getInputStream(entry);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();

        final TraceClassVisitor tracer = new TraceClassVisitor(node, asmifier, new PrintWriter(bout));
        reader.accept(tracer, ClassReader.EXPAND_FRAMES);
        final String className = filename.replace('/', '.').substring(0, filename.length() - ".class".length());
        final String javap = JavaPGrabber.INSTANCE.run(file.toPath(), className);
        Platform.runLater(() -> {/*w  w  w.  j  av a2s . c  o m*/
            classDetails.populateClassSummary(node.name, filename, node.sourceFile, node.version,
                    node.superName, node.interfaces);
            classDetails.populateAsmifierTab(bout.toString());
            classDetails.populateJavaPTab(javap);
        });
    } catch (IOException ex) {
        Logger.getLogger(JarNavigationController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.dragome.compiler.invokedynamic.InvokeDynamicBackporter.java

License:Open Source License

public static byte[] transform(byte[] bytecode) {
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    InvokeDynamicConverter invokeDynamicConverter = new InvokeDynamicConverter(classNode);
    new ClassReader(bytecode).accept(invokeDynamicConverter, 0);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);/*from  w w w. j a  v  a2s.c o m*/
    return cw.toByteArray();
}