Skip to content

Commit

Permalink
Create warning when parsing targets too new Java versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Sep 27, 2024
1 parent 7cd2e50 commit e09fd55
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,8 @@ public interface IProblem {
int PreviewAPIUsed = Compliance + 1108;
/** @since 3.39*/
int JavaVersionNotSupported = Compliance + 1109;
/** @since 3.40*/
int JavaVersionTooRecent = Compliance + 1110;

/** @since 3.13 */
int UnlikelyCollectionMethodArgumentType = 1200;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ public class CompilerOptions {
public long complianceLevel;
/** Java source level, refers to a JDK version, e.g. {@link ClassFileConstants#JDK1_4} */
public long sourceLevel;
/**
* Initially requested source version, not necessarily consistent with {@link #sourceLevel} as
* sourceLevel forcibly contain a version that is compatible with ECJ.
* <p>Consumers are free to use {@code sourceLevel} or this
* {@code requestedSourceVersion} value to decide of their behavior.</p>
* <p>May be {@code null}.</p>
*/
public String requestedSourceVersion;
/** VM target level, refers to a JDK version, e.g. {@link ClassFileConstants#JDK1_4} */
public long targetJDK;
/** Source encoding format */
Expand Down Expand Up @@ -1764,7 +1772,7 @@ public void set(Map<String, String> optionsMap) {
long level = versionToJdkLevel(optionValue);
if (level != 0) this.complianceLevel = level;
}
if ((optionValue = optionsMap.get(OPTION_Source)) != null) {
if ((this.requestedSourceVersion = optionValue = optionsMap.get(OPTION_Source)) != null) {
long level = versionToJdkLevel(optionValue);
if (level != 0) this.sourceLevel = level;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Runtime.Version;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -12844,6 +12845,21 @@ public CompilationUnitDeclaration parse(
compilationResult,
0);

var problemReporterContext = this.problemReporter.referenceContext;
this.problemReporter.referenceContext = this.referenceContext;
if (this.options.requestedSourceVersion != null) {
try {
var requestedVersion = Version.parse(this.options.requestedSourceVersion);
var latestVersion = Version.parse(CompilerOptions.getLatestVersion());
if (requestedVersion.compareTo(latestVersion) > 0) {
this.problemReporter.tooRecentJavaVersion(requestedVersion.toString(), latestVersion.toString());
}
} catch (Exception ex) {
this.problemReporter.abortDueToInternalError(ex.getMessage());
}
}
this.problemReporter.referenceContext = problemReporterContext;

/* scanners initialization */
char[] contents;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,16 @@ public void abortDueToNotSupportedJavaVersion(String notSupportedVersion, String
0,
0);
}
public void tooRecentJavaVersion(String notSupportedVersion, String firstSupportedVersion) {
String[] args = new String[] {notSupportedVersion, firstSupportedVersion};
this.handle(
IProblem.JavaVersionTooRecent,
args,
args,
ProblemSeverities.Warning,
0,
0);
}
public void abstractMethodCannotBeOverridden(SourceTypeBinding type, MethodBinding concreteMethod) {

this.handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@
1107 = The Java feature ''{0}'' is only available with source level {1} and above
1108 = You are using an API that is part of a preview feature and may be removed in future
1109 = Compiling for Java version ''{0}'' is no longer supported. Minimal supported version is ''{1}''
1110 = Compiling for Java version ''{0}'' is not supported yet. Using ''{1}'' instead
# more programming problems:
1200 = Unlikely argument type {0} for {1} on a {2}
1201 = Unlikely argument type for equals(): {0} seems to be unrelated to {2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ class ProblemAttributes {
expectedProblemAttributes.put("FeatureNotSupported", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE));
expectedProblemAttributes.put("PreviewAPIUsed", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE));
expectedProblemAttributes.put("JavaVersionNotSupported", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE));
expectedProblemAttributes.put("JavaVersionTooRecent", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE));
expectedProblemAttributes.put("SwitchExpressionsYieldIncompatibleResultExpressionTypes", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("SwitchExpressionsYieldEmptySwitchBlock", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
expectedProblemAttributes.put("SwitchExpressionsYieldNoResultExpression", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
Expand Down Expand Up @@ -2382,6 +2383,7 @@ class ProblemAttributes {
expectedProblemAttributes.put("FeatureNotSupported", SKIP);
expectedProblemAttributes.put("PreviewAPIUsed", SKIP);
expectedProblemAttributes.put("JavaVersionNotSupported", SKIP);
expectedProblemAttributes.put("JavaVersionTooRecent", SKIP);
expectedProblemAttributes.put("SwitchExpressionsYieldIncompatibleResultExpressionTypes", SKIP);
expectedProblemAttributes.put("SwitchExpressionsYieldEmptySwitchBlock", SKIP);
expectedProblemAttributes.put("SwitchExpressionsYieldNoResultExpression", SKIP);
Expand Down

0 comments on commit e09fd55

Please sign in to comment.