Skip to content

Commit

Permalink
Removing unwanted package imports
Browse files Browse the repository at this point in the history
Signed-off-by: Subhash Arabhi <subhashsubbu636@gmail.com>

Tests for changes in addImports function

Signed-off-by: Subhash Arabhi <subhashsubbu636@gmail.com>
  • Loading branch information
subhash-arabhi committed Aug 29, 2024
1 parent effcb4f commit 9b6efe6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ private CompilationUnitTree addImports(CompilationUnitTree cut, List<? extends I
// check for possible name clashes originating from adding the package imports
Set<Element> explicitNamedImports = new HashSet<Element>();
for (Element element : elementsToImport) {
if (element.getKind().isClass() || element.getKind().isInterface()) {
if (element.getEnclosingElement() != pkg && (element.getKind().isClass() || element.getKind().isInterface())) {
for (Symbol sym : importScope.getSymbolsByName((com.sun.tools.javac.util.Name)element.getSimpleName())) {
if (sym.getKind().isClass() || sym.getKind().isInterface()) {
if (sym != element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ public void validate(CompilationInfo info) {
}, false);
}

public void testAddImports14() throws Exception {
performTest("test/Process.java", "package test;\nimport java.util.Collections;\npublic class Process {\npublic static void main(String... args) {\n" +
"Collections.singleton(Process.class).forEach(System.out::println);\n}\n}", "1.5", new AddImportsTask("test.Process"), new Validator() {
public void validate(CompilationInfo info) {
assertEquals(0, info.getDiagnostics().size());
List<? extends ImportTree> imports = info.getCompilationUnit().getImports();
assertEquals(1, imports.size());
assertEquals("java.util.Collections", imports.get(0).getQualifiedIdentifier().toString());
}
}, false);
}

public void testAddImportsIncrementallyWithStatic_JIRA3019() throws Exception {
JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true;
performTest("package test;\npublic class Test { public static final String CONST = null; }\n", "11", new AddImportsTask(true, "test.Test.CONST", "java.util.List"), new Validator() {
Expand Down

0 comments on commit 9b6efe6

Please sign in to comment.