jadx.api.JadxArgs Java Examples

The following examples show how to use jadx.api.JadxArgs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RenameVisitor.java    From Box with Apache License 2.0 7 votes vote down vote up
private static void checkPackage(Deobfuscator deobfuscator, ClassNode cls, ClassInfo classInfo, JadxArgs args) {
	if (classInfo.isInner()) {
		return;
	}
	String aliasPkg = classInfo.getAliasPkg();
	if (args.isRenameValid() && aliasPkg.isEmpty()) {
		classInfo.changePkg(Consts.DEFAULT_PACKAGE_NAME);
		cls.addAttr(new RenameReasonAttr(cls).append("default package"));
		return;
	}
	String fullPkgAlias = deobfuscator.getPkgAlias(cls);
	if (!fullPkgAlias.equals(aliasPkg)) {
		classInfo.changePkg(fullPkgAlias);
		cls.addAttr(new RenameReasonAttr(cls).append("invalid package"));
	}
}
 
Example #2
Source File: RenameVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
private static void checkPackage(Deobfuscator deobfuscator, ClassNode cls, ClassInfo classInfo, JadxArgs args) {
	if (classInfo.isInner()) {
		return;
	}
	String aliasPkg = classInfo.getAliasPkg();
	if (args.isRenameValid() && aliasPkg.isEmpty()) {
		classInfo.changePkg(Consts.DEFAULT_PACKAGE_NAME);
		cls.addAttr(new RenameReasonAttr(cls).append("default package"));
		return;
	}
	String fullPkgAlias = deobfuscator.getPkgAlias(cls);
	if (!fullPkgAlias.equals(aliasPkg)) {
		classInfo.changePkg(fullPkgAlias);
		cls.addAttr(new RenameReasonAttr(cls).append("invalid package"));
	}
}
 
Example #3
Source File: StringUtilsTest.java    From Box with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("AvoidEscapedUnicodeCharacters")
public void testStringUnescape() {
	JadxArgs args = new JadxArgs();
	args.setEscapeUnicode(true);
	stringUtils = new StringUtils(args);

	checkStringUnescape("", "");
	checkStringUnescape("'", "'");
	checkStringUnescape("a", "a");
	checkStringUnescape("\n", "\\n");
	checkStringUnescape("\t", "\\t");
	checkStringUnescape("\r", "\\r");
	checkStringUnescape("\b", "\\b");
	checkStringUnescape("\f", "\\f");
	checkStringUnescape("\\", "\\\\");
	checkStringUnescape("\"", "\\\"");
	checkStringUnescape("\u1234", "\\u1234");
}
 
Example #4
Source File: RenameVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String fixClsShortName(JadxArgs args, String clsName) {
	char firstChar = clsName.charAt(0);
	boolean renameValid = args.isRenameValid();
	if (Character.isDigit(firstChar) && renameValid) {
		return Consts.ANONYMOUS_CLASS_PREFIX + NameMapper.removeInvalidCharsMiddle(clsName);
	}
	if (firstChar == '$' && renameValid) {
		return 'C' + NameMapper.removeInvalidCharsMiddle(clsName);
	}
	String cleanClsName = args.isRenamePrintable()
			? NameMapper.removeInvalidChars(clsName, "C")
			: clsName;
	if (cleanClsName.isEmpty()) {
		return null;
	}
	if (renameValid && !NameMapper.isValidIdentifier(cleanClsName)) {
		return 'C' + cleanClsName;
	}
	return cleanClsName;
}
 
Example #5
Source File: IntegrationTest.java    From Box with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void init() {
	this.deleteTmpFiles = true;
	this.unloadCls = true;
	this.withDebugInfo = true;
	this.compile = true;
	this.useEclipseCompiler = false;
	this.resMap = Collections.emptyMap();

	args = new JadxArgs();
	args.setOutDir(new File(OUT_DIR));
	args.setShowInconsistentCode(true);
	args.setThreadsCount(1);
	args.setSkipResources(true);
	args.setFsCaseSensitive(false); // use same value on all systems
}
 
