use of org.jboss.windup.decompiler.fernflower.FernflowerDecompiler in project meghanada-server by mopemope.
the class LocationSearcher method searchLocationFromDecompileFile.
private Location searchLocationFromDecompileFile(SearchContext context, String searchFQCN, File classFile, String tempDir) throws IOException {
final FernflowerDecompiler decompiler = new FernflowerDecompiler();
decompiler.getLogger().setLevel(Level.OFF);
final File output = new File(tempDir, TEMP_DECOMPILE_DIR);
if (!output.exists() && !output.mkdirs()) {
log.warn("{} mkdirs fail", output);
}
try {
final DecompilationResult decompilationResult = decompiler.decompileArchive(classFile.toPath(), output.toPath(), zipEntry -> {
final String name = zipEntry.getName();
final String base = ClassNameUtils.replace(searchFQCN, ".", "/");
final String search = base + FileUtils.CLASS_EXT;
if (name.equals(search)) {
return Filter.Result.ACCEPT;
}
final String inner = base + '$';
if (name.startsWith(inner)) {
return Filter.Result.ACCEPT;
}
return Filter.Result.REJECT;
}, new DefaultDecompileFilter());
final String fqcn = ClassNameUtils.getParentClass(context.searchFQCN);
if (this.decompileFiles.containsKey(fqcn)) {
final List<String> files = this.decompileFiles.get(fqcn);
for (final String decompileFile : files) {
final File file = new File(decompileFile);
final Location location = searchLocationFromFile(context, fqcn, file);
if (location != null) {
return location;
}
}
} else {
final Map<String, String> decompiledFiles = decompilationResult.getDecompiledFiles();
final List<String> tempList = new ArrayList<>(4);
for (final String decompileFile : decompiledFiles.values()) {
final File decompiled = new File(decompileFile);
final File temp = File.createTempFile(TEMP_FILE_PREFIX + "-decompile-", FileUtils.JAVA_EXT);
LocationSearcher.copyAndFilter(decompiled, temp);
tempList.add(temp.getCanonicalPath());
decompiled.deleteOnExit();
if (!decompiled.delete()) {
log.warn("{} delete fail", decompiled);
}
if (!temp.setReadOnly()) {
log.warn("{} setReadOnly fail", temp);
}
temp.deleteOnExit();
final Location location = searchLocationFromFile(context, fqcn, temp);
if (location != null) {
this.decompileFiles.put(fqcn, tempList);
return location;
}
}
this.decompileFiles.put(fqcn, tempList);
}
return null;
} finally {
org.apache.commons.io.FileUtils.deleteDirectory(output);
}
}
Aggregations