|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2000-2015 JetBrains s.r.o. |
|
|
|
* Copyright 2000-2016 JetBrains s.r.o. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
@ -24,24 +24,25 @@ import org.jetbrains.java.decompiler.struct.StructContext; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.Map.Entry; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ImportCollector { |
|
|
|
public class ImportCollector { |
|
|
|
|
|
|
|
|
|
|
|
private static final String JAVA_LANG_PACKAGE = "java.lang"; |
|
|
|
private static final String JAVA_LANG_PACKAGE = "java.lang"; |
|
|
|
|
|
|
|
|
|
|
|
private final Map<String, String> mapSimpleNames = new HashMap<String, String>(); |
|
|
|
private final Map<String, String> mapSimpleNames = new HashMap<String, String>(); |
|
|
|
private final Set<String> setNotImportedNames = new HashSet<String>(); |
|
|
|
private final Set<String> setNotImportedNames = new HashSet<String>(); |
|
|
|
private String currentPackageSlash = ""; |
|
|
|
private final String currentPackageSlash; |
|
|
|
private String currentPackagePoint = ""; |
|
|
|
private final String currentPackagePoint; |
|
|
|
|
|
|
|
|
|
|
|
public ImportCollector(ClassNode root) { |
|
|
|
public ImportCollector(ClassNode root) { |
|
|
|
|
|
|
|
String clName = root.classStruct.qualifiedName; |
|
|
|
String clname = root.classStruct.qualifiedName; |
|
|
|
int index = clName.lastIndexOf('/'); |
|
|
|
int index = clname.lastIndexOf("/"); |
|
|
|
|
|
|
|
if (index >= 0) { |
|
|
|
if (index >= 0) { |
|
|
|
currentPackageSlash = clname.substring(0, index); |
|
|
|
String packageName = clName.substring(0, index); |
|
|
|
currentPackagePoint = currentPackageSlash.replace('/', '.'); |
|
|
|
currentPackageSlash = packageName + '/'; |
|
|
|
currentPackageSlash += "/"; |
|
|
|
currentPackagePoint = packageName.replace('/', '.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
currentPackageSlash = ""; |
|
|
|
|
|
|
|
currentPackagePoint = ""; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -49,42 +50,38 @@ public class ImportCollector { |
|
|
|
return getShortName(fullname, true); |
|
|
|
return getShortName(fullname, true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getShortName(String fullname, boolean imported) { |
|
|
|
public String getShortName(String fullName, boolean imported) { |
|
|
|
|
|
|
|
ClassesProcessor clProc = DecompilerContext.getClassProcessor(); |
|
|
|
ClassesProcessor clproc = DecompilerContext.getClassProcessor(); |
|
|
|
ClassNode node = clProc.getMapRootClasses().get(fullName.replace('.', '/')); |
|
|
|
ClassNode node = clproc.getMapRootClasses().get(fullname.replace('.', '/')); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String retname = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String result = null; |
|
|
|
if (node != null && node.classStruct.isOwn()) { |
|
|
|
if (node != null && node.classStruct.isOwn()) { |
|
|
|
|
|
|
|
result = node.simpleName; |
|
|
|
retname = node.simpleName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (node.parent != null && node.type == ClassNode.CLASS_MEMBER) { |
|
|
|
while (node.parent != null && node.type == ClassNode.CLASS_MEMBER) { |
|
|
|
retname = node.parent.simpleName + "." + retname; |
|
|
|
result = node.parent.simpleName + '.' + result; |
|
|
|
node = node.parent; |
|
|
|
node = node.parent; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (node.type == ClassNode.CLASS_ROOT) { |
|
|
|
if (node.type == ClassNode.CLASS_ROOT) { |
|
|
|
fullname = node.classStruct.qualifiedName; |
|
|
|
fullName = node.classStruct.qualifiedName; |
|
|
|
fullname = fullname.replace('/', '.'); |
|
|
|
fullName = fullName.replace('/', '.'); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
return retname; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
fullname = fullname.replace('$', '.'); |
|
|
|
fullName = fullName.replace('$', '.'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String nshort = fullname; |
|
|
|
String shortName = fullName; |
|
|
|
String npackage = ""; |
|
|
|
String packageName = ""; |
|
|
|
|
|
|
|
|
|
|
|
int lastpoint = fullname.lastIndexOf("."); |
|
|
|
int lastDot = fullName.lastIndexOf('.'); |
|
|
|
|
|
|
|
if (lastDot >= 0) { |
|
|
|
if (lastpoint >= 0) { |
|
|
|
shortName = fullName.substring(lastDot + 1); |
|
|
|
nshort = fullname.substring(lastpoint + 1); |
|
|
|
packageName = fullName.substring(0, lastDot); |
|
|
|
npackage = fullname.substring(0, lastpoint); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
StructContext context = DecompilerContext.getStructContext(); |
|
|
|
StructContext context = DecompilerContext.getStructContext(); |
|
|
@ -92,42 +89,40 @@ public class ImportCollector { |
|
|
|
// check for another class which could 'shadow' this one. Two cases:
|
|
|
|
// check for another class which could 'shadow' this one. Two cases:
|
|
|
|
// 1) class with the same short name in the current package
|
|
|
|
// 1) class with the same short name in the current package
|
|
|
|
// 2) class with the same short name in the default package
|
|
|
|
// 2) class with the same short name in the default package
|
|
|
|
boolean existsDefaultClass = (context.getClass(currentPackageSlash + nshort) != null |
|
|
|
boolean existsDefaultClass = |
|
|
|
&& !npackage.equals(currentPackagePoint)) // current package
|
|
|
|
(context.getClass(currentPackageSlash + shortName) != null && !packageName.equals(currentPackagePoint)) || // current package
|
|
|
|
|| (context.getClass(nshort) != null |
|
|
|
(context.getClass(shortName) != null && !currentPackagePoint.isEmpty()); // default package
|
|
|
|
&& !currentPackagePoint.isEmpty()); // default package
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (existsDefaultClass || |
|
|
|
if (existsDefaultClass || |
|
|
|
(mapSimpleNames.containsKey(nshort) && !npackage.equals(mapSimpleNames.get(nshort)))) { |
|
|
|
(mapSimpleNames.containsKey(shortName) && !packageName.equals(mapSimpleNames.get(shortName)))) { |
|
|
|
// don't return full name because if the class is a inner class, full name refers to the parent full name, not the child full name
|
|
|
|
// don't return full name because if the class is a inner class, full name refers to the parent full name, not the child full name
|
|
|
|
return retname == null ? fullname : (npackage + "." + retname); |
|
|
|
return result == null ? fullName : (packageName + "." + result); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (!mapSimpleNames.containsKey(nshort)) { |
|
|
|
else if (!mapSimpleNames.containsKey(shortName)) { |
|
|
|
mapSimpleNames.put(nshort, npackage); |
|
|
|
mapSimpleNames.put(shortName, packageName); |
|
|
|
|
|
|
|
|
|
|
|
if (!imported) { |
|
|
|
if (!imported) { |
|
|
|
setNotImportedNames.add(nshort); |
|
|
|
setNotImportedNames.add(shortName); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return retname == null ? nshort : retname; |
|
|
|
return result == null ? shortName : result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int writeImports(TextBuffer buffer) { |
|
|
|
public int writeImports(TextBuffer buffer) { |
|
|
|
int importlines_written = 0; |
|
|
|
int importLinesWritten = 0; |
|
|
|
|
|
|
|
|
|
|
|
List<String> imports = packImports(); |
|
|
|
List<String> imports = packImports(); |
|
|
|
|
|
|
|
|
|
|
|
for (String s : imports) { |
|
|
|
for (String s : imports) { |
|
|
|
buffer.append("import "); |
|
|
|
buffer.append("import "); |
|
|
|
buffer.append(s); |
|
|
|
buffer.append(s); |
|
|
|
buffer.append(";"); |
|
|
|
buffer.append(';'); |
|
|
|
buffer.appendLineSeparator(); |
|
|
|
buffer.appendLineSeparator(); |
|
|
|
|
|
|
|
|
|
|
|
importlines_written++; |
|
|
|
importLinesWritten++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return importlines_written; |
|
|
|
return importLinesWritten; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private List<String> packImports() { |
|
|
|
private List<String> packImports() { |
|
|
|