Example #6
Source File: CodeGen.java    From jadx with Apache License 2.0 6 votes vote down vote up
public static ICodeInfo generate(ClassNode cls) {
	if (cls.contains(AFlag.DONT_GENERATE)) {
		return ICodeInfo.EMPTY;
	}
	JadxArgs args = cls.root().getArgs();
	switch (args.getOutputFormat()) {
		case JAVA:
			return generateJavaCode(cls, args);

		case JSON:
			return generateJson(cls);

		default:
			throw new JadxRuntimeException("Unknown output format");
	}
}
 
Example #7
Source File: CodeGen.java    From Box with Apache License 2.0 6 votes vote down vote up
public static ICodeInfo generate(ClassNode cls) {
	if (cls.contains(AFlag.DONT_GENERATE)) {
		return CodeWriter.EMPTY;
	}
	JadxArgs args = cls.root().getArgs();
	switch (args.getOutputFormat()) {
		case JAVA:
			return generateJavaCode(cls, args);

		case JSON:
			return generateJson(cls);

		default:
			throw new JadxRuntimeException("Unknown output format");
	}
}
 
Example #8
Source File: CodeGen.java    From Box with Apache License 2.0 6 votes vote down vote up
public static ICodeInfo generate(ClassNode cls) {
	if (cls.contains(AFlag.DONT_GENERATE)) {
		return CodeWriter.EMPTY;
	}
	JadxArgs args = cls.root().getArgs();
	switch (args.getOutputFormat()) {
		case JAVA:
			return generateJavaCode(cls, args);

		case JSON:
			return generateJson(cls);

		default:
			throw new JadxRuntimeException("Unknown output format");
	}
}
 
Example #9
Source File: RenameVisitor.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String fixClsShortName(JadxArgs args, String clsName) {
	char firstChar = clsName.charAt(0);
	boolean renameValid = args.isRenameValid();
	if (Character.isDigit(firstChar) && renameValid) {
		return Consts.ANONYMOUS_CLASS_PREFIX + NameMapper.removeInvalidCharsMiddle(clsName);
	}
	if (firstChar == '$' && renameValid) {
		return 'C' + NameMapper.removeInvalidCharsMiddle(clsName);
	}
	String cleanClsName = args.isRenamePrintable()
			? NameMapper.removeInvalidChars(clsName, "C")
			: clsName;
	if (cleanClsName.isEmpty()) {
		return null;
	}
	if (renameValid && !NameMapper.isValidIdentifier(cleanClsName)) {
		return 'C' + cleanClsName;
	}
	return cleanClsName;
}
 
Example #10
Source File: IntegrationTest.java    From Box with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void init() {
	this.deleteTmpFiles = true;
	this.unloadCls = true;
	this.withDebugInfo = true;
	this.compile = true;
	this.useEclipseCompiler = false;
	this.resMap = Collections.emptyMap();

	args = new JadxArgs();
	args.setOutDir(new File(OUT_DIR));
	args.setShowInconsistentCode(true);
	args.setThreadsCount(1);
	args.setSkipResources(true);
	args.setFsCaseSensitive(false); // use same value on all systems
}
 
Example #11
Source File: StringUtilsTest.java    From Box with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("AvoidEscapedUnicodeCharacters")
public void testStringUnescape() {
	JadxArgs args = new JadxArgs();
	args.setEscapeUnicode(true);
	stringUtils = new StringUtils(args);

	checkStringUnescape("", "");
	checkStringUnescape("'", "'");
	checkStringUnescape("a", "a");
	checkStringUnescape("\n", "\\n");
	checkStringUnescape("\t", "\\t");
	checkStringUnescape("\r", "\\r");
	checkStringUnescape("\b", "\\b");
	checkStringUnescape("\f", "\\f");
	checkStringUnescape("\\", "\\\\");
	checkStringUnescape("\"", "\\\"");
	checkStringUnescape("\u1234", "\\u1234");
}
 
