Search in sources :

Example 1 with DecompilerSettings

use of com.strobel.decompiler.DecompilerSettings 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 DecompilerSettings

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

the class ProcyonDecompiler method getDecompilerSettings.

public DecompilerSettings getDecompilerSettings() {
    CommandLineOptions options = new CommandLineOptions();
    JCommander jCommander = new JCommander(options);
    String[] args = new String[Settings.values().length * 2];
    int index = 0;
    for (the.bytecode.club.bytecodeviewer.DecompilerSettings.Setting setting : Settings.values()) {
        args[index++] = "--" + setting.getParam();
        args[index++] = String.valueOf(getSettings().isSelected(setting));
    }
    jCommander.parse(args);
    DecompilerSettings settings = new DecompilerSettings();
    settings.setFlattenSwitchBlocks(options.getFlattenSwitchBlocks());
    settings.setForceExplicitImports(!options.getCollapseImports());
    settings.setForceExplicitTypeArguments(options.getForceExplicitTypeArguments());
    settings.setRetainRedundantCasts(options.getRetainRedundantCasts());
    settings.setShowSyntheticMembers(options.getShowSyntheticMembers());
    settings.setExcludeNestedTypes(options.getExcludeNestedTypes());
    settings.setOutputDirectory(options.getOutputDirectory());
    settings.setIncludeLineNumbersInBytecode(options.getIncludeLineNumbers());
    settings.setRetainPointlessSwitches(options.getRetainPointlessSwitches());
    settings.setUnicodeOutputEnabled(options.isUnicodeOutputEnabled());
    settings.setMergeVariables(options.getMergeVariables());
    settings.setShowDebugLineNumbers(options.getShowDebugLineNumbers());
    settings.setSimplifyMemberReferences(options.getSimplifyMemberReferences());
    settings.setDisableForEachTransforms(options.getDisableForEachTransforms());
    settings.setTypeLoader(new InputTypeLoader());
    if (options.isRawBytecode()) {
        settings.setLanguage(Languages.bytecode());
    } else if (options.isBytecodeAst()) {
        settings.setLanguage(options.isUnoptimized() ? Languages.bytecodeAstUnoptimized() : Languages.bytecodeAst());
    }
    return settings;
}
Also used : InputTypeLoader(com.strobel.assembler.InputTypeLoader) CommandLineOptions(com.strobel.decompiler.CommandLineOptions) JCommander(com.beust.jcommander.JCommander) DecompilerSettings(com.strobel.decompiler.DecompilerSettings)

Example 3 with DecompilerSettings

use of com.strobel.decompiler.DecompilerSettings 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

DecompilerSettings (com.strobel.decompiler.DecompilerSettings)3 InputTypeLoader (com.strobel.assembler.InputTypeLoader)2 DecompilationOptions (com.strobel.decompiler.DecompilationOptions)2 PlainTextOutput (com.strobel.decompiler.PlainTextOutput)2 ZipException (java.util.zip.ZipException)2 JCommander (com.beust.jcommander.JCommander)1 CommandLineOptions (com.strobel.decompiler.CommandLineOptions)1 HashSet (java.util.HashSet)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1