Search in sources :

Example 1 with JarTypeLoader

use of com.strobel.assembler.metadata.JarTypeLoader in project bytecode-viewer by Konloch.

the class ProcyonDecompiler method doSaveJarDecompiled.

/**
 * @author DeathMarine
 */
private void doSaveJarDecompiled(File inFile, File outFile) throws Exception {
    try (JarFile jfile = new JarFile(inFile);
        FileOutputStream dest = new FileOutputStream(outFile);
        BufferedOutputStream buffDest = new BufferedOutputStream(dest);
        ZipOutputStream out = new ZipOutputStream(buffDest)) {
        byte[] data = new byte[1024];
        DecompilerSettings settings = getDecompilerSettings();
        LuytenTypeLoader typeLoader = new LuytenTypeLoader();
        MetadataSystem metadataSystem = new MetadataSystem(typeLoader);
        ITypeLoader jarLoader = new JarTypeLoader(jfile);
        typeLoader.getTypeLoaders().add(jarLoader);
        DecompilationOptions decompilationOptions = new DecompilationOptions();
        decompilationOptions.setSettings(settings);
        decompilationOptions.setFullDecompilation(true);
        Enumeration<JarEntry> ent = jfile.entries();
        Set<JarEntry> history = new HashSet<>();
        while (ent.hasMoreElements()) {
            JarEntry entry = ent.nextElement();
            if (entry.getName().endsWith(".class")) {
                JarEntry etn = new JarEntry(entry.getName().replace(".class", ".java"));
                if (history.add(etn)) {
                    out.putNextEntry(etn);
                    try {
                        String internalName = StringUtilities.removeRight(entry.getName(), ".class");
                        TypeReference type = metadataSystem.lookupType(internalName);
                        TypeDefinition resolvedType;
                        if ((type == null) || ((resolvedType = type.resolve()) == null)) {
                            throw new Exception("Unable to resolve type.");
                        }
                        Writer writer = new OutputStreamWriter(out);
                        settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(writer), decompilationOptions);
                        writer.flush();
                    } finally {
                        out.closeEntry();
                    }
                }
            } else {
                try {
                    JarEntry etn = new JarEntry(entry.getName());
                    if (history.add(etn))
                        continue;
                    history.add(etn);
                    out.putNextEntry(etn);
                    try (InputStream in = jfile.getInputStream(entry)) {
                        if (in != null) {
                            int count;
                            while ((count = in.read(data, 0, 1024)) != -1) {
                                out.write(data, 0, count);
                            }
                        }
                    } finally {
                        out.closeEntry();
                    }
                } catch (ZipException ze) {
                    // some jars contain duplicate pom.xml entries: ignore it
                    if (!ze.getMessage().contains("duplicate")) {
                        throw ze;
                    }
                }
            }
        }
    }
}
Also used : PlainTextOutput(com.strobel.decompiler.PlainTextOutput) JarTypeLoader(com.strobel.assembler.metadata.JarTypeLoader) TypeDefinition(com.strobel.assembler.metadata.TypeDefinition) TypeReference(com.strobel.assembler.metadata.TypeReference) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet) DecompilationOptions(com.strobel.decompiler.DecompilationOptions) InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) MetadataSystem(com.strobel.assembler.metadata.MetadataSystem) JarEntry(java.util.jar.JarEntry) ITypeLoader(com.strobel.assembler.metadata.ITypeLoader) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) DecompilerSettings(com.strobel.decompiler.DecompilerSettings) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 2 with JarTypeLoader

use of com.strobel.assembler.metadata.JarTypeLoader in project j2objc by google.

the class ClassFile method create.

public static ClassFile create(InputFile file) throws IOException {
    ITypeLoader loader;
    String path = file.getAbsolutePath();
    if (path.endsWith(".jar")) {
        loader = new JarTypeLoader(new JarFile(path));
        path = file.getUnitName();
        if (!path.endsWith(".class")) {
            return null;
        }
        // Remove .class suffix, as JarTypeLoader adds it.
        path = path.substring(0, path.length() - 6);
    } else {
        loader = new InputTypeLoader();
    }
    TypeReference typeRef = lookupType(path, loader);
    CompilationUnit unit = decompileClassFile(typeRef);
    return new ClassFile(unit, typeRef);
}
Also used : InputTypeLoader(com.strobel.assembler.InputTypeLoader) CompilationUnit(com.strobel.decompiler.languages.java.ast.CompilationUnit) JarTypeLoader(com.strobel.assembler.metadata.JarTypeLoader) TypeReference(com.strobel.assembler.metadata.TypeReference) JarFile(java.util.jar.JarFile) ITypeLoader(com.strobel.assembler.metadata.ITypeLoader)

Aggregations

ITypeLoader (com.strobel.assembler.metadata.ITypeLoader)2 JarTypeLoader (com.strobel.assembler.metadata.JarTypeLoader)2 TypeReference (com.strobel.assembler.metadata.TypeReference)2 JarFile (java.util.jar.JarFile)2 InputTypeLoader (com.strobel.assembler.InputTypeLoader)1 MetadataSystem (com.strobel.assembler.metadata.MetadataSystem)1 TypeDefinition (com.strobel.assembler.metadata.TypeDefinition)1 DecompilationOptions (com.strobel.decompiler.DecompilationOptions)1 DecompilerSettings (com.strobel.decompiler.DecompilerSettings)1 PlainTextOutput (com.strobel.decompiler.PlainTextOutput)1 CompilationUnit (com.strobel.decompiler.languages.java.ast.CompilationUnit)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1