Example #12
Source File: ConvertToClsSet.java    From jadx with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
	if (args.length < 2) {
		usage();
		System.exit(1);
	}
	List<Path> inputPaths = Stream.of(args).map(s -> Paths.get(s)).collect(Collectors.toList());
	Path output = inputPaths.remove(0);

	JadxPluginManager pluginManager = new JadxPluginManager();
	List<ILoadResult> loadedInputs = new ArrayList<>();
	for (JadxInputPlugin inputPlugin : pluginManager.getInputPlugins()) {
		loadedInputs.add(inputPlugin.loadFiles(inputPaths));
	}

	JadxArgs jadxArgs = new JadxArgs();
	jadxArgs.setRenameFlags(EnumSet.noneOf(JadxArgs.RenameEnum.class));
	RootNode root = new RootNode(jadxArgs);
	root.loadClasses(loadedInputs);

	ClsSet set = new ClsSet(root);
	set.loadFrom(root);
	set.save(output);
	LOG.info("Output: {}, file size: {}B", output, output.toFile().length());
	LOG.info("done");
}
 
Example #13
Source File: RenameVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
private static void checkFields(Deobfuscator deobfuscator, ClassNode cls, JadxArgs args) {
	Set<String> names = new HashSet<>();
	for (FieldNode field : cls.getFields()) {
		FieldInfo fieldInfo = field.getFieldInfo();
		String fieldName = fieldInfo.getAlias();
		boolean notUnique = !names.add(fieldName);
		boolean notValid = args.isRenameValid() && !NameMapper.isValidIdentifier(fieldName);
		boolean notPrintable = args.isRenamePrintable() && !NameMapper.isAllCharsPrintable(fieldName);
		if (notUnique || notValid || notPrintable) {
			deobfuscator.forceRenameField(field);
			field.addAttr(new RenameReasonAttr(field, notValid, notPrintable));
			if (notUnique) {
				field.addAttr(new RenameReasonAttr(field).append("collision with other field name"));
			}
		}
	}
}
 
Example #14
Source File: RenameVisitor.java    From jadx with Apache License 2.0 6 votes vote down vote up
private static void checkFields(Deobfuscator deobfuscator, ClassNode cls, JadxArgs args) {
	Set<String> names = new HashSet<>();
	for (FieldNode field : cls.getFields()) {
		FieldInfo fieldInfo = field.getFieldInfo();
		String fieldName = fieldInfo.getAlias();
		boolean notUnique = !names.add(fieldName);
		boolean notValid = args.isRenameValid() && !NameMapper.isValidIdentifier(fieldName);
		boolean notPrintable = args.isRenamePrintable() && !NameMapper.isAllCharsPrintable(fieldName);
		if (notUnique || notValid || notPrintable) {
			deobfuscator.forceRenameField(field);
			field.addAttr(new RenameReasonAttr(field, notValid, notPrintable));
			if (notUnique) {
				field.addAttr(new RenameReasonAttr(field).append("collision with other field name"));
			}
		}
	}
}
 
Example #15
Source File: StringUtilsTest.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("AvoidEscapedUnicodeCharacters")
public void testStringUnescape() {
	JadxArgs args = new JadxArgs();
	args.setEscapeUnicode(true);
	stringUtils = new StringUtils(args);

	checkStringUnescape("", "");
	checkStringUnescape("'", "'");
	checkStringUnescape("a", "a");
	checkStringUnescape("\n", "\\n");
	checkStringUnescape("\t", "\\t");
	checkStringUnescape("\r", "\\r");
	checkStringUnescape("\b", "\\b");
	checkStringUnescape("\f", "\\f");
	checkStringUnescape("\\", "\\\\");
	checkStringUnescape("\"", "\\\"");
	checkStringUnescape("\u1234", "\\u1234");
}
 
