java-decompiler: renamer interface cleaned

- enum instead of magic constants
- typos
master
Roman Shevchenko 10 years ago
parent 1fde78b6f1
commit 0b9402bd45
  1. 4
      src/org/jetbrains/java/decompiler/main/ClassesProcessor.java
  2. 16
      src/org/jetbrains/java/decompiler/main/extern/IIdentifierRenamer.java
  3. 121
      src/org/jetbrains/java/decompiler/modules/renamer/ConverterHelper.java
  4. 19
      src/org/jetbrains/java/decompiler/modules/renamer/IdentifierConverter.java

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

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

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

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

Loading…
Cancel
Save