Search in sources :

Example 1 with DecompilationOptions

use of com.strobel.decompiler.DecompilationOptions 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();
        MetadataSystem metadataSystem = new MetadataSystem(new JarTypeLoader(jfile));
        DecompilationOptions decompilationOptions = new DecompilationOptions();
        decompilationOptions.setSettings(settings);
        decompilationOptions.setFullDecompilation(true);
        Enumeration<JarEntry> ent = jfile.entries();
        Set<JarEntry> history = new HashSet<JarEntry>();
        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 = null;
                        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) {
                            try {
                                int count;
                                while ((count = in.read(data, 0, 1024)) != -1) {
                                    out.write(data, 0, count);
                                }
                            } finally {
                                in.close();
                            }
                        }
                    } finally {
                        out.closeEntry();
                    }
                } catch (ZipException ze) {
                    // it
                    if (!ze.getMessage().contains("duplicate")) {
                        throw ze;
                    }
                }
            }
        }
    }
}
Also used : DecompilationOptions(com.strobel.decompiler.DecompilationOptions) PlainTextOutput(com.strobel.decompiler.PlainTextOutput) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ZipException(java.util.zip.ZipException) ZipOutputStream(java.util.zip.ZipOutputStream) DecompilerSettings(com.strobel.decompiler.DecompilerSettings) HashSet(java.util.HashSet)

Example 2 with DecompilationOptions

use of com.strobel.decompiler.DecompilationOptions 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 3 with DecompilationOptions

use of com.strobel.decompiler.DecompilationOptions in project bytecode-viewer by Konloch.

the class ProcyonDecompiler method decompileClassNode.

@Override
public String decompileClassNode(ClassNode cn, byte[] b) {
    String exception;
    try {
        String fileStart = tempDirectory + fs + "temp";
        final File tempClass = new File(MiscUtils.getUniqueName(fileStart, ".class") + ".class");
        try (FileOutputStream fos = new FileOutputStream(tempClass)) {
            fos.write(b);
        } catch (final IOException e) {
            BytecodeViewer.handleException(e);
        }
        DecompilerSettings settings = getDecompilerSettings();
        LuytenTypeLoader typeLoader = new LuytenTypeLoader();
        MetadataSystem metadataSystem = new MetadataSystem(typeLoader);
        TypeReference type = metadataSystem.lookupType(tempClass.getCanonicalPath());
        DecompilationOptions decompilationOptions = new DecompilationOptions();
        decompilationOptions.setSettings(settings);
        decompilationOptions.setFullDecompilation(true);
        TypeDefinition resolvedType;
        if (type == null || ((resolvedType = type.resolve()) == null))
            throw new Exception("Unable to resolve type.");
        StringWriter stringwriter = new StringWriter();
        settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
        return EncodeUtils.unicodeToString(stringwriter.toString());
    } catch (StackOverflowError | Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        e.printStackTrace();
        exception = ExceptionUI.SEND_STACKTRACE_TO_NL + sw;
    }
    return PROCYON + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + nl + nl + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + nl + nl + exception;
}
Also used : DecompilationOptions(com.strobel.decompiler.DecompilationOptions) PlainTextOutput(com.strobel.decompiler.PlainTextOutput) IOException(java.io.IOException) MetadataSystem(com.strobel.assembler.metadata.MetadataSystem) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) TypeDefinition(com.strobel.assembler.metadata.TypeDefinition) StringWriter(java.io.StringWriter) FileOutputStream(java.io.FileOutputStream) DecompilerSettings(com.strobel.decompiler.DecompilerSettings) TypeReference(com.strobel.assembler.metadata.TypeReference) JarFile(java.util.jar.JarFile) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with DecompilationOptions

use of com.strobel.decompiler.DecompilationOptions in project j2objc by google.

the class ClassFile method decompileClassFile.

private static CompilationUnit decompileClassFile(TypeReference typeRef) {
    TypeDefinition typeDef = typeRef.resolve();
    DeobfuscationUtilities.processType(typeDef);
    DecompilationOptions options = new DecompilationOptions();
    DecompilerSettings settings = DecompilerSettings.javaDefaults();
    settings.setShowSyntheticMembers(true);
    options.setSettings(settings);
    options.setFullDecompilation(true);
    return Languages.java().decompileTypeToAst(typeDef, options);
}
Also used : DecompilationOptions(com.strobel.decompiler.DecompilationOptions) DecompilerSettings(com.strobel.decompiler.DecompilerSettings) TypeDefinition(com.strobel.assembler.metadata.TypeDefinition)

Example 5 with DecompilationOptions

use of com.strobel.decompiler.DecompilationOptions in project bytecode-viewer by Konloch.

the class ProcyonDecompiler method decompileClassNode.

@Override
public String decompileClassNode(final ClassNode cn, byte[] b) {
    try {
        if (cn.version < 49) {
            b = fixBytes(b);
        }
        final byte[] bytesToUse = b;
        final Map<String, byte[]> loadedClasses = BytecodeViewer.getLoadedBytes();
        DecompilerSettings settings = getDecompilerSettings();
        MetadataSystem metadataSystem = new MetadataSystem(new ITypeLoader() {

            private InputTypeLoader backLoader = new InputTypeLoader();

            @Override
            public boolean tryLoadType(String s, Buffer buffer) {
                if (s.equals(cn.name)) {
                    buffer.putByteArray(bytesToUse, 0, bytesToUse.length);
                    buffer.position(0);
                    return true;
                } else {
                    byte[] toUse = loadedClasses.get(s + ".class");
                    if (toUse != null) {
                        buffer.putByteArray(toUse, 0, toUse.length);
                        buffer.position(0);
                        return true;
                    } else {
                        return backLoader.tryLoadType(s, buffer);
                    }
                }
            }
        });
        TypeReference type = metadataSystem.lookupType(cn.name);
        DecompilationOptions decompilationOptions = new DecompilationOptions();
        decompilationOptions.setSettings(DecompilerSettings.javaDefaults());
        decompilationOptions.setFullDecompilation(true);
        TypeDefinition resolvedType = null;
        if (type == null || ((resolvedType = type.resolve()) == null)) {
            throw new Exception("Unable to resolve type.");
        }
        StringWriter stringwriter = new StringWriter();
        settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
        String decompiledSource = stringwriter.toString();
        return decompiledSource;
    } catch (Throwable e) {
        return parseException(e);
    }
}
Also used : DecompilationOptions(com.strobel.decompiler.DecompilationOptions) PlainTextOutput(com.strobel.decompiler.PlainTextOutput) ZipException(java.util.zip.ZipException) InputTypeLoader(com.strobel.assembler.InputTypeLoader) DecompilerSettings(com.strobel.decompiler.DecompilerSettings)

Aggregations

DecompilationOptions (com.strobel.decompiler.DecompilationOptions)5 DecompilerSettings (com.strobel.decompiler.DecompilerSettings)5 PlainTextOutput (com.strobel.decompiler.PlainTextOutput)4 ZipException (java.util.zip.ZipException)4 TypeDefinition (com.strobel.assembler.metadata.TypeDefinition)3 JarFile (java.util.jar.JarFile)3 MetadataSystem (com.strobel.assembler.metadata.MetadataSystem)2 TypeReference (com.strobel.assembler.metadata.TypeReference)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 JarEntry (java.util.jar.JarEntry)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 InputTypeLoader (com.strobel.assembler.InputTypeLoader)1 ITypeLoader (com.strobel.assembler.metadata.ITypeLoader)1 JarTypeLoader (com.strobel.assembler.metadata.JarTypeLoader)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1