[java-decompiler] skips illegal local variable names

This commit is contained in:
Roman Shevchenko
2016-04-20 15:02:39 +02:00
parent b96586cc2f
commit eaa61a1f81
6 changed files with 80 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.TextUtil;
import java.util.*;
import java.util.Map.Entry;
@@ -64,8 +65,11 @@ public class VarProcessor {
String name = mapVarNames.get(pair);
Integer index = mapOriginalVarIndices.get(pair.var);
if (index != null && mapDebugVarNames.containsKey(index)) {
name = mapDebugVarNames.get(index);
if (index != null) {
String debugName = mapDebugVarNames.get(index);
if (debugName != null && TextUtil.isValidIdentifier(debugName, method.getClassStruct().getBytecodeVersion())) {
name = debugName;
}
}
Integer counter = mapNames.get(name);

View File

@@ -15,13 +15,23 @@
*/
package org.jetbrains.java.decompiler.util;
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.ClassesProcessor;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.TextBuffer;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
import java.util.Arrays;
import java.util.HashSet;
public class TextUtil {
private static final HashSet<String> KEYWORDS = new HashSet<>(Arrays.asList(
"abstract", "default", "if", "private", "this", "boolean", "do", "implements", "protected", "throw", "break", "double", "import",
"public", "throws", "byte", "else", "instanceof", "return", "transient", "case", "extends", "int", "short", "try", "catch", "final",
"interface", "static", "void", "char", "finally", "long", "strictfp", "volatile", "class", "float", "native", "super", "while",
"const", "for", "new", "switch", "continue", "goto", "package", "synchronized", "true", "false", "null", "assert"));
public static void writeQualifiedSuper(TextBuffer buf, String qualifier) {
ClassesProcessor.ClassNode classNode = (ClassesProcessor.ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
if (!qualifier.equals(classNode.classStruct.qualifiedName)) {
@@ -51,4 +61,26 @@ public class TextUtil {
sTemp = ("0000" + sTemp).substring(sTemp.length());
return "\\u" + sTemp;
}
public static boolean isValidIdentifier(String id, int version) {
return isJavaIdentifier(id) && !isKeyword(id, version);
}
private static boolean isJavaIdentifier(String id) {
if (id.isEmpty() || !Character.isJavaIdentifierStart(id.charAt(0))) {
return false;
}
for (int i = 1; i < id.length(); i++) {
if (!Character.isJavaIdentifierPart(id.charAt(i))) {
return false;
}
}
return true;
}
private static boolean isKeyword(String id, int version) {
return KEYWORDS.contains(id) || version > CodeConstants.BYTECODE_JAVA_5 && "enum".equals(id);
}
}