Example #16
Source File: TestJsonOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
@Test
public void testFallback() {
	disableCompilation();
	setFallback();
	args.setOutputFormat(JadxArgs.OutputFormatEnum.JSON);

	ClassNode cls = getClassNode(TestCls.class);
	String code = cls.getCode().toString();

	assertThat(code, containsString("\"offset\": \"0x"));
	assertThat(code, containsOne("public static class Inner implements java.lang.Runnable"));
}
 
Example #17
Source File: TestJsonOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
	disableCompilation();
	args.setOutputFormat(JadxArgs.OutputFormatEnum.JSON);

	ClassNode cls = getClassNode(TestCls.class);
	String code = cls.getCode().toString();

	assertThat(code, containsString("\"offset\": \"0x"));
	assertThat(code, containsOne("public static class Inner implements Runnable"));
}
 
Example #18
Source File: JadxClasspathTest.java    From jadx with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void initClsp() {
	this.root = new RootNode(new JadxArgs());
	this.root.loadClasses(Collections.emptyList());
	this.root.initClassPath();
	this.clsp = root.getClsp();
}
 
Example #19
Source File: JsonMappingGen.java    From Box with Apache License 2.0 5 votes vote down vote up
public static void dump(RootNode root) {
	JsonMapping mapping = new JsonMapping();
	fillMapping(mapping, root);

	JadxArgs args = root.getArgs();
	File outDirSrc = args.getOutDirSrc().getAbsoluteFile();
	File mappingFile = new File(outDirSrc, "mapping.json");
	FileUtils.makeDirsForFile(mappingFile);
	try (Writer writer = new FileWriter(mappingFile)) {
		GSON.toJson(mapping, writer);
		LOG.info("Save mappings to {}", mappingFile.getAbsolutePath());
	} catch (Exception e) {
		throw new JadxRuntimeException("Failed to save mapping json", e);
	}
}
 
Example #20
Source File: RenameVisitor.java    From jadx with Apache License 2.0 5 votes vote down vote up
private static void checkClassName(Deobfuscator deobfuscator, ClassNode cls, JadxArgs args) {
	ClassInfo classInfo = cls.getClassInfo();
	String clsName = classInfo.getAliasShortName();

	String newShortName = fixClsShortName(args, clsName);
	if (newShortName == null) {
		// rename failed, use deobfuscator
		String deobfName = deobfuscator.getClsAlias(cls);
		classInfo.changeShortName(deobfName);
		cls.addAttr(new RenameReasonAttr(cls).notPrintable());
		return;
	}
	if (!newShortName.equals(clsName)) {
		classInfo.changeShortName(newShortName);
		cls.addAttr(new RenameReasonAttr(cls).append("invalid class name"));
	}
	if (classInfo.isInner() && args.isRenameValid()) {
		// check inner classes names
		ClassInfo parentClass = classInfo.getParentClass();
		while (parentClass != null) {
			if (parentClass.getAliasShortName().equals(clsName)) {
				String clsAlias = deobfuscator.getClsAlias(cls);
				classInfo.changeShortName(clsAlias);
				cls.addAttr(new RenameReasonAttr(cls).append("collision with other inner class name"));
				break;
			}
			parentClass = parentClass.getParentClass();
		}
	}
	checkPackage(deobfuscator, cls, classInfo, args);
}
 
Example #21
Source File: StringUtilsTest.java    From Box with Apache License 2.0 5 votes vote down vote up
@Test
public void testCharUnescape() {
	stringUtils = new StringUtils(new JadxArgs());

	checkCharUnescape('a', "a");
	checkCharUnescape(' ', " ");
	checkCharUnescape('\n', "\\n");
	checkCharUnescape('\'', "\\\'");
	checkCharUnescape('\0', "\\u0000");
}
 
