Skip to content

Commit fcac054

Browse files
authored
Merge pull request #9048 from lahodaj/improving-handling-of-release-in-javacparser
Improving handling of broken bootpaths with an implicit hardcoded --release
2 parents 0f6eb1e + e409a3e commit fcac054

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

java/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,12 @@ private static JavacTaskImpl createJavacTask(
10341034
options.add("-proc:none"); // NOI18N, Disable annotation processors
10351035
}
10361036
if (compilerOptions != null) {
1037-
for (String compilerOption : validateCompilerOptions(compilerOptions.getArguments(), validatedSourceLevel)) {
1038-
options.add(compilerOption);
1039-
}
1037+
options.addAll(
1038+
validateCompilerOptions(
1039+
compilerOptions.getArguments(),
1040+
useRelease != null ? com.sun.tools.javac.code.Source.lookup(useRelease) : validatedSourceLevel
1041+
)
1042+
);
10401043
}
10411044
if (!additionalModules.isEmpty()) {
10421045
options.add("--add-modules"); //NOI18N
@@ -1098,8 +1101,12 @@ private static String useRelease(final String requestedSource, com.sun.tools.jav
10981101
if (validatedSourceLevel.equals(sourceLevel)) {
10991102
return null;
11001103
}
1101-
if (sourceLevel.compareTo(com.sun.tools.javac.code.Source.JDK7) <= 0) {
1102-
sourceLevel = com.sun.tools.javac.code.Source.JDK7;
1104+
if (!sourceLevel.isSupported()) {
1105+
for (com.sun.tools.javac.code.Source searchForSupported : com.sun.tools.javac.code.Source.values()) {
1106+
if (searchForSupported.isSupported()) {
1107+
sourceLevel = searchForSupported;
1108+
}
1109+
}
11031110
}
11041111
return sourceLevel.isSupported() ? sourceLevel.requiredTarget().multiReleaseValue() : null;
11051112
}

java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
import org.netbeans.modules.parsing.spi.SchedulerTask;
9494
import org.netbeans.modules.parsing.spi.SourceModificationEvent;
9595
import org.netbeans.modules.parsing.spi.TaskFactory;
96+
import org.netbeans.spi.java.queries.CompilerOptionsQueryImplementation;
97+
import org.openide.util.lookup.ProxyLookup;
9698

9799
/**
98100
*
@@ -627,6 +629,53 @@ public void run(CompilationController parameter) throws Exception {
627629
}, true);
628630
}
629631

632+
public void testReleaseValidateCompilerOptionsBasedOnRelease() throws Exception {
633+
FileObject f = createFile("test/Test.java",
634+
"""
635+
package test;
636+
import javax.tools.Diagnostic;
637+
""");
638+
SourceUtilsTestUtil.setSourceLevel(f, "17");
639+
SourceUtilsTestUtil.setCompilerOptions(f, List.of("--limit-modules=java.base"));
640+
JavaSource js = JavaSource.create(new ClasspathInfo.Builder(ClassPath.EMPTY).build(), f);
641+
642+
js.runUserActionTask(new Task<CompilationController>() {
643+
public void run(CompilationController parameter) throws Exception {
644+
assertTrue(Phase.RESOLVED.compareTo(parameter.toPhase(Phase.RESOLVED)) <= 0);
645+
assertEquals(parameter.getDiagnostics().toString(), 1, parameter.getDiagnostics().size());
646+
647+
Set<String> codes = new HashSet<String>();
648+
649+
for (Diagnostic d : parameter.getDiagnostics()) {
650+
codes.add(d.getCode());
651+
}
652+
653+
assertEquals(new HashSet<String>(Arrays.asList("compiler.err.package.not.visible")), codes);
654+
}
655+
}, true);
656+
}
657+
658+
public void testReleaseUnsupportedSourceLevel() throws Exception {
659+
FileObject f = createFile("test/Test.java",
660+
"""
661+
package test;
662+
public class Test {
663+
public static void main(String... args) {
664+
System.out.println("");
665+
}
666+
}
667+
""");
668+
SourceUtilsTestUtil.setSourceLevel(f, "1.4");
669+
JavaSource js = JavaSource.create(new ClasspathInfo.Builder(ClassPath.EMPTY).build(), f);
670+
671+
js.runUserActionTask(new Task<CompilationController>() {
672+
public void run(CompilationController parameter) throws Exception {
673+
assertTrue(Phase.RESOLVED.compareTo(parameter.toPhase(Phase.RESOLVED)) <= 0);
674+
assertEquals(parameter.getDiagnostics().toString(), 0, parameter.getDiagnostics().size());
675+
}
676+
}, true);
677+
}
678+
630679
private FileObject createFile(String path, String content) throws Exception {
631680
FileObject file = FileUtil.createData(sourceRoot, path);
632681
TestUtilities.copyStringToFile(file, content);

0 commit comments

Comments
 (0)