Use fully qualified static field name in conflict #541
This commit is contained in:
committed by
Egor.Ushakov
parent
0b442fc64b
commit
2a213aa4a0
@@ -15,10 +15,13 @@
|
||||
*/
|
||||
package org.jetbrains.java.decompiler.main.collectors;
|
||||
|
||||
import org.jetbrains.java.decompiler.main.ClassesProcessor;
|
||||
import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
||||
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
||||
import org.jetbrains.java.decompiler.main.TextBuffer;
|
||||
import org.jetbrains.java.decompiler.struct.StructClass;
|
||||
import org.jetbrains.java.decompiler.struct.StructContext;
|
||||
import org.jetbrains.java.decompiler.struct.StructField;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -28,6 +31,8 @@ public class ImportCollector {
|
||||
|
||||
private final Map<String, String> mapSimpleNames = new HashMap<>();
|
||||
private final Set<String> setNotImportedNames = new HashSet<>();
|
||||
// set of field names in this class and all its predecessors.
|
||||
private final Set<String> setFieldNames = new HashSet<>();
|
||||
private final String currentPackageSlash;
|
||||
private final String currentPackagePoint;
|
||||
|
||||
@@ -43,6 +48,37 @@ public class ImportCollector {
|
||||
currentPackageSlash = "";
|
||||
currentPackagePoint = "";
|
||||
}
|
||||
|
||||
Map<String, ClassNode> mapRootCases = DecompilerContext.getClassProcessor().getMapRootClasses();
|
||||
for(StructClass sClass = root.classStruct;
|
||||
sClass!=null;
|
||||
){
|
||||
// all field names for current class ..
|
||||
for(StructField f: sClass.getFields()) {
|
||||
setFieldNames.add(f.getName());
|
||||
}
|
||||
|
||||
// .. and traverse through parent.
|
||||
ClassNode classNode;
|
||||
if(sClass.superClass==null || (classNode = (mapRootCases.get(sClass.superClass.getString())))==null)
|
||||
break;
|
||||
sClass = classNode.classStruct;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the package-less name ClassName is shaded by variable in a context of
|
||||
* the decompiled class
|
||||
* @param classToName - pkg.name.ClassName - class to find shortname for
|
||||
* @return ClassName if the name is not shaded by local field, pkg.name.ClassName otherwise
|
||||
*/
|
||||
public String getShortNameInClassContext(String classToName) {
|
||||
String shortName = getShortName(classToName);
|
||||
if(setFieldNames.contains(shortName)) {
|
||||
return classToName;
|
||||
} else {
|
||||
return shortName;
|
||||
}
|
||||
}
|
||||
|
||||
public String getShortName(String fullName) {
|
||||
|
||||
@@ -102,7 +102,7 @@ public class FieldExprent extends Exprent {
|
||||
if (isStatic) {
|
||||
ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
|
||||
if (node == null || !classname.equals(node.classStruct.qualifiedName) || isAmbiguous()) {
|
||||
buf.append(DecompilerContext.getImportCollector().getShortName(ExprProcessor.buildJavaClassName(classname)));
|
||||
buf.append(DecompilerContext.getImportCollector().getShortNameInClassContext(ExprProcessor.buildJavaClassName(classname)));
|
||||
buf.append(".");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -225,7 +225,7 @@ public class InvocationExprent extends Exprent {
|
||||
|
||||
ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
|
||||
if (node == null || !classname.equals(node.classStruct.qualifiedName)) {
|
||||
buf.append(DecompilerContext.getImportCollector().getShortName(ExprProcessor.buildJavaClassName(classname)));
|
||||
buf.append(DecompilerContext.getImportCollector().getShortNameInClassContext(ExprProcessor.buildJavaClassName(classname)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user