Java Code Examples for jadx.api.JadxArgs#isRenameValid()

The following examples show how to use jadx.api.JadxArgs#isRenameValid() . 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
@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 3
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 4
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 5
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 6
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 7
Source File: RenameVisitor.java    From jadx 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 8
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 9
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 10
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 11
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 12
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 13
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);
	}
}