Example #22
Source File: JadxSettings.java    From jadx with Apache License 2.0 5 votes vote down vote up
public void updateRenameFlag(JadxArgs.RenameEnum flag, boolean enabled) {
	if (enabled) {
		renameFlags.add(flag);
	} else {
		renameFlags.remove(flag);
	}
}
 
Example #23
Source File: MainWindow.java    From jadx with Apache License 2.0 5 votes vote down vote up
private void saveAll(boolean export) {
	JadxArgs decompilerArgs = wrapper.getArgs();
	if ((!decompilerArgs.isFsCaseSensitive() && !decompilerArgs.isRenameCaseSensitive())
			|| !decompilerArgs.isRenameValid() || !decompilerArgs.isRenamePrintable()) {
		JOptionPane.showMessageDialog(
				this,
				NLS.str("msg.rename_disabled", settings.getLangLocale()),
				NLS.str("msg.rename_disabled_title", settings.getLangLocale()),
				JOptionPane.INFORMATION_MESSAGE);
	}
	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	fileChooser.setToolTipText(NLS.str("file.save_all_msg"));

	Path currentDirectory = settings.getLastSaveFilePath();
	if (currentDirectory != null) {
		fileChooser.setCurrentDirectory(currentDirectory.toFile());
	}

	int ret = fileChooser.showSaveDialog(mainPanel);
	if (ret == JFileChooser.APPROVE_OPTION) {
		decompilerArgs.setExportAsGradleProject(export);
		if (export) {
			decompilerArgs.setSkipSources(false);
			decompilerArgs.setSkipResources(false);
		} else {
			decompilerArgs.setSkipSources(settings.isSkipSources());
			decompilerArgs.setSkipResources(settings.isSkipResources());
		}
		settings.setLastSaveFilePath(fileChooser.getCurrentDirectory().toPath());
		ProgressMonitor progressMonitor = new ProgressMonitor(mainPanel, NLS.str("msg.saving_sources"), "", 0, 100);
		progressMonitor.setMillisToPopup(0);
		wrapper.saveAll(fileChooser.getSelectedFile(), progressMonitor);
	}
}
 
Example #24
Source File: SaveCode.java    From Box with Apache License 2.0 5 votes vote down vote up
private static String getFileExtension(ClassNode cls) {
	JadxArgs.OutputFormatEnum outputFormat = cls.root().getArgs().getOutputFormat();
	switch (outputFormat) {
		case JAVA:
			return ".java";

		case JSON:
			return ".json";

		default:
			throw new JadxRuntimeException("Unknown output format: " + outputFormat);
	}
}
 
Example #25
Source File: TypeCompareTest.java    From Box with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void init() {
	JadxArgs args = new JadxArgs();
	RootNode root = new RootNode(args);
	root.load(Collections.emptyList());
	root.initClassPath();
	compare = new TypeCompare(root);
}
 
Example #26
Source File: BaseExternalTest.java    From Box with Apache License 2.0 5 votes vote down vote up
protected void decompile(JadxArgs jadxArgs, @Nullable String clsPatternStr, @Nullable String mthPatternStr) {
	JadxDecompiler jadx = new JadxDecompiler(jadxArgs);
	jadx.load();

	if (clsPatternStr == null) {
		processAll(jadx);
		// jadx.saveSources();
	} else {
		processByPatterns(jadx, clsPatternStr, mthPatternStr);
	}
	printErrorReport(jadx);
}
 
