use of jadx.api.JadxDecompiler in project jadx by skylot.
the class IntegrationTest method getClassNodeFromFile.
public ClassNode getClassNodeFromFile(File file, String clsName) {
JadxDecompiler d = new JadxDecompiler(args);
try {
d.loadFile(file);
} catch (JadxException e) {
e.printStackTrace();
fail(e.getMessage());
}
RootNode root = JadxInternalAccess.getRoot(d);
root.getConstValues().getResourcesNames().putAll(resMap);
ClassNode cls = root.searchClassByName(clsName);
assertThat("Class not found: " + clsName, cls, notNullValue());
assertThat(clsName, is(cls.getClassInfo().getFullName()));
if (unloadCls) {
decompile(d, cls);
} else {
decompileWithoutUnload(d, cls);
}
System.out.println("-----------------------------------------------------------");
System.out.println(cls.getCode());
System.out.println("-----------------------------------------------------------");
checkCode(cls);
compile(cls);
runAutoCheck(clsName);
return cls;
}
use of jadx.api.JadxDecompiler in project jadx by skylot.
the class SingleClassMode method process.
public static boolean process(JadxDecompiler jadx, JadxCLIArgs cliArgs) {
String singleClass = cliArgs.getSingleClass();
String singleClassOutput = cliArgs.getSingleClassOutput();
if (singleClass == null && singleClassOutput == null) {
return false;
}
ClassNode clsForProcess;
if (singleClass != null) {
clsForProcess = jadx.getRoot().resolveClass(singleClass);
if (clsForProcess == null) {
clsForProcess = jadx.getRoot().getClasses().stream().filter(cls -> cls.getClassInfo().getAliasFullName().equals(singleClass)).findFirst().orElse(null);
}
if (clsForProcess == null) {
throw new JadxRuntimeException("Input class not found: " + singleClass);
}
if (clsForProcess.contains(AFlag.DONT_GENERATE)) {
throw new JadxRuntimeException("Input class can't be saved by currect jadx settings (marked as DONT_GENERATE)");
}
if (clsForProcess.isInner()) {
clsForProcess = clsForProcess.getTopParentClass();
LOG.warn("Input class is inner, parent class will be saved: {}", clsForProcess.getFullName());
}
} else {
// singleClassOutput is set
// expect only one class to be loaded
List<ClassNode> classes = jadx.getRoot().getClasses().stream().filter(c -> !c.isInner() && !c.contains(AFlag.DONT_GENERATE)).collect(Collectors.toList());
int size = classes.size();
if (size == 1) {
clsForProcess = classes.get(0);
} else {
throw new JadxRuntimeException("Found " + size + " classes, single class output can't be used");
}
}
ICodeInfo codeInfo;
try {
codeInfo = clsForProcess.decompile();
} catch (Exception e) {
throw new JadxRuntimeException("Class decompilation failed", e);
}
String fileExt = SaveCode.getFileExtension(jadx.getRoot());
File out;
if (singleClassOutput == null) {
out = new File(jadx.getArgs().getOutDirSrc(), clsForProcess.getClassInfo().getAliasFullPath() + fileExt);
} else {
if (singleClassOutput.endsWith(fileExt)) {
// treat as file name
out = new File(singleClassOutput);
} else {
// treat as directory
out = new File(singleClassOutput, clsForProcess.getShortName() + fileExt);
}
}
File resultOut = FileUtils.prepareFile(out);
if (clsForProcess.getClassInfo().hasAlias()) {
LOG.info("Saving class '{}' (alias: '{}') to file '{}'", clsForProcess.getClassInfo().getFullName(), clsForProcess.getFullName(), resultOut.getAbsolutePath());
} else {
LOG.info("Saving class '{}' to file '{}'", clsForProcess.getFullName(), resultOut.getAbsolutePath());
}
SaveCode.save(codeInfo.getCodeStr(), resultOut);
return true;
}
use of jadx.api.JadxDecompiler in project jadx by skylot.
the class SummaryNode method writeInputSummary.
private void writeInputSummary(StringEscapeUtils.Builder builder) throws IOException {
builder.append("<h2>Input</h2>");
JadxDecompiler jadx = mainWindow.getWrapper().getDecompiler();
builder.append("<h3>Files</h3>");
builder.append("<ul>");
for (File inputFile : jadx.getArgs().getInputFiles()) {
builder.append("<li>");
builder.escape(inputFile.getCanonicalFile().getAbsolutePath());
builder.append("</li>");
}
builder.append("</ul>");
List<ClassNode> classes = jadx.getRoot().getClasses(true);
List<String> codeSources = classes.stream().map(ClassNode::getInputFileName).distinct().sorted().collect(Collectors.toList());
codeSources.remove("synthetic");
int codeSourcesCount = codeSources.size();
builder.append("<h3>Code sources</h3>");
builder.append("<ul>");
if (codeSourcesCount != 1) {
builder.append("<li>Count: " + codeSourcesCount + "</li>");
}
// dex files list
codeSources.removeIf(f -> !f.endsWith(".dex"));
if (!codeSources.isEmpty()) {
for (String input : codeSources) {
builder.append("<li>");
builder.escape(input);
builder.append("</li>");
}
}
builder.append("</ul>");
int methodsCount = classes.stream().mapToInt(cls -> cls.getMethods().size()).sum();
int fieldsCount = classes.stream().mapToInt(cls -> cls.getFields().size()).sum();
int insnCount = classes.stream().flatMap(cls -> cls.getMethods().stream()).mapToInt(MethodNode::getInsnsCount).sum();
builder.append("<h3>Counts</h3>");
builder.append("<ul>");
builder.append("<li>Classes: " + classes.size() + "</li>");
builder.append("<li>Methods: " + methodsCount + "</li>");
builder.append("<li>Fields: " + fieldsCount + "</li>");
builder.append("<li>Instructions: " + insnCount + " (units)</li>");
builder.append("</ul>");
}
use of jadx.api.JadxDecompiler in project jadx by skylot.
the class IntegrationTest method loadFiles.
protected JadxDecompiler loadFiles(List<File> inputFiles) {
args.setInputFiles(inputFiles);
boolean useDx = !isJavaInput();
LOG.info(useDx ? "Using dex input" : "Using java input");
args.setUseDxInput(useDx);
JadxDecompiler d = new JadxDecompiler(args);
try {
d.load();
} catch (Exception e) {
LOG.error("Load failed", e);
d.close();
fail(e.getMessage());
return null;
}
RootNode root = JadxInternalAccess.getRoot(d);
insertResources(root);
return d;
}
use of jadx.api.JadxDecompiler in project jadx by skylot.
the class ExportGradleTest method exportGradle.
protected void exportGradle(String manifestFilename, String stringsFileName) {
final JadxDecompiler decompiler = JadxDecompilerTestUtils.getMockDecompiler();
ResourceFile androidManifest = mock(ResourceFile.class);
final ResContainer androidManifestContainer = createResourceContainer(manifestFilename);
when(androidManifest.loadContent()).thenReturn(androidManifestContainer);
final ResContainer strings = createResourceContainer(stringsFileName);
final RootNode root = decompiler.getRoot();
final ExportGradleProject export = new ExportGradleProject(root, exportDir, androidManifest, strings);
export.init();
assertThat(export.getSrcOutDir().exists());
assertThat(export.getResOutDir().exists());
}
Aggregations