use of com.strobel.assembler.metadata.TypeDefinition 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;
}
}
}
}
}
}
use of com.strobel.assembler.metadata.TypeDefinition 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;
}
use of com.strobel.assembler.metadata.TypeDefinition 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);
}
Aggregations