Skip to content

Commit

Permalink
fix: false posive of RemoveUnusedUmportsTest, If multiple packages ha…
Browse files Browse the repository at this point in the history
…ve conflicting class names, we have to explicitly import the specific class to avoid ambiguity, so it can not be removed.
  • Loading branch information
kunli2 committed Apr 17, 2023
1 parent 9b11d3f commit 93caaf2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,4 +1203,115 @@ class A {
)
);
}

// If multiple packages have conflicting class names,
// so have to explicitly import the specific class to avoid ambiguity.
@Test
void multiplePackagesHaveConflictingClassNames() {
rewriteRun(
java(
"""
package org.b;
public class Pair<K, V> {
K key;
V value;
}
"""
),
java(
"""
package org.b;
public class Table<T> { }
"""
),
java(
"""
package org.c;
public class Pair<K, V> {
K key;
V value;
}
"""
),
java(
"""
package org.c;
public class Table<T> { }
"""
),
java(
"""
package org.b;
public class B1 { }
"""
),
java(
"""
package org.b;
public class B2 { }
"""
),
java(
"""
package org.b;
public class B3 { }
"""
),
java(
"""
package org.b;
public class B4 { }
"""
),
java(
"""
package org.c;
public class C1 {}
"""
),
java(
"""
package org.c;
public class C2 {}
"""
),
java(
"""
package org.c;
public class C3 {}
"""
),
java(
"""
package org.c;
public class C4 {}
"""
),
java(
"""
package org.a;
import org.b.*;
import org.b.Pair;
import org.c.*;
import org.c.Table;
class A {
void method() {
Pair<String, String > pair = new Pair<>();
Table<String> table = new Table<>();
B1 b1 = new B1();
B2 b2 = new B2();
B3 b3 = new B3();
B4 b4 = new B4();
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
changed = true;
}
} else {
if (usedWildcardImports.contains(elem.getPackageName()) && !elem.getTypeName().contains("$")) {
if (usedWildcardImports.size() == 1 && usedWildcardImports.contains(elem.getPackageName()) && !elem.getTypeName().contains("$")) {
anImport.used = false;
changed = true;
}
Expand Down

0 comments on commit 93caaf2

Please sign in to comment.