Skip to content

SONARJAVA-7278 Override SystemReleaseClasspath to expose the JDK API of a requested release (JEP 247) - #169

Merged
leonardo-pilastri-sonarsource merged 9 commits into
masterfrom
use-release-apis
Nov 19, 2026
Merged

leonardo-pilastri-sonarsource merged 9 commits into
masterfrom
use-release-apis

Conversation

@leonardo-pilastri-sonarsource

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • New class:
    • Added SystemReleaseClasspath exposing the JDK API of a requested release (JEP 247 ct.sym) to ASTParser
    • Options org.sonarsource.java.jdt.systemRelease and ...javaHome select the release and the JDK providing ct.sym
  • ASTParser integration:
    • getClasspath() uses the release classpath instead of the running VM bootclasspath when a release is requested, falling back otherwise
    • Parsers without the running VM also read the release from the JDK given by javaHome
  • Behavior:
    • Releases below 9 are never served from ct.sym, so the running JVM API is kept
    • Releases that cannot be initialized from ct.sym fall back to the JDK API instead of throwing
  • Tests:
    • Added SystemReleaseClasspathTest covering option guards and the API exposed for a release
    • Added ASTParserReleaseIT checking the release API through the shaded jar's real ASTParser
    • Added JUnit Jupiter and AssertJ test dependencies in pom.xml
  • Build:
    • Configured maven-failsafe-plugin to run integration tests against the shaded jar
    • Upgraded org.eclipse.jdt.core to 3.47.0 and project version to 1.11-SNAPSHOT

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

SONARJAVA-7278

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource changed the title SONARJAVA-7278 Expose the JDK API of a requested release (JEP 247) to ASTParser SONARJAVA-7278 Override ASTParser to expose the JDK API of a requested release (JEP 247) Nov 2, 2026
…d release (JEP 247)

Override ASTParser so that the JDK API matching the requested release is
exposed through the new SystemReleaseClasspath (backed by ct.sym, JEP 247).

The release API is also applied to an explicit JDK: read the version of the
JDK providing ct.sym from its release file, and only substitute when the
requested release is older than it, so that a ct.sym is never asked for a
release it does not describe. Expose isApplicable so that a client handing a
JDK to the parser through its classpath (jrt-fs.jar) can replace it with the
release API of that same JDK.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sonarqube-next

sonarqube-next Bot commented Nov 2, 2026

Copy link
Copy Markdown

…rn and fall back otherwise

The release is now served as soon as the JDK providing it has a ct.sym that describes it, instead of being compared
with the version read from the "release" file of the JDK. When ECJ cannot initialize the entry (no lib/ct.sym, ct.sym
that does not describe the release, release that is not a number...), one warning is logged per JDK and release and the
parser keeps its previous behavior. Initializing the entry up front also avoids a failure in the middle of a parse, as ECJ
cannot serve from ct.sym the release of the JDK that owns it.

Java 8 and older are never served: ct.sym only holds the supported public API, which is less than a JDK 8 offers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@datadog-sonarsource

datadog-sonarsource Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 6978507 | Docs | Give us feedback!

@david-schroeter david-schroeter left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good and seems almost ready to merge.

There is just one point to clarify about errors when reading ct.sym: we fall back to the selected JDK when the file is missing, but throw when it exists and cannot be read. Is that difference intentional?

Otherwise, there are a few gaps in the test assertions that could let future regressions go unnoticed. If they aren't straightforward to address here, we could create a ticket and strengthen the tests in a follow-up PR.

@david-schroeter david-schroeter left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for addressing the comments and for finding a way to better test which release version is used!

There is just an import in SystemReleaseClasspathTest.java that is unused and should be remove, but other than that this PR is ready to be merged.

@gitar-bot

gitar-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 4 closed / 4 findings

Changing ASTParser classpaths to use ct.sym for requested Java releases may alter type resolution and analysis results across supported releases.

Adds a custom ASTParser override so the JDT parser can expose the JDK API of a requested release (JEP 247), serving it from ct.sym when the JDK supports it and warning and falling back otherwise. Resolved issues cover release resolution when javaHome is explicit, preserving the ASTParser patch across overrideECJ.sh upgrades, and tightening the legacy 1.8.0_362 and release-file version tests so they actually verify parsing.

No open issues remain.

✅ 4 closed
✅ Bug: Explicit javaHome with release >= that JDK's version breaks resolution

📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:117-126 📄 src/main/java/org/eclipse/jdt/core/dom/ASTParser.java:242-247
The check that skips substitution when the release is not older than the JDK (release.value >= Runtime.version().feature()) only runs when usesRunningJdk is true. With an explicit OPTION_JAVA_HOME, resolve() returns getOlderSystemRelease(javaHome, release, null) for any release >= 9, even when the release equals or exceeds that JDK's own version. A common case is a project targeting 17 analysed with a JDK 17 home. ct.sym only describes older releases. When FileSystem later calls initialize(), ClasspathJep247/ClasspathJep247Jdk12 throw IllegalArgumentException("release N is not found in the system") and do not fall back to jrt. That call happens inside CompilationUnitResolver/NameEnvironmentWithProgress, after getClasspath() and outside its try/catch. The result is that createAST/createASTs fail, or the JDK library is missing, instead of keeping the old behaviour that the class Javadoc promises. ECJ's own Main avoids this by calling getOlderSystemRelease only when complianceLevel < jdkLevel. Fix: work out the feature version of the explicit JDK (for example from <javaHome>/release JAVA_VERSION, or by checking that ct.sym has an entry for the release) and return empty when the release is not older than it. As defence in depth, call initialize() eagerly inside resolve() and return empty on IllegalArgumentException/IOException.

✅ Quality: ASTParser patch will be lost on the next overrideECJ.sh upgrade

📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:72-74 📄 src/main/java/org/eclipse/jdt/core/dom/ASTParser.java:241-247
overrideECJ.sh re-applies the patches by taking exactly one commit (git format-patch --stdout -1 "$override_commit"), which the operator picks from commits whose message contains 'Override'. This PR changes ASTParser.java in a separate commit whose message is not an 'Override JDT core …' commit. On the next ECJ upgrade the operator will pick the latest Override commit, ASTParser.java will be re-downloaded from upstream, and the SystemReleaseClasspath.resolve hook will be dropped without any error. SystemReleaseClasspath.java will still compile, so nothing will flag that it is no longer called. The new class's Javadoc even refers readers to overrideECJ.sh as the mechanism that re-applies this patch. Fix: fold this change into the Override commit (for example a new 'Override JDT core … with custom changes' commit that contains all ASTParser patches), or update the script/README so that several patch commits are applied.

✅ Quality: Legacy 1.8.0_362 notation assertion passes whether or not it is parsed

📄 src/test/java/org/eclipse/jdt/core/dom/SystemReleaseClasspathTest.java:124-132 📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:151-163 📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:201-213
supports_the_notations_of_the_version asserts isApplicable("9", fakeJdk("1.8.0_362", true)) is false to cover the legacy 1.x notation in parseFeatureVersion. plan() rejects the release when release >= jdkVersion, so the result is false whether the version is parsed correctly as 8 or wrongly as 1 (9 >= 1). Releases below 9 are rejected earlier anyway. So this line can't catch a regression in the 1.x branch, even though the test claims to cover that notation.

Since no release ≥ 9 can be applicable on a 1.x JDK, the isApplicable path can't observe this branch. To check it, test parseFeatureVersion directly (make it package-private), or drop the line and its claim.

✅ Quality: Release-file version test passes even if the version is never parsed

📄 src/test/java/org/eclipse/jdt/core/dom/SystemReleaseClasspathTest.java:130-137 📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:113-114 📄 src/main/java/org/eclipse/jdt/core/dom/SystemReleaseClasspath.java:150-163
jdk_version_is_read_from_its_release_file requests release 11 from a fake JDK whose release file reports 11 (in four notations) and expects the full JDK API. In resolveTargetRelease, an empty result from jdkFeatureVersion (no regex match, unreadable file) also falls through orElseGet to getJrtClasspath, which gives the same full API. So the test passes when JAVA_VERSION_LINE matches none of the notations (for example if 1.11 or 11-ea stopped parsing). It only fails when the version is misread as a number above 11, so it does not check that the version is actually read. To fix it, use a version that decides the result one way or the other: for example a fake home with the running JDK's ct.sym where JAVA_VERSION is below the running feature, so release 11 must be served (no Record), or a broken ct.sym with a version above 11 that must throw.

Verification Rules 2/2 met Repos 1 inspected · PRs 3 inspected

Functional validation No results

Rules 2 passed 2 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 1 inspected PRs 3 inspected
Cross-repo 1 repository inspected 0 open impacts

SonarSource/sonar-java Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 6459: SONARJAVA-7279 Resolve the JDK API of the target release through the new jdt-package option Inspected
PR 6517: SONARJAVA-7341 Simplify the parser interface Inspected
PR 6485: SONARJAVA-7300 Decouple the Sonar Java frontend from Eclipse JDT core Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource changed the title SONARJAVA-7278 Override ASTParser to expose the JDK API of a requested release (JEP 247) SONARJAVA-7278 Override SystemReleaseClasspath to expose the JDK API of a requested release (JEP 247) Nov 18, 2026
@leonardo-pilastri-sonarsource
leonardo-pilastri-sonarsource merged commit 6148057 into master Nov 19, 2026
9 checks passed
@leonardo-pilastri-sonarsource
leonardo-pilastri-sonarsource deleted the use-release-apis branch November 19, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants