Refactor ImportWrapper

This commit is contained in:
Clément Fournier committed 2021-04-05 20:41:56 +02:00
1 parent 81739da5ca
commit a493b15eb2
3 files changed
+30 -39

No files matched your search

@@ -27,7 +27,6 @@ public class ImportWrapper {
private final ASTImportDeclaration node;
private final String name;
private final String fullname;
private final boolean isStaticDemand;
private final Set<String> allStaticDemands;
public ImportWrapper(String fullname, String name) {
@@ -35,23 +34,24 @@ public class ImportWrapper {
}
public ImportWrapper(String fullname, String name, ASTImportDeclaration node) {
this(fullname, name, node, false);
}
public ImportWrapper(String fullname, String name, ASTImportDeclaration node, boolean isStaticDemand) {
this.fullname = fullname;
this.name = name;
this.node = node;
this.isStaticDemand = isStaticDemand;
this.allStaticDemands = collectStaticFieldsAndMethods(node);
}
public ImportWrapper(ASTImportDeclaration node) {
this.fullname = node.getImportedName();
this.name = node.getImportedSimpleName();
this.node = node;
this.allStaticDemands = collectStaticFieldsAndMethods(node);
}
/**
* @param node
*/
private Set<String> collectStaticFieldsAndMethods(ASTImportDeclaration node) {
if (!this.isStaticDemand || node == null || node.getType() == null) {
if (!this.isStaticDemand() || node == null || node.getType() == null) {
return Collections.emptySet();
}
@@ -96,7 +96,7 @@ public class ImportWrapper {
}
ImportWrapper i = (ImportWrapper) other;
if (isStaticDemand != i.isStaticDemand) {
if (isStaticDemand() != i.isStaticDemand()) {
return false;
}
if (name == null) {
@@ -106,7 +106,7 @@ public class ImportWrapper {
}
public boolean matches(ImportWrapper i) {
if (isStaticDemand) {
if (isStaticDemand()) {
if (allStaticDemands.contains(i.fullname)) {
return true;
}
@@ -120,15 +120,19 @@ public class ImportWrapper {
@Override
public int hashCode() {
if (name == null) {
return Objects.hash(fullname, isStaticDemand);
return Objects.hash(fullname, isStaticDemand());
}
return Objects.hash(name, isStaticDemand);
return Objects.hash(name, isStaticDemand());
}
public String getName() {
return name;
}
public String getPackageName() {
return node.getPackageName();
}
public String getFullName() {
return fullname;
}
@@ -138,11 +142,15 @@ public class ImportWrapper {
}
public boolean isStaticOnDemand() {
return isStaticDemand;
return isStaticDemand();
}
@Override
public String toString() {
return "Import[name=" + name + ",fullname=" + fullname + ",static*=" + isStaticDemand + ']';
return "Import[name=" + name + ",fullname=" + fullname + ",static*=" + isStaticDemand() + ']';
}
public boolean isStaticDemand() {
return node != null && node.isStatic() && node.isImportOnDemand();
}
}
@@ -115,22 +115,7 @@ public class UnusedImportsRule extends AbstractJavaRule {
@Override
public Object visit(ASTImportDeclaration node, Object data) {
if (node.isImportOnDemand()) {
ASTName importedType = (ASTName) node.getChild(0);
imports.add(new ImportWrapper(importedType.getImage(), null, node, node.isStatic()));
} else {
if (!node.isImportOnDemand()) {
ASTName importedType = (ASTName) node.getChild(0);
String className;
if (isQualifiedName(importedType)) {
int lastDot = importedType.getImage().lastIndexOf('.') + 1;
className = importedType.getImage().substring(lastDot);
} else {
className = importedType.getImage();
}
imports.add(new ImportWrapper(importedType.getImage(), className, node));
}
}
imports.add(new ImportWrapper(node));
return data;
}
@@ -33,13 +33,12 @@ public class DuplicateImportsRule extends AbstractJavaRule {
// import java.io.File;
for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
for (ImportWrapper thisSingleTypeImport : singleTypeImports) {
String singleTypeFullName = thisSingleTypeImport.getName(); // java.io.File
String singleTypeFullName = thisSingleTypeImport.getFullName(); // java.io.File
int lastDot = singleTypeFullName.lastIndexOf('.');
String singleTypePkg = singleTypeFullName.substring(0, lastDot); // java.io
String singleTypeName = singleTypeFullName.substring(lastDot + 1); // File
String singleTypePkg = thisSingleTypeImport.getPackageName(); // java.io
String singleTypeName = thisSingleTypeImport.getName(); // File
if (thisImportOnDemand.getName().equals(singleTypePkg)
if (thisImportOnDemand.getFullName().equals(singleTypePkg)
&& !isDisambiguationImport(node, singleTypePkg, singleTypeName)) {
addViolation(data, thisSingleTypeImport.getNode(), singleTypeFullName);
}
@@ -61,15 +60,15 @@ public class DuplicateImportsRule extends AbstractJavaRule {
// Loop over .* imports
for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
// Skip same package
if (!thisImportOnDemand.getName().equals(singleTypePkg)) {
if (!thisImportOnDemand.getFullName().equals(singleTypePkg)) {
if (!thisImportOnDemand.isStaticOnDemand()) {
String fullyQualifiedClassName = thisImportOnDemand.getName() + "." + singleTypeName;
String fullyQualifiedClassName = thisImportOnDemand.getFullName() + "." + singleTypeName;
if (node.getClassTypeResolver().classNameExists(fullyQualifiedClassName)) {
// Class exists in another imported package
return true;
}
} else {
Class<?> importClass = node.getClassTypeResolver().loadClassOrNull(thisImportOnDemand.getName());
Class<?> importClass = node.getClassTypeResolver().loadClassOrNull(thisImportOnDemand.getFullName());
if (importClass != null) {
try {
for (Method m : importClass.getMethods()) {
@@ -94,8 +93,7 @@ public class DuplicateImportsRule extends AbstractJavaRule {
@Override
public Object visit(ASTImportDeclaration node, Object data) {
ImportWrapper wrapper = new ImportWrapper(node.getImportedName(), node.getImportedName(),
node, node.isStatic() && node.isImportOnDemand());
ImportWrapper wrapper = new ImportWrapper(node);
// blahhhh... this really wants to be ASTImportDeclaration to be
// polymorphic...