java-decompiler: renamer interface cleaned

- enum instead of magic constants
- typos
This commit is contained in:
Roman Shevchenko 2014-11-05 13:57:07 +01:00
parent 1fde78b6f1
commit 0b9402bd45
4 changed files with 50 additions and 106 deletions

View File

@ -83,8 +83,8 @@ public class ClassesProcessor {
}
else if (simpleName != null && DecompilerContext.getOption(IFernflowerPreferences.RENAME_ENTITIES)) {
IIdentifierRenamer renamer = DecompilerContext.getPoolInterceptor().getHelper();
if (renamer.toBeRenamed(IIdentifierRenamer.ELEMENT_CLASS, simpleName, null, null)) {
simpleName = renamer.getNextClassname(innername, simpleName);
if (renamer.toBeRenamed(IIdentifierRenamer.Type.ELEMENT_CLASS, simpleName, null, null)) {
simpleName = renamer.getNextClassName(innername, simpleName);
mapNewSimpleNames.put(innername, simpleName);
}
}

View File

@ -15,21 +15,15 @@
*/
package org.jetbrains.java.decompiler.main.extern;
public interface IIdentifierRenamer {
int ELEMENT_CLASS = 1;
enum Type {ELEMENT_CLASS, ELEMENT_FIELD, ELEMENT_METHOD};
int ELEMENT_FIELD = 2;
boolean toBeRenamed(Type elementType, String className, String element, String descriptor);
int ELEMENT_METHOD = 3;
String getNextClassName(String fullName, String shortName);
String getNextFieldName(String className, String field, String descriptor);
boolean toBeRenamed(int element_type, String classname, String element, String descriptor);
String getNextClassname(String fullname, String shortname);
String getNextFieldname(String classname, String field, String descriptor);
String getNextMethodname(String classname, String method, String descriptor);
String getNextMethodName(String className, String method, String descriptor);
}

View File

@ -17,102 +17,51 @@ package org.jetbrains.java.decompiler.modules.renamer;
import org.jetbrains.java.decompiler.main.extern.IIdentifierRenamer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ConverterHelper implements IIdentifierRenamer {
private static HashSet<String> setReserved = new HashSet<String>();
private static final Set<String> KEYWORDS = new HashSet<String>(Arrays.asList(
"abstract", "do", "if", "package", "synchronized", "boolean", "double", "implements", "private", "this", "break", "else", "import",
"protected", "throw", "byte", "extends", "instanceof", "public", "throws", "case", "false", "int", "return", "transient", "catch",
"final", "interface", "short", "true", "char", "finally", "long", "static", "try", "class", "float", "native", "strictfp", "void",
"const", "for", "new", "super", "volatile", "continue", "goto", "null", "switch", "while", "default", "assert", "enum"));
static {
setReserved.add("abstract");
setReserved.add("do");
setReserved.add("if");
setReserved.add("package");
setReserved.add("synchronized");
setReserved.add("boolean");
setReserved.add("double");
setReserved.add("implements");
setReserved.add("private");
setReserved.add("this");
setReserved.add("break");
setReserved.add("else");
setReserved.add("import");
setReserved.add("protected");
setReserved.add("throw");
setReserved.add("byte");
setReserved.add("extends");
setReserved.add("instanceof");
setReserved.add("public");
setReserved.add("throws");
setReserved.add("case");
setReserved.add("false");
setReserved.add("int");
setReserved.add("return");
setReserved.add("transient");
setReserved.add("catch");
setReserved.add("final");
setReserved.add("interface");
setReserved.add("short");
setReserved.add("true");
setReserved.add("char");
setReserved.add("finally");
setReserved.add("long");
setReserved.add("static");
setReserved.add("try");
setReserved.add("class");
setReserved.add("float");
setReserved.add("native");
setReserved.add("strictfp");
setReserved.add("void");
setReserved.add("const");
setReserved.add("for");
setReserved.add("new");
setReserved.add("super");
setReserved.add("volatile");
setReserved.add("continue");
setReserved.add("goto");
setReserved.add("null");
setReserved.add("switch");
setReserved.add("while");
setReserved.add("default");
setReserved.add("assert");
setReserved.add("enum");
}
private int classCounter = 0;
private int fieldCounter = 0;
private int methodCounter = 0;
private Set<String> setNonStandardClassNames = new HashSet<String>();
private int class_counter = 0;
private int field_counter = 0;
private int method_counter = 0;
private HashSet<String> setNonStandardClassNames = new HashSet<String>();
public boolean toBeRenamed(int element_type, String classname, String element, String descriptor) {
String value = (element_type == IIdentifierRenamer.ELEMENT_CLASS) ? classname : element;
return value == null || value.length() == 0 || value.length() <= 2 || setReserved.contains(value) || Character.isDigit(value.charAt(0));
@Override
public boolean toBeRenamed(Type elementType, String className, String element, String descriptor) {
String value = elementType == Type.ELEMENT_CLASS ? className : element;
return value == null || value.length() == 0 || value.length() <= 2 || KEYWORDS.contains(value) || Character.isDigit(value.charAt(0));
}
// TODO: consider possible conflicts with not renamed classes, fields and methods!
// We should get all relevant information here.
public String getNextClassname(String fullname, String shortname) {
@Override
public String getNextClassName(String fullName, String shortName) {
if (shortname == null) {
return "class_" + (class_counter++);
if (shortName == null) {
return "class_" + (classCounter++);
}
int index = 0;
while (Character.isDigit(shortname.charAt(index))) {
while (Character.isDigit(shortName.charAt(index))) {
index++;
}
if (index == 0 || index == shortname.length()) {
return "class_" + (class_counter++);
if (index == 0 || index == shortName.length()) {
return "class_" + (classCounter++);
}
else {
String name = shortname.substring(index);
String name = shortName.substring(index);
if (setNonStandardClassNames.contains(name)) {
return "Inner" + name + "_" + (class_counter++);
return "Inner" + name + "_" + (classCounter++);
}
else {
setNonStandardClassNames.add(name);
@ -121,23 +70,25 @@ public class ConverterHelper implements IIdentifierRenamer {
}
}
public String getNextFieldname(String classname, String field, String descriptor) {
return "field_" + (field_counter++);
@Override
public String getNextFieldName(String className, String field, String descriptor) {
return "field_" + (fieldCounter++);
}
public String getNextMethodname(String classname, String method, String descriptor) {
return "method_" + (method_counter++);
@Override
public String getNextMethodName(String className, String method, String descriptor) {
return "method_" + (methodCounter++);
}
// *****************************************************************************
// static methods
// *****************************************************************************
public static String getSimpleClassName(String fullname) {
return fullname.substring(fullname.lastIndexOf('/') + 1);
public static String getSimpleClassName(String fullName) {
return fullName.substring(fullName.lastIndexOf('/') + 1);
}
public static String replaceSimpleClassName(String fullname, String newname) {
return fullname.substring(0, fullname.lastIndexOf('/') + 1) + newname;
public static String replaceSimpleClassName(String fullName, String newName) {
return fullName.substring(0, fullName.lastIndexOf('/') + 1) + newName;
}
}

View File

@ -180,11 +180,11 @@ public class IdentifierConverter implements NewClassNameBuilder {
// TODO: rename packages
String clSimpleName = ConverterHelper.getSimpleClassName(classOldFullName);
if (helper.toBeRenamed(IIdentifierRenamer.ELEMENT_CLASS, clSimpleName, null, null)) {
if (helper.toBeRenamed(IIdentifierRenamer.Type.ELEMENT_CLASS, clSimpleName, null, null)) {
String classNewFullName;
do {
String classname = helper.getNextClassname(classOldFullName, ConverterHelper.getSimpleClassName(classOldFullName));
String classname = helper.getNextClassName(classOldFullName, ConverterHelper.getSimpleClassName(classOldFullName));
classNewFullName = ConverterHelper.replaceSimpleClassName(classOldFullName, classname);
}
while (context.getClasses().containsKey(classNewFullName));
@ -223,10 +223,10 @@ public class IdentifierConverter implements NewClassNameBuilder {
names.put(key, name);
}
}
else if (helper.toBeRenamed(IIdentifierRenamer.ELEMENT_METHOD, classOldFullName, name, mt.getDescriptor())) {
else if (helper.toBeRenamed(IIdentifierRenamer.Type.ELEMENT_METHOD, classOldFullName, name, mt.getDescriptor())) {
if (isPrivate || !names.containsKey(key)) {
do {
name = helper.getNextMethodname(classOldFullName, name, mt.getDescriptor());
name = helper.getNextMethodName(classOldFullName, name, mt.getDescriptor());
}
while (setMethodNames.contains(name));
@ -256,16 +256,15 @@ public class IdentifierConverter implements NewClassNameBuilder {
}
for (StructField fd : cl.getFields()) {
if (helper.toBeRenamed(IIdentifierRenamer.ELEMENT_FIELD, classOldFullName, fd.getName(), fd.getDescriptor())) {
String newname;
if (helper.toBeRenamed(IIdentifierRenamer.Type.ELEMENT_FIELD, classOldFullName, fd.getName(), fd.getDescriptor())) {
String newName;
do {
newname = helper.getNextFieldname(classOldFullName, fd.getName(), fd.getDescriptor());
newName = helper.getNextFieldName(classOldFullName, fd.getName(), fd.getDescriptor());
}
while (setFieldNames.contains(newname));
while (setFieldNames.contains(newName));
interceptor.addName(classOldFullName + " " + fd.getName() + " " + fd.getDescriptor(),
classNewFullName + " " + newname + " " + buildNewDescriptor(true, fd.getDescriptor()));
classNewFullName + " " + newName + " " + buildNewDescriptor(true, fd.getDescriptor()));
}
}
}