Skip to content

Commit

Permalink
[3731] Fixing unused import handling for module-infos.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlahoda committed Mar 12, 2022
1 parent fbee1a2 commit be7c9bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public Void visitCompilationUnit(CompilationUnitTree tree, Void d) {
scan(tree.getImports(), d);
scan(tree.getPackageAnnotations(), d);
scan(tree.getTypeDecls(), d);
scan(tree.getModule(), d);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.netbeans.core.startup.Main;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.java.editor.base.imports.UnusedImports;
import org.netbeans.modules.java.source.parsing.JavacParser;
import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
Expand Down Expand Up @@ -333,8 +334,44 @@ public void testBrokenSource() throws Exception {
"}\n"));
}

public void testModuleInfo() throws Exception {
SourceUtilsTestUtil.setSourceLevel(src, "11");
writeFilesAndWaitForScan(src,
new File("module-info.java",
"import java.util.List;\n" +
"import test.Ann;\n" +
"import test.Service1;\n" +
"import test.Service2;\n" +
"import test.Service2Impl;\n" +
"@Ann\n" +
"module m {\n" +
" uses Service1;\n" +
" uses Service2;\n" +
" provides Service2 with Service2Impl;\n" +
"}\n"),
new File("test/Ann.java",
"package test;\n" +
"public @interface Ann { }"),
new File("test/Service1.java",
"package test;\n" +
"public interface Service1 { }"),
new File("test/Service2.java",
"package test;\n" +
"public interface Service2 { }"),
new File("test/Service2Impl.java",
"package test;\n" +
"public class Service2Impl implements Service2 { }"));

performUnusedImportsTestForFile("module-info.java",
"import java.util.List;\n");
}

private void performUnusedImportsTest(String... golden) throws Exception {
CompilationInfo ci = SourceUtilsTestUtil.getCompilationInfo(JavaSource.forFileObject(src.getFileObject("test/Main.java")), Phase.RESOLVED);
performUnusedImportsTestForFile("test/Main.java", golden);
}

private void performUnusedImportsTestForFile(String fileName, String... golden) throws Exception {
CompilationInfo ci = SourceUtilsTestUtil.getCompilationInfo(JavaSource.forFileObject(src.getFileObject(fileName)), Phase.RESOLVED);
SourceUtilsTestUtil2.disableConfinementTest();
Document doc = ci.getSnapshot().getSource().getDocument(true);
doc.putProperty(Language.class, JavaTokenId.language());
Expand Down Expand Up @@ -398,4 +435,7 @@ public File(String filename, String content) {
}
}

static {
JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true;
}
}

0 comments on commit be7c9bf

Please sign in to comment.