Example #27
Source File: RenameVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
private static void checkClassName(Deobfuscator deobfuscator, ClassNode cls, JadxArgs args) {
	ClassInfo classInfo = cls.getClassInfo();
	String clsName = classInfo.getAliasShortName();

	String newShortName = fixClsShortName(args, clsName);
	if (newShortName == null) {
		// rename failed, use deobfuscator
		String deobfName = deobfuscator.getClsAlias(cls);
		classInfo.changeShortName(deobfName);
		cls.addAttr(new RenameReasonAttr(cls).notPrintable());
		return;
	}
	if (!newShortName.equals(clsName)) {
		classInfo.changeShortName(newShortName);
		cls.addAttr(new RenameReasonAttr(cls).append("invalid class name"));
	}
	if (classInfo.isInner() && args.isRenameValid()) {
		// check inner classes names
		ClassInfo parentClass = classInfo.getParentClass();
		while (parentClass != null) {
			if (parentClass.getAliasShortName().equals(clsName)) {
				String clsAlias = deobfuscator.getClsAlias(cls);
				classInfo.changeShortName(clsAlias);
				cls.addAttr(new RenameReasonAttr(cls).append("collision with other inner class name"));
				break;
			}
			parentClass = parentClass.getParentClass();
		}
	}
	checkPackage(deobfuscator, cls, classInfo, args);
}
 
Example #28
Source File: RenameVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void init(RootNode root) {
	List<DexNode> dexNodes = root.getDexNodes();
	if (dexNodes.isEmpty()) {
		return;
	}
	InputFile firstInputFile = dexNodes.get(0).getDexFile().getInputFile();
	Path inputFilePath = firstInputFile.getFile().getAbsoluteFile().toPath();

	String inputName = inputFilePath.getFileName().toString();
	String baseName = inputName.substring(0, inputName.lastIndexOf('.'));
	Path deobfMapPath = inputFilePath.getParent().resolve(baseName + ".jobf");

	JadxArgs args = root.getArgs();
	Deobfuscator deobfuscator = new Deobfuscator(args, dexNodes, deobfMapPath);
	if (args.isDeobfuscationOn()) {
		deobfuscator.execute();
	}

	checkClasses(deobfuscator, root, args);

	if (args.isDeobfuscationOn()) {
		deobfuscator.savePresets();
		deobfuscator.clear();
	}
	if (args.isJsonOutput()) {
		JsonMappingGen.dump(root);
	}
}
 
Example #29
Source File: JadxSettingsWindow.java    From jadx with Apache License 2.0 5 votes vote down vote up
private SettingsGroup makeRenameGroup() {
	JCheckBox renameCaseSensitive = new JCheckBox();
	renameCaseSensitive.setSelected(settings.isRenameCaseSensitive());
	renameCaseSensitive.addItemListener(e -> {
		settings.updateRenameFlag(JadxArgs.RenameEnum.CASE, e.getStateChange() == ItemEvent.SELECTED);
		needReload();
	});

	JCheckBox renameValid = new JCheckBox();
	renameValid.setSelected(settings.isRenameValid());
	renameValid.addItemListener(e -> {
		settings.updateRenameFlag(JadxArgs.RenameEnum.VALID, e.getStateChange() == ItemEvent.SELECTED);
		needReload();
	});

	JCheckBox renamePrintable = new JCheckBox();
	renamePrintable.setSelected(settings.isRenamePrintable());
	renamePrintable.addItemListener(e -> {
		settings.updateRenameFlag(JadxArgs.RenameEnum.PRINTABLE, e.getStateChange() == ItemEvent.SELECTED);
		needReload();
	});

	SettingsGroup group = new SettingsGroup(NLS.str("preferences.rename"));
	group.addRow(NLS.str("preferences.rename_case"), renameCaseSensitive);
	group.addRow(NLS.str("preferences.rename_valid"), renameValid);
	group.addRow(NLS.str("preferences.rename_printable"), renamePrintable);
	return group;
}
 
Example #30
Source File: RootNode.java    From Box with Apache License 2.0 5 votes vote down vote up
public RootNode(JadxArgs args) {
	this.args = args;
	this.passes = Jadx.getPassesList(args);
	this.stringUtils = new StringUtils(args);
	this.constValues = new ConstStorage(args);
	this.typeUpdate = new TypeUpdate(this);
	this.codeCache = args.getCodeCache();
}