@Override public String decompileClassNode(ClassNode cn, byte[] b) { String exception = ""; try { String fileStart = BytecodeViewer.tempDirectory + BytecodeViewer.fs + "temp"; final File tempClass = new File(MiscUtils.getUniqueName(fileStart, ".class") + ".class"); try { final FileOutputStream fos = new FileOutputStream(tempClass); fos.write(b); fos.close(); } catch (final IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(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(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 (Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); e.printStackTrace(); exception = "Bytecode Viewer Version: " + BytecodeViewer.version + BytecodeViewer.nl + BytecodeViewer.nl + sw.toString(); } return "Procyon error! Send the stacktrace to Konloch at http://the.bytecode.club or [email protected]" + BytecodeViewer.nl + BytecodeViewer.nl + "Suggested Fix: Click refresh class, if it fails again try another decompiler." + BytecodeViewer.nl + BytecodeViewer.nl + exception; }
@Override public boolean decompile(final ClassNode classNode, byte[] bytes, StringBuilder output) { try { if (classNode.version < 49) { bytes = fixBytes(bytes); } final byte[] bytesToUse = bytes; final Map<String, byte[]> loadedClasses = Helios.getAllLoadedData(); DecompilerSettings settings = getDecompilerSettings(); MetadataSystem metadataSystem = new MetadataSystem( new ITypeLoader() { private final InputTypeLoader backLoader = new InputTypeLoader(); @Override public boolean tryLoadType(String s, Buffer buffer) { if (s.equals(classNode.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(classNode.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(); output.append(decompiledSource); return true; } catch (Throwable e) { output.append(parseException(e)); return false; } }
public static void decompile( final String internalName, final ITextOutput output, final DecompilerSettings settings) { VerifyArgument.notNull(internalName, "internalName"); VerifyArgument.notNull(settings, "settings"); final ITypeLoader typeLoader = settings.getTypeLoader() != null ? settings.getTypeLoader() : new InputTypeLoader(); final MetadataSystem metadataSystem = new MetadataSystem(typeLoader); final TypeReference type; if (internalName.length() == 1) { // // Hack to get around classes whose descriptors clash with primitive types. // final MetadataParser parser = new MetadataParser(IMetadataResolver.EMPTY); final TypeReference reference = parser.parseTypeDescriptor(internalName); type = metadataSystem.resolve(reference); } else { type = metadataSystem.lookupType(internalName); } final TypeDefinition resolvedType; if (type == null || (resolvedType = type.resolve()) == null) { output.writeLine("!!! ERROR: Failed to load class %s.", internalName); return; } DeobfuscationUtilities.processType(resolvedType); final DecompilationOptions options = new DecompilationOptions(); options.setSettings(settings); options.setFullDecompilation(true); if (settings.getJavaFormattingOptions() == null) { settings.setJavaFormattingOptions(JavaFormattingOptions.createDefault()); } settings.getLanguage().decompileType(resolvedType, output, options); }
/** @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<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) { // some jar-s contain duplicate pom.xml entries: ignore // it if (!ze.getMessage().contains("duplicate")) { throw ze; } } } } } }