Search in sources :

Example 56 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project dex2jar by pxb1988.

the class Dex2jarMultiThreadCmd method run0.

private void run0(String fileName, final ExecutorService executorService) throws IOException {
    // long baseTS = System.currentTimeMillis();
    String baseName = getBaseName(new File(fileName).toPath());
    Path currentDir = new File(".").toPath();
    Path file = currentDir.resolve(baseName + "-dex2jar.jar");
    final Path errorFile = currentDir.resolve(baseName + "-error.zip");
    System.err.println("dex2jar " + fileName + " -> " + file);
    final BaksmaliBaseDexExceptionHandler exceptionHandler = new BaksmaliBaseDexExceptionHandler();
    BaseDexFileReader reader = MultiDexFileReader.open(Files.readAllBytes(new File(fileName).toPath()));
    DexFileNode fileNode = new DexFileNode();
    try {
        reader.accept(fileNode, DexFileReader.SKIP_DEBUG | DexFileReader.IGNORE_READ_EXCEPTION);
    } catch (Exception ex) {
        exceptionHandler.handleFileException(ex);
        throw ex;
    }
    final FileSystem fs = createZip(file);
    final Path dist = fs.getPath("/");
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String name) {
            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw);
            return new ClassVisitor(Opcodes.ASM4, rca) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    String className = rca.getClassName();
                    byte[] data;
                    try {
                        // FIXME handle 'java.lang.RuntimeException: Method code too large!'
                        data = cw.toByteArray();
                    } catch (Exception ex) {
                        System.err.println(String.format("ASM fail to generate .class file: %s", className));
                        exceptionHandler.handleFileException(ex);
                        return;
                    }
                    try {
                        Path dist1 = dist.resolve(className + ".class");
                        BaseCmd.createParentDirectories(dist1);
                        Files.write(dist1, data);
                    } catch (IOException e) {
                        exceptionHandler.handleFileException(e);
                    }
                }
            };
        }
    };
    new ExDex2Asm(exceptionHandler) {

        @Override
        public void convertDex(final DexFileNode fileNode, final ClassVisitorFactory cvf) {
            if (fileNode.clzs != null) {
                final Map<String, Clz> classes = collectClzInfo(fileNode);
                final List<Future<?>> results = new ArrayList<>(fileNode.clzs.size());
                for (final DexClassNode classNode : fileNode.clzs) {
                    results.add(executorService.submit(new Runnable() {

                        @Override
                        public void run() {
                            convertClass(fileNode, classNode, cvf, classes);
                        }
                    }));
                }
                executorService.submit(new Runnable() {

                    @Override
                    public void run() {
                        for (Future<?> result : results) {
                            try {
                                result.get();
                            } catch (InterruptedException | ExecutionException e) {
                                e.printStackTrace();
                            }
                        }
                        BaksmaliBaseDexExceptionHandler exceptionHandler1 = (BaksmaliBaseDexExceptionHandler) exceptionHandler;
                        if (exceptionHandler1.hasException()) {
                            exceptionHandler1.dump(errorFile, new String[0]);
                        }
                        try {
                            fs.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }.convertDex(fileNode, cvf);
}
Also used : LambadaNameSafeClassAdapter(com.googlecode.d2j.dex.LambadaNameSafeClassAdapter) ClassVisitor(org.objectweb.asm.ClassVisitor) DexClassNode(com.googlecode.d2j.node.DexClassNode) FileSystem(java.nio.file.FileSystem) ExDex2Asm(com.googlecode.d2j.dex.ExDex2Asm) Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) DexFileNode(com.googlecode.d2j.node.DexFileNode) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) BaseDexFileReader(com.googlecode.d2j.reader.BaseDexFileReader) File(java.io.File)

Example 57 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project groovy-core by groovy.

the class MetaClassImpl method getClassNode.

/**
 * Obtains a reference to the original AST for the MetaClass if it is available at runtime
 *
 * @return The original AST or null if it cannot be returned
 */
public ClassNode getClassNode() {
    if (classNode == null && GroovyObject.class.isAssignableFrom(theClass)) {
        // let's try load it from the classpath
        String groovyFile = theClass.getName();
        int idx = groovyFile.indexOf('$');
        if (idx > 0) {
            groovyFile = groovyFile.substring(0, idx);
        }
        groovyFile = groovyFile.replace('.', '/') + ".groovy";
        // System.out.println("Attempting to load: " + groovyFile);
        URL url = theClass.getClassLoader().getResource(groovyFile);
        if (url == null) {
            url = Thread.currentThread().getContextClassLoader().getResource(groovyFile);
        }
        if (url != null) {
            try {
                /**
                 * todo there is no CompileUnit in scope so class name
                 * checking won't work but that mostly affects the bytecode
                 * generation rather than viewing the AST
                 */
                CompilationUnit.ClassgenCallback search = new CompilationUnit.ClassgenCallback() {

                    public void call(ClassVisitor writer, ClassNode node) {
                        if (node.getName().equals(theClass.getName())) {
                            MetaClassImpl.this.classNode = node;
                        }
                    }
                };
                final ClassLoader parent = theClass.getClassLoader();
                CompilationUnit unit = new CompilationUnit();
                unit.setClassgenCallback(search);
                unit.addSource(url);
                unit.compile(Phases.CLASS_GENERATION);
            } catch (Exception e) {
                throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
            }
        }
    }
    return classNode;
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassVisitor(org.objectweb.asm.ClassVisitor) URL(java.net.URL) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) IntrospectionException(java.beans.IntrospectionException) PrivilegedActionException(java.security.PrivilegedActionException) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException)

Example 58 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project gwt-test-utils by gwt-test-utils.

the class OverlayTypesRewriter method rewrite.

/**
 * Performs rewriting transformations on a class.
 *
 * @param className  the name of the class
 * @param classBytes the bytes of the class
 */
public byte[] rewrite(String className, byte[] classBytes) {
    Event classBytesRewriteEvent = SpeedTracerLogger.start(DevModeEventType.CLASS_BYTES_REWRITE, "Class Name", className);
    String desc = toDescriptor(className);
    assert !jsoIntfDescs.contains(desc);
    // The ASM model is to chain a bunch of visitors together.
    ClassWriter writer = new ClassWriter(0);
    ClassVisitor v = writer;
    // v = new CheckClassAdapter(v);
    // v = new TraceClassVisitor(v, new PrintWriter(System.out));
    v = new UseMirroredClasses(v, className);
    v = new RewriteSingleJsoImplDispatches(v, typeOracle, jsoData);
    v = new RewriteRefsToJsoClasses(v, jsoIntfDescs, mapper);
    if (jsoImplDescs.contains(desc)) {
        v = WriteJsoImpl.create(v, desc, jsoIntfDescs, mapper, jsoData);
    }
    if (Double.parseDouble(System.getProperty("java.class.version")) < Opcodes.V1_6) {
        v = new ForceClassVersion15(v);
    }
    new ClassReader(classBytes).accept(v, 0);
    classBytesRewriteEvent.end();
    return writer.toByteArray();
}
Also used : Event(com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 59 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project AndroidUtilCode by Blankj.

the class ApiTest method inject2ApiUtils.

private static void inject2ApiUtils(Map<String, ApiInfo> apiImpls) throws IOException {
    ClassReader cr = new ClassReader(ApiUtils.class.getName());
    ClassWriter cw = new ClassWriter(cr, 0);
    ClassVisitor cv = new ApiUtilsClassVisitor(cw, apiImpls, ApiUtils.class.getCanonicalName());
    cr.accept(cv, ClassReader.SKIP_FRAMES);
    FileUtils.writeByteArrayToFile(new File("ApiUtils2333.class"), cw.toByteArray());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

Example 60 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project AndroidUtilCode by Blankj.

the class BusTest method inject2BusUtils.

private static void inject2BusUtils(Map<String, List<BusInfo>> busMap) throws IOException {
    ClassReader cr = new ClassReader(BusUtils.class.getName());
    ClassWriter cw = new ClassWriter(cr, 0);
    ClassVisitor cv = new BusUtilsClassVisitor(cw, busMap, BusUtils.class.getName());
    cr.accept(cv, ClassReader.SKIP_FRAMES);
    FileUtils.writeByteArrayToFile(new File("BusUtils2333.class"), cw.toByteArray());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)133 ClassReader (org.objectweb.asm.ClassReader)97 ClassWriter (org.objectweb.asm.ClassWriter)80 MethodVisitor (org.objectweb.asm.MethodVisitor)45 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 Type (org.objectweb.asm.Type)14 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)14 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)10 Label (org.objectweb.asm.Label)10 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)9 FileOutputStream (java.io.FileOutputStream)6 FieldVisitor (org.objectweb.asm.FieldVisitor)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 FieldList (net.bytebuddy.description.field.FieldList)5