Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,12 @@ private static JavacTaskImpl createJavacTask(
options.add("-proc:none"); // NOI18N, Disable annotation processors
}
if (compilerOptions != null) {
for (String compilerOption : validateCompilerOptions(compilerOptions.getArguments(), validatedSourceLevel)) {
options.add(compilerOption);
}
options.addAll(
validateCompilerOptions(
compilerOptions.getArguments(),
useRelease != null ? com.sun.tools.javac.code.Source.lookup(useRelease) : validatedSourceLevel
)
);
}
if (!additionalModules.isEmpty()) {
options.add("--add-modules"); //NOI18N
Expand Down Expand Up @@ -1098,8 +1101,12 @@ private static String useRelease(final String requestedSource, com.sun.tools.jav
if (validatedSourceLevel.equals(sourceLevel)) {
return null;
}
if (sourceLevel.compareTo(com.sun.tools.javac.code.Source.JDK7) <= 0) {
sourceLevel = com.sun.tools.javac.code.Source.JDK7;
if (!sourceLevel.isSupported()) {
for (com.sun.tools.javac.code.Source searchForSupported : com.sun.tools.javac.code.Source.values()) {
if (searchForSupported.isSupported()) {
sourceLevel = searchForSupported;
}
}
}
return sourceLevel.isSupported() ? sourceLevel.requiredTarget().multiReleaseValue() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
import org.netbeans.modules.parsing.spi.SchedulerTask;
import org.netbeans.modules.parsing.spi.SourceModificationEvent;
import org.netbeans.modules.parsing.spi.TaskFactory;
import org.netbeans.spi.java.queries.CompilerOptionsQueryImplementation;
import org.openide.util.lookup.ProxyLookup;

/**
*
Expand Down Expand Up @@ -627,6 +629,53 @@ public void run(CompilationController parameter) throws Exception {
}, true);
}

public void testReleaseValidateCompilerOptionsBasedOnRelease() throws Exception {
FileObject f = createFile("test/Test.java",
"""
package test;
import javax.tools.Diagnostic;
""");
SourceUtilsTestUtil.setSourceLevel(f, "17");
SourceUtilsTestUtil.setCompilerOptions(f, List.of("--limit-modules=java.base"));
JavaSource js = JavaSource.create(new ClasspathInfo.Builder(ClassPath.EMPTY).build(), f);

js.runUserActionTask(new Task<CompilationController>() {
public void run(CompilationController parameter) throws Exception {
assertTrue(Phase.RESOLVED.compareTo(parameter.toPhase(Phase.RESOLVED)) <= 0);
assertEquals(parameter.getDiagnostics().toString(), 1, parameter.getDiagnostics().size());

Set<String> codes = new HashSet<String>();

for (Diagnostic d : parameter.getDiagnostics()) {
codes.add(d.getCode());
}

assertEquals(new HashSet<String>(Arrays.asList("compiler.err.package.not.visible")), codes);
}
}, true);
}

public void testReleaseUnsupportedSourceLevel() throws Exception {
FileObject f = createFile("test/Test.java",
"""
package test;
public class Test {
public static void main(String... args) {
System.out.println("");
}
}
""");
SourceUtilsTestUtil.setSourceLevel(f, "1.4");
JavaSource js = JavaSource.create(new ClasspathInfo.Builder(ClassPath.EMPTY).build(), f);

js.runUserActionTask(new Task<CompilationController>() {
public void run(CompilationController parameter) throws Exception {
assertTrue(Phase.RESOLVED.compareTo(parameter.toPhase(Phase.RESOLVED)) <= 0);
assertEquals(parameter.getDiagnostics().toString(), 0, parameter.getDiagnostics().size());
}
}, true);
}

private FileObject createFile(String path, String content) throws Exception {
FileObject file = FileUtil.createData(sourceRoot, path);
TestUtilities.copyStringToFile(file, content);
Expand Down
Loading