Skip to content

[DO NOT MERGE][PoC] SONARJAVA-7339 Use SemSitter as semantic layer - #6533

Draft
rombirli wants to merge 246 commits into
masterfrom
rombirli/poc-semsitter-udg-server-rebased
Draft

rombirli wants to merge 246 commits into
masterfrom
rombirli/poc-semsitter-udg-server-rebased

Conversation

@rombirli

@rombirli rombirli commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Duplicate of #6514. Same PoC, rebased onto the current rombirli/semsitter-ecj-replacement-v2. antonio/poc-semsitter-udg-server is unchanged.

Draft PoC. Do not merge.

The 52 commits that start after 10fa6fd1f7 are replayed on top of the updated base. Description and measurements stay on #6514.


Summary by Gitar

  • Analyzer session:
    • Added AnalyzerSession client that keeps one SemSitter server process per module and caches type lookups
    • Added FlatFrame decoder for the flat get-file frame, used from protocol 1.8
  • Semantic model:
    • Extended JSema and SemsitterTranslator with project member annotations, thrown types, and undeclared-type marking
  • Checks:
    • Extended CompilerWarnings with redundant-cast and unreachable-catch detection
  • Packaging:
    • Bundled analyzer binaries for linux, macOS and windows, extracted at runtime
  • Parser:
    • Replaced the JDT parser stack (JDTParser, JDTAstConverter) with SemSitter-backed JParserConfig batch parsing

This will update automatically on new commits.

Main sources still compile against empty trees and symbols. Tests are expected to fail until a semsitter parser fills them in.
Run one SemSitter scan (with the classpath) and its semantic dump over the
batch, then parse each file with tree-sitter-java and attach symbols,
resolved occurrences, graph call targets and graph facts by byte span.
…uery it

Rust: a serve mode answering analyze, cst, parse and quit requests, and cst
and parse commands printing the CST of files. Java: SemsitterBridge client.
A new expression whose type reference is not a constructor used to leave a class or enum-field symbol on the identifier, and rules that cast it aborted the analysis.
…range

Leaf primitive types and braces nested under a block were turned into tokens at the wrong place, so highlighting and issue ranges fell outside the line.
scoped_type_identifier was not a type kind, so a generic type like Map.Entry<K, V> reached type() with a null child and the translator threw.
Top-level methods were emitted as compilation-unit members, so visitors that look up the enclosing class crashed on Java 25 compact files.
The scanner drops issues whose location has no text range, which discarded every file-level finding such as S105.
Catch-clause variables had no "type" field in the CST, so every catch parameter fell back to an inferred "var" token at line 1, corrupting highlighting and file offsets downstream. Multi-catch types are now translated as a union type.

Type parameters were also registered in the declarations map used by Symbol.declaration(). A field or variable typed with a type parameter resolves its type symbol to that type parameter, and TypeSymbol.declaration() unconditionally casts to ClassTree, so any check reading annotations on such a type crashed. The ECJ-based parser never declared type-parameter bindings; do the same here.
Member lookup matched a call's actual argument count against a method's declared parameter count exactly, so any varargs call passing more arguments than declared parameters (e.g. String.format with several arguments) failed to resolve, leaving parameterTypes() empty and crashing every check that indexes into it. Matching now falls back to a varargs-aware comparison when no exact-arity member is found.

The semsitter bridge's type dump never reports whether a method is varargs, so isVarArgsMethod() stayed false even for genuine varargs library methods. It's now inferred from the declaration's last parameter (ellipsis token) when available, or from the last parameter type being an array otherwise.
A variable or field typed with a type parameter resolves its type symbol to that type parameter's own TypeSymbol. declaration() unconditionally cast the stored declaration tree to ClassTree, which crashed for such symbols. Now declaration() checks the tree type and returns null instead of casting blindly, matching the API's @nullable contract.
The pipe only carries a ping byte so the frontend blocks until the document is ready, and the CST, semantic dump and graph facts stay in the shared mapping. --dump-json still writes them as JSON for debugging.
The UCFG classpath moved to JVM settings upstream and was never read here because UCFG emission is off. SemSitter now resolves the call in the CST test on its own, so the reference no longer comes from the call-graph fallback.
Comment on lines +258 to +262
if (request.has("javaLevel") && !settings.has("jvm.java_version")) {
JsonArray level = new JsonArray();
level.add(request.get("javaLevel").getAsString());
settings.add("jvm.java_version", level);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Performance: Background module scan is discarded once javaLevel is in the settings

prepareAsync (AnalyzerSession.java:97-116) builds its request without javaLevel, and publishModule in JavaFrontend never adds jvm.java_version to the module settings. So the background scan stores typesSettings with no Java version. When sonar.java.source is set (most real projects), the first batch request goes through SemsitterBridge.analyze(..., javaLevel), and settings() now adds jvm.java_version (lines 258-262). The settings key no longer matches, so analyze calls stop(); start(root) and runs full-gen again. That throws away the scan prefetched ahead of the first file and does the module scan twice. Before this commit the prepare and batch settings were the same. Fix: send the same level from the prepare path, for example by publishing jvm.java_version from sonar.java.source in publishModule (the module setting wins over javaLevel), or by adding javaLevel to the prepareAsync request.

Publish the Java level with the module settings so the prepare scan and the batch requests share a settings key:

config.get("sonar.java.source").map(String::trim).filter(v -> !v.isEmpty())
  .ifPresent(v -> settings.put("jvm.java_version", List.of(v.startsWith("1.") ? v.substring(2) : v)));
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Comment on lines +46 to +60
public static JavaTree.CompilationUnitTreeImpl parse(String version, String unitName, String source) {
return parse(version, unitName, source, List.of());
}

/** {@link #parse(String, String, String)} with a classpath, as ECJ attributed single sources. */
public static JavaTree.CompilationUnitTreeImpl parse(String version, String unitName, String source, List<File> classpath) {
Objects.requireNonNull(version);
Objects.requireNonNull(unitName);
Objects.requireNonNull(source);
Path directory = null;
try {
SemsitterBridge bridge = unitBridge();
directory = privateTempDirectory();
Path file = directory.resolve(Path.of(unitName.endsWith(".java") ? unitName : (unitName + ".java")).getFileName().toString());
Files.writeString(file, source, StandardCharsets.UTF_8);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Cross-Repo: SE test helper JParserTestUtils.parse won't compile against this frontend

Fix here and in SonarSource/sonar-java-symbolic-execution

Breaks in: SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java - JParserTestUtils.parse

Breaks in: SonarSource/sonar-java-symbolic-execution, in java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java (JParserTestUtils.parse).

That helper builds an ECJ parser with JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser() and passes it to the static JParser.parse(parser, version, unitName, source). This PR turns JParser into a final utility class. Its only single-source entry points are parse(String version, String unitName, String source) and parse(String, String, String, List<File>) (JParser.java:46-93), and both drive the SemSitter analyzer. The re-added JParserConfig has no astParser() method, and ECJ is no longer how parsing happens. So once the SE repo bumps its pinned java-frontend to a build with this change, its test helper no longer compiles and no SE test can build. The master base already lacked these APIs, but the SE repo still pins a release that has them. Dropping ECJ is the point of this PoC, so the consumer is the side that has to move. Even after it moves, its tests need an analyzer executable, either the packaged one, -Dsonar.java.semsitter.executable, or SEMSITTER_ANALYZER.

Here: Before releasing, list the removal of JParserConfig#astParser() and of the static JParser.parse(ASTParser/JParser, …) in the release or migration notes. Name JParser.parse(version, unitName, source, classpath) as the replacement, and note that MAXIMUM_SUPPORTED_JAVA_VERSION lives on JParserConfig. Coordinate with sonar-java-symbolic-execution so it does not bump java-frontend before it has migrated.

There: In JParserTestUtils.parse, remove the astParser() call and call the four-argument string-based JParser.parse with the classpath. Make sure the test JVM can find a SemSitter analyzer (the packaged binary or sonar.java.semsitter.executable).

In SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java:

public static CompilationUnitTree parse(String unitName, String source, List<File> classpath) {
  JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
  return JParser.parse(version.effectiveJavaVersionAsString(), unitName, source, classpath);
}

Was this helpful? React with 👍 / 👎

import org.sonar.java.semsitter.SemsitterBridge;

public interface JParser {
public final class JParser {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ Cross-PR: JDK release option from #6459 loses its consumer and stops applying

Breaks when #6459 merges to master, this PR is rebased on top of it, and a project sets sonar.java.useJdkReleaseOption to analyze for an older Java release.

How

  1. #6459 sends shouldUseJdkReleaseOption through JParser.Mode.create(..., useRelease) and JDTParser.create(...) (java-frontend/src/main/java/org/sonar/java/model/JParser.java:34 on #6459).
  2. This PR turns JParser into a final utility class with no Mode (java-frontend/src/main/java/org/sonar/java/model/JParser.java:37) and deletes the org.sonar.java.jdt package. JParserConfig.Mode.create takes only (javaVersion, classpath[, ignoreSplit]) (JParserConfig.java:72-81).
  3. JParserConfig.bridgeClasspath puts the running JDK's lib/modules first, and openBridge sends only a numeric Java level (JParserConfig.java:225-254). Nothing restricts the JDK API to the target release, so classes newer than the configured release stay visible to the rules.

Fix

  • This PR: Carry shouldUseJdkReleaseOption into JParserConfig.Mode.create and pass it to the analyzer (for example a jvm.* release setting), or record that the option is unsupported under SemSitter.
  • #6459: Nothing is needed before this PoC goes further. Keep the option's contract documented so the SemSitter path can implement it.

Compared 824981b with #6459 at 822819f

Was this helpful? React with 👍 / 👎

Comment on lines +225 to +239
void openBridge(List<? extends InputFile> files) {
try {
bridge = SemsitterBridge.start(JParser.bridgePath());
if (!files.isEmpty()) {
Path root = files.get(0).path().toAbsolutePath().getParent();
bridge.analyze(
root == null ? Path.of(".") : root,
files.stream().map(file -> file.path().toAbsolutePath()).toList(),
bridgeClasspath(classpath),
javaVersion.isNotSet() ? Integer.MAX_VALUE : javaVersion.asInt());
}
} catch (Exception failure) {
closeBridge();
LOG.warn("SemSitter bridge did not start ({})", failure.toString());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ Cross-PR: Sourcepath files from #6501 never reach the server's scan

Breaks when both PRs merge with this PR's AnalyzerSession replacing #6501's bridge, and sonar.java.internal.sourcepath names sources that are not analyzed.

How

  1. #6501 adds the sourcepath .java files to the files passed to bridge.analyze in openBridge (java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:224 on #6501).
  2. In this PR, JavaFrontend publishes SemsitterBridge.scanFiles(allFiles) and a module root (JavaFrontend.java:168,269).
  3. AnalyzerSession.analyze scans scanFiles and falls back to request.files only when scanFiles is empty and no module root is set (AnalyzerSession.java:145-152). The extra sourcepath files are dropped, so project types they declare stay unresolved.

Fix

  • This PR: Merge the sourcepath files into the list given to SemsitterBridge.scanFiles, or have analyze union request.files with scanFiles.
  • #6501: Port to the JSON AnalyzerSession API: the 4-argument analyze and bridgeClasspath, with sourcepath files sent through scanFiles.

Compared 824981b with #6501 at 3e91a4d

Was this helpful? React with 👍 / 👎

Comment on lines +1597 to +1604
// a parameterized return keeps its arguments, so the member's return type is the one a call reports
if (!method.isStatic() && retType.has("args")) {
JType plain = typeFromFact(retType, Map.of(), true);
if (!plain.isUnknown() && plain.isParameterized()
&& plain.args.stream().noneMatch(argument -> argument.isCapture() || argument.isWildcard())) {
method.returnTypeSymbol(plain.symbol());
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Bug: Library return type now keeps unbound type variables such as List<E>

The new block in memberFromTypeInfo copies the invocation-time rule at JSema.java:742-748 but leaves out its !mentionsTypeVariable(plain) guard. It only rejects wildcard and capture arguments. So any instance library method whose signature names a class or method type variable now has a parameterized return type that carries a free variable: List.subList returns List<E>, Map.entrySet returns Set<Map.Entry<K,V>>, and Stream.map returns Stream<R>. Before this change the return type was the erased descriptor type.

That stored type reaches call sites through the fallback at line 750 of invocationReturnType. If the receiver is raw but not written raw (an unqualified subList(0, 1) in class Foo extends ArrayList, or a qualifier returned by another call), classSubstitution is empty and SemsitterTranslator does not call rawReturn, so the call now evaluates to List<E> instead of List. The same fallback also returns the leaking type (e.g. Stream<R>) whenever method type inference fails, because the earlier branch rejected it as leaks. A free type variable in an expression type is new, incorrect behavior that checks will see.

Fix: add the same !mentionsTypeVariable(plain) guard used at line 745, so that only fully concrete arguments, like the List<String> in the new test, are kept.

Keep the declared arguments only when they are concrete, matching the invocation-time rule.:

if (!method.isStatic() && retType.has("args")) {
  JType plain = typeFromFact(retType, Map.of(), true);
  if (!plain.isUnknown() && plain.isParameterized() && !mentionsTypeVariable(plain)
    && plain.args.stream().noneMatch(argument -> argument.isCapture() || argument.isWildcard())) {
    method.returnTypeSymbol(plain.symbol());
  }
}
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Comment on lines +1971 to +1984
private boolean takesNoFunction(Symbol.MethodSymbol method, Set<Integer> functions) {
List<Type> parameters = method.parameterTypes();
for (int index : functions) {
if (index >= parameters.size()) {
continue;
}
Type target = parameters.get(index).erasure();
if (target.isPrimitive() || target.isArray() || (target.isClass() && !target.isUnknown() && !target.isTypeVar() && !target.symbol().isUnknown()
&& (!target.symbol().isInterface() || (declaresType(target.fullyQualifiedName()) && functionalMethod(target) == null)))) {
return true;
}
}
return false;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Bug: Lambda overload filter drops functional interfaces with default overrides

takesNoFunction now throws out any declared interface for which functionalMethod returns null. collectAbstractMethods only ever adds abstract methods with putIfAbsent. It never lets a default method in a subinterface hide the abstract method it overrides from a parent. Take Guava's Predicate<T>: it declares abstract apply(T) and overrides java.util.function.Predicate.test(T) with a default method. The code ends up collecting both apply/1 and test/1, so functionalMethod returns null and the interface is treated as non-functional.

As a result, a call like Iterables.filter(list, x -> ...) loses its Predicate overload (the Class<T> overload is dropped as well). declaredMethod then returns null and a call that used to resolve is now unresolved. Before this commit an interface parameter was never filtered out.

Fix: when collectAbstractMethods walks the hierarchy, skip any name/arity key that a nearer (sub)interface already declares as a non-abstract (default) method. Alternatively, only filter the candidate when the interface has no abstract methods at all.

Record keys of default methods met first so inherited abstract methods they override are not counted:

private void collectAbstractMethods(String ownerFqn, Set<String> seen, Map<String, JMethodSymbol> found, Set<String> overridden) {
  ...
  if (memberFromTypeInfo(typeSymbol(ownerFqn), element.getAsJsonObject()) instanceof JMethodSymbol method && !method.isUnknown()) {
    String key = method.name() + "/" + method.parameterTypes().size();
    if (!method.isAbstract()) { overridden.add(key); }
    else if (!overridden.contains(key) && !OBJECT_METHODS.contains(key)) { found.putIfAbsent(key, method); }
  }
  ...
}
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Comment on lines +153 to +167
@Test
void constantOfAFieldFromTypeInfoIsKept() {
sema.ingestDocument(JsonParser.parseString("""
{"symbols": [
{"id": "p.Cfg", "kind": "type", "name": "Cfg"},
{"id": "p.Cfg#NAME", "kind": "field", "name": "NAME", "owner": "p.Cfg"}]}
""").getAsJsonObject());
JTypeSymbol owner = (JTypeSymbol) sema.resolve("p.Cfg");
JsonObject member = JsonParser.parseString("""
{"id": "p.Cfg#NAME", "name": "NAME", "k": "f", "constant": "abc"}
""").getAsJsonObject();

Symbol field = sema.memberFromTypeInfo(owner, member);

assertThat(((Symbol.VariableSymbol) field).constantValue()).isPresent();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: Test's second assertion never exercises servedConstant

In constantOfAFieldFromTypeInfoIsKept, the first call sema.memberFromTypeInfo(owner, member) already sets the constant on the cached p.Cfg#NAME symbol through field.constantValue(...). resolve then returns that same instance from symbols. The later ingestTypes + resolve("p.Cfg#NAME").constantValue() assertion passes on the cached value without ever reaching JVariableSymbol.constantValue() → servedConstant. The comment says this part tests "a field only ever read through its symbol", so that path is untested. Fix: move the second part into its own test with a fresh JSema and don't call memberFromTypeInfo first.

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🔴 High risk 1 closed / 12 findings

Replacing the JDT parser and semantic model with SemSitter across the Java frontend could broadly alter parsing and rule results for analyzed projects.

Replaces the JDT parser stack with a SemSitter-backed analyzer session, bundling per-platform analyzer binaries, extending JSema type resolution and CompilerWarnings, and adding a long-lived AnalyzerSession with a shared scan.

  • Changes requested: several open findings need fixes before merge.
  • Duplicate MASKED_CATCH warnings on the same catch clause produce duplicated issues in CompilerWarnings.java.
  • The macOS x86_64 analyzer resource is mapped in EmbeddedAnalyzer.java but not packaged, so Intel Mac analysis fails with a misleading message.
  • Single-source parses via JParser.parse restart the shared analyzer process on every call, and the shutdown hook in AnalyzerSession.java can block JVM exit.
  • The background module scan is discarded when javaLevel is present, because the prepare and batch settings no longer match.
  • JParserTestUtils.parse in the symbolic-execution repo will not compile against the removed astParser() API, and the removal needs migration notes.
  • #6459 JDK release option and #6501 sourcepath files lose their consumers in this PR.
  • JSema library return types keep unbound type variables such as List<E>, and the lambda overload filter drops functional interfaces that have default overrides.
  • The JSemaTest constant assertion does not exercise servedConstant.

Resolved this session: file-by-file parsing now checks cancellation after each file.

⚠️ Bug: Same catch type gets two MASKED_CATCH warnings, so issues are duplicated

📄 java-frontend/src/main/java/org/sonar/java/model/CompilerWarnings.java:268-282

CompilerWarnings now produces MASKED_CATCH warnings in two passes. visitTryStatement adds "...is already handled by the catch block for X" when an alternative is a subtype of an earlier catch. reportUnreachableCatches(TryStatementTree) adds "...Only more specific exceptions are thrown..." when no leftover thrown type reaches a checked catch. Take try { new FileInputStream(f); } catch (IOException e) {} catch (FileNotFoundException e) {}. Pass 1 flags FileNotFoundException as hidden by IOException. In pass 2, the IOException catch removes FileNotFoundException from remaining, so the second catch gets flagged again. The two messages differ, and JProblem.equals compares type+message, so both survive the LinkedHashSet. UnreachableCatchCheck.checkWarnings adds the type tree to typesWithWarnings once per matching warning, so the check raises two identical issues on the same catch clause. ECJ reports only the first diagnostic. Fix: in the second pass, skip alternatives that already have a MASKED_CATCH warning.

⚠️ Bug: Intel macOS maps to an analyzer resource that is not packaged

📄 java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedAnalyzer.java:40-54 📄 java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedAnalyzer.java:73-86

EmbeddedAnalyzer.resourceName returns /semsitter/analyzer-macos-x64.xz for macOS on x86_64. Only four analyzers are packaged, though: linux-aarch64, linux-x64, macos-aarch64 and windows-x64. The old bridge did ship bridge-macos-x64.xz. On an Intel Mac without sonar.java.semsitter.executable/SEMSITTER_ANALYZER, every analysis therefore fails with "packaged analyzer ... is missing from the plugin". That message suggests a broken plugin, when the real cause is an unsupported platform. Fix: package a macos-x64 analyzer, or return null for that platform so the user gets the 'no packaged analyzer for <os> <arch>: set -Dsonar.java.semsitter.executable' guidance.

⚠️ Performance: Background module scan is discarded once javaLevel is in the settings

📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:258-262 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:97-111 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:130-144 📄 java-frontend/src/main/java/org/sonar/java/JavaFrontend.java:265-279 📄 java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java:78-90

prepareAsync (AnalyzerSession.java:97-116) builds its request without javaLevel, and publishModule in JavaFrontend never adds jvm.java_version to the module settings. So the background scan stores typesSettings with no Java version. When sonar.java.source is set (most real projects), the first batch request goes through SemsitterBridge.analyze(..., javaLevel), and settings() now adds jvm.java_version (lines 258-262). The settings key no longer matches, so analyze calls stop(); start(root) and runs full-gen again. That throws away the scan prefetched ahead of the first file and does the module scan twice. Before this commit the prepare and batch settings were the same. Fix: send the same level from the prepare path, for example by publishing jvm.java_version from sonar.java.source in publishModule (the module setting wins over javaLevel), or by adding javaLevel to the prepareAsync request.

Publish the Java level with the module settings so the prepare scan and the batch requests share a settings key
config.get("sonar.java.source").map(String::trim).filter(v -> !v.isEmpty())
  .ifPresent(v -> settings.put("jvm.java_version", List.of(v.startsWith("1.") ? v.substring(2) : v)));
⚠️ Cross-Repo: SE test helper JParserTestUtils.parse won't compile against this frontend

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:46-60 📄 java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:42-56

Fix here and in SonarSource/sonar-java-symbolic-execution

Breaks in: SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java - JParserTestUtils.parse

Breaks in: SonarSource/sonar-java-symbolic-execution, in java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java (JParserTestUtils.parse).

That helper builds an ECJ parser with JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser() and passes it to the static JParser.parse(parser, version, unitName, source). This PR turns JParser into a final utility class. Its only single-source entry points are parse(String version, String unitName, String source) and parse(String, String, String, List<File>) (JParser.java:46-93), and both drive the SemSitter analyzer. The re-added JParserConfig has no astParser() method, and ECJ is no longer how parsing happens. So once the SE repo bumps its pinned java-frontend to a build with this change, its test helper no longer compiles and no SE test can build. The master base already lacked these APIs, but the SE repo still pins a release that has them. Dropping ECJ is the point of this PoC, so the consumer is the side that has to move. Even after it moves, its tests need an analyzer executable, either the packaged one, -Dsonar.java.semsitter.executable, or SEMSITTER_ANALYZER.

Here: Before releasing, list the removal of JParserConfig#astParser() and of the static JParser.parse(ASTParser/JParser, …) in the release or migration notes. Name JParser.parse(version, unitName, source, classpath) as the replacement, and note that MAXIMUM_SUPPORTED_JAVA_VERSION lives on JParserConfig. Coordinate with sonar-java-symbolic-execution so it does not bump java-frontend before it has migrated.

There: In JParserTestUtils.parse, remove the astParser() call and call the four-argument string-based JParser.parse with the classpath. Make sure the test JVM can find a SemSitter analyzer (the packaged binary or sonar.java.semsitter.executable).

In SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java:

public static CompilationUnitTree parse(String unitName, String source, List<File> classpath) {
  JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
  return JParser.parse(version.effectiveJavaVersionAsString(), unitName, source, classpath);
}
⚠️ Bug: Library return type now keeps unbound type variables such as List<E>

📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:1597-1604 📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:742-756

The new block in memberFromTypeInfo copies the invocation-time rule at JSema.java:742-748 but leaves out its !mentionsTypeVariable(plain) guard. It only rejects wildcard and capture arguments. So any instance library method whose signature names a class or method type variable now has a parameterized return type that carries a free variable: List.subList returns List<E>, Map.entrySet returns Set<Map.Entry<K,V>>, and Stream.map returns Stream<R>. Before this change the return type was the erased descriptor type.

That stored type reaches call sites through the fallback at line 750 of invocationReturnType. If the receiver is raw but not written raw (an unqualified subList(0, 1) in class Foo extends ArrayList, or a qualifier returned by another call), classSubstitution is empty and SemsitterTranslator does not call rawReturn, so the call now evaluates to List<E> instead of List. The same fallback also returns the leaking type (e.g. Stream<R>) whenever method type inference fails, because the earlier branch rejected it as leaks. A free type variable in an expression type is new, incorrect behavior that checks will see.

Fix: add the same !mentionsTypeVariable(plain) guard used at line 745, so that only fully concrete arguments, like the List<String> in the new test, are kept.

Keep the declared arguments only when they are concrete, matching the invocation-time rule.
if (!method.isStatic() && retType.has("args")) {
  JType plain = typeFromFact(retType, Map.of(), true);
  if (!plain.isUnknown() && plain.isParameterized() && !mentionsTypeVariable(plain)
    && plain.args.stream().noneMatch(argument -> argument.isCapture() || argument.isWildcard())) {
    method.returnTypeSymbol(plain.symbol());
  }
}
⚠️ Bug: Lambda overload filter drops functional interfaces with default overrides

📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:1971-1984 📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:1994-2008

takesNoFunction now throws out any declared interface for which functionalMethod returns null. collectAbstractMethods only ever adds abstract methods with putIfAbsent. It never lets a default method in a subinterface hide the abstract method it overrides from a parent. Take Guava's Predicate<T>: it declares abstract apply(T) and overrides java.util.function.Predicate.test(T) with a default method. The code ends up collecting both apply/1 and test/1, so functionalMethod returns null and the interface is treated as non-functional.

As a result, a call like Iterables.filter(list, x -> ...) loses its Predicate overload (the Class<T> overload is dropped as well). declaredMethod then returns null and a call that used to resolve is now unresolved. Before this commit an interface parameter was never filtered out.

Fix: when collectAbstractMethods walks the hierarchy, skip any name/arity key that a nearer (sub)interface already declares as a non-abstract (default) method. Alternatively, only filter the candidate when the interface has no abstract methods at all.

Record keys of default methods met first so inherited abstract methods they override are not counted
private void collectAbstractMethods(String ownerFqn, Set<String> seen, Map<String, JMethodSymbol> found, Set<String> overridden) {
  ...
  if (memberFromTypeInfo(typeSymbol(ownerFqn), element.getAsJsonObject()) instanceof JMethodSymbol method && !method.isUnknown()) {
    String key = method.name() + "/" + method.parameterTypes().size();
    if (!method.isAbstract()) { overridden.add(key); }
    else if (!overridden.contains(key) && !OBJECT_METHODS.contains(key)) { found.putIfAbsent(key, method); }
  }
  ...
}
💡 Performance: Each single-source parse restarts the shared analyzer process

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:51-65 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:130-137 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:170-184

JParser.parse(version, unitName, source, classpath) writes the source into a new private temp directory and calls bridge.analyze(directory, ...) on the JVM-wide shared AnalyzerSession. When no module root is set, projectRoot returns that temp directory. It differs on every call, so analyze hits !root.equals(startedRoot) and runs stop() + start(). That kills the process, clears every type cache, and pays the handshake and JDK settings load again for each unit. Because the session is shared, this also tears down a server that another tree's lazy typeLoader is still using. This path is the fallback in JParserConfig.parseFile when bridge == null, and every test helper uses it. Fix: reuse one fixed per-JVM scratch root for single-source parses (and rescan the files only), or give single-source parses their own session.

💡 Quality: Shutdown hook can block JVM exit on the module scan or the server

📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:97-111 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:451-464

AnalyzerSession.shared registers quit as a shutdown hook. quit is synchronized and calls process.waitFor() with no timeout. prepareAsync runs session.analyze (the synchronized full-gen scan) on a daemon thread. If the JVM exits during that scan, for example a cancelled analysis on a large module, the hook waits for the monitor until the scan finishes. It then waits with no limit for the server to exit after quit. Fix: bound the wait (waitFor(timeout) then destroyForcibly()), and avoid taking the session lock in the hook, for example by destroying the process directly.

ℹ️ Cross-PR: JDK release option from #6459 loses its consumer and stops applying

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:37 📄 java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:72-81 📄 java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:225-239

Breaks when #6459 merges to master, this PR is rebased on top of it, and a project sets sonar.java.useJdkReleaseOption to analyze for an older Java release.

How

  1. #6459 sends shouldUseJdkReleaseOption through JParser.Mode.create(..., useRelease) and JDTParser.create(...) (java-frontend/src/main/java/org/sonar/java/model/JParser.java:34 on #6459).
  2. This PR turns JParser into a final utility class with no Mode (java-frontend/src/main/java/org/sonar/java/model/JParser.java:37) and deletes the org.sonar.java.jdt package. JParserConfig.Mode.create takes only (javaVersion, classpath[, ignoreSplit]) (JParserConfig.java:72-81).
  3. JParserConfig.bridgeClasspath puts the running JDK's lib/modules first, and openBridge sends only a numeric Java level (JParserConfig.java:225-254). Nothing restricts the JDK API to the target release, so classes newer than the configured release stay visible to the rules.

Fix

  • This PR: Carry shouldUseJdkReleaseOption into JParserConfig.Mode.create and pass it to the analyzer (for example a jvm.* release setting), or record that the option is unsupported under SemSitter.
  • #6459: Nothing is needed before this PoC goes further. Keep the option's contract documented so the SemSitter path can implement it.

Compared 824981b with #6459 at 822819f

ℹ️ Cross-PR: Sourcepath files from #6501 never reach the server's scan

📄 java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:225-239 📄 java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:145-152 📄 java-frontend/src/main/java/org/sonar/java/JavaFrontend.java:168

Breaks when both PRs merge with this PR's AnalyzerSession replacing #6501's bridge, and sonar.java.internal.sourcepath names sources that are not analyzed.

How

  1. #6501 adds the sourcepath .java files to the files passed to bridge.analyze in openBridge (java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:224 on #6501).
  2. In this PR, JavaFrontend publishes SemsitterBridge.scanFiles(allFiles) and a module root (JavaFrontend.java:168,269).
  3. AnalyzerSession.analyze scans scanFiles and falls back to request.files only when scanFiles is empty and no module root is set (AnalyzerSession.java:145-152). The extra sourcepath files are dropped, so project types they declare stay unresolved.

Fix

  • This PR: Merge the sourcepath files into the list given to SemsitterBridge.scanFiles, or have analyze union request.files with scanFiles.
  • #6501: Port to the JSON AnalyzerSession API: the 4-argument analyze and bridgeClasspath, with sourcepath files sent through scanFiles.

Compared 824981b with #6501 at 3e91a4d

💡 Quality: Test's second assertion never exercises servedConstant

📄 java-frontend/src/test/java/org/sonar/java/model/JSemaTest.java:153-167 📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:344-351 📄 java-frontend/src/main/java/org/sonar/java/model/JSema.java:1546-1549

In constantOfAFieldFromTypeInfoIsKept, the first call sema.memberFromTypeInfo(owner, member) already sets the constant on the cached p.Cfg#NAME symbol through field.constantValue(...). resolve then returns that same instance from symbols. The later ingestTypes + resolve("p.Cfg#NAME").constantValue() assertion passes on the cached value without ever reaching JVariableSymbol.constantValue() → servedConstant. The comment says this part tests "a field only ever read through its symbol", so that path is untested. Fix: move the second part into its own test with a fresh JSema and don't call memberFromTypeInfo first.

✅ 1 closed
✅ Bug: File-by-file parse now runs a file before checking for cancellation

📄 java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:120-129 📄 java-frontend/src/test/java/org/sonar/java/model/JParserTest.java:540-554
In JParserConfig file-by-file parse, the isCanceled check moved from before each file to after action.accept(...). If the analysis is already cancelled, the first file still gets parsed and handed to the checks, and only then is a CancellationException thrown. Two existing tests encode the old contract. test_is_canceled_is_called_file_by_file passes () -> true and asserts results is empty: it now gets one result and an uncaught exception. test_is_canceled_is_called_before_each_action_file_by_file checks that isCanceled is called before each accept in order, which the new code also breaks. Fix: check for cancellation at the top of the loop, before executionTimeReport.start, and throw or break there.

🤖 Prompt for agents
Code Review: Replaces the JDT parser stack with a SemSitter-backed analyzer session, bundling per-platform analyzer binaries, extending JSema type resolution and CompilerWarnings, and adding a long-lived AnalyzerSession with a shared scan.
  
  - Changes requested: several open findings need fixes before merge.
  - Duplicate MASKED_CATCH warnings on the same catch clause produce duplicated issues in `CompilerWarnings.java`.
  - The macOS x86_64 analyzer resource is mapped in `EmbeddedAnalyzer.java` but not packaged, so Intel Mac analysis fails with a misleading message.
  - Single-source parses via `JParser.parse` restart the shared analyzer process on every call, and the shutdown hook in `AnalyzerSession.java` can block JVM exit.
  - The background module scan is discarded when `javaLevel` is present, because the prepare and batch settings no longer match.
  - `JParserTestUtils.parse` in the symbolic-execution repo will not compile against the removed `astParser()` API, and the removal needs migration notes.
  - `#6459` JDK release option and `#6501` sourcepath files lose their consumers in this PR.
  - `JSema` library return types keep unbound type variables such as `List<E>`, and the lambda overload filter drops functional interfaces that have default overrides.
  - The `JSemaTest` constant assertion does not exercise `servedConstant`.
  
  Resolved this session: file-by-file parsing now checks cancellation after each file.

1. ⚠️ Bug: Same catch type gets two MASKED_CATCH warnings, so issues are duplicated
   Files: java-frontend/src/main/java/org/sonar/java/model/CompilerWarnings.java:268-282

   `CompilerWarnings` now produces MASKED_CATCH warnings in two passes. `visitTryStatement` adds "...is already handled by the catch block for X" when an alternative is a subtype of an earlier catch. `reportUnreachableCatches(TryStatementTree)` adds "...Only more specific exceptions are thrown..." when no leftover thrown type reaches a checked catch. Take `try { new FileInputStream(f); } catch (IOException e) {} catch (FileNotFoundException e) {}`. Pass 1 flags `FileNotFoundException` as hidden by `IOException`. In pass 2, the `IOException` catch removes `FileNotFoundException` from `remaining`, so the second catch gets flagged again. The two messages differ, and `JProblem.equals` compares type+message, so both survive the `LinkedHashSet`. `UnreachableCatchCheck.checkWarnings` adds the type tree to `typesWithWarnings` once per matching warning, so the check raises two identical issues on the same catch clause. ECJ reports only the first diagnostic. Fix: in the second pass, skip alternatives that already have a MASKED_CATCH warning.

2. ⚠️ Bug: Intel macOS maps to an analyzer resource that is not packaged
   Files: java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedAnalyzer.java:40-54, java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedAnalyzer.java:73-86

   `EmbeddedAnalyzer.resourceName` returns `/semsitter/analyzer-macos-x64.xz` for macOS on x86_64. Only four analyzers are packaged, though: linux-aarch64, linux-x64, macos-aarch64 and windows-x64. The old bridge did ship `bridge-macos-x64.xz`. On an Intel Mac without `sonar.java.semsitter.executable`/`SEMSITTER_ANALYZER`, every analysis therefore fails with "packaged analyzer ... is missing from the plugin". That message suggests a broken plugin, when the real cause is an unsupported platform. Fix: package a macos-x64 analyzer, or return null for that platform so the user gets the 'no packaged analyzer for <os> <arch>: set -Dsonar.java.semsitter.executable' guidance.

3. 💡 Performance: Each single-source parse restarts the shared analyzer process
   Files: java-frontend/src/main/java/org/sonar/java/model/JParser.java:51-65, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:130-137, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:170-184

   `JParser.parse(version, unitName, source, classpath)` writes the source into a new private temp directory and calls `bridge.analyze(directory, ...)` on the JVM-wide shared `AnalyzerSession`. When no module root is set, `projectRoot` returns that temp directory. It differs on every call, so `analyze` hits `!root.equals(startedRoot)` and runs `stop()` + `start()`. That kills the process, clears every type cache, and pays the handshake and JDK settings load again for each unit. Because the session is shared, this also tears down a server that another tree's lazy `typeLoader` is still using. This path is the fallback in `JParserConfig.parseFile` when `bridge == null`, and every test helper uses it. Fix: reuse one fixed per-JVM scratch root for single-source parses (and rescan the files only), or give single-source parses their own session.

4. 💡 Quality: Shutdown hook can block JVM exit on the module scan or the server
   Files: java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:97-111, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:451-464

   `AnalyzerSession.shared` registers `quit` as a shutdown hook. `quit` is `synchronized` and calls `process.waitFor()` with no timeout. `prepareAsync` runs `session.analyze` (the synchronized `full-gen` scan) on a daemon thread. If the JVM exits during that scan, for example a cancelled analysis on a large module, the hook waits for the monitor until the scan finishes. It then waits with no limit for the server to exit after `quit`. Fix: bound the wait (`waitFor(timeout)` then `destroyForcibly()`), and avoid taking the session lock in the hook, for example by destroying the process directly.

5. ⚠️ Performance: Background module scan is discarded once javaLevel is in the settings
   Files: java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:258-262, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:97-111, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:130-144, java-frontend/src/main/java/org/sonar/java/JavaFrontend.java:265-279, java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java:78-90

   `prepareAsync` (AnalyzerSession.java:97-116) builds its request without `javaLevel`, and `publishModule` in JavaFrontend never adds `jvm.java_version` to the module settings. So the background scan stores `typesSettings` with no Java version. When `sonar.java.source` is set (most real projects), the first batch request goes through `SemsitterBridge.analyze(..., javaLevel)`, and `settings()` now adds `jvm.java_version` (lines 258-262). The settings key no longer matches, so `analyze` calls `stop(); start(root)` and runs `full-gen` again. That throws away the scan prefetched ahead of the first file and does the module scan twice. Before this commit the prepare and batch settings were the same. Fix: send the same level from the prepare path, for example by publishing `jvm.java_version` from `sonar.java.source` in `publishModule` (the module setting wins over `javaLevel`), or by adding `javaLevel` to the `prepareAsync` request.

   Fix (Publish the Java level with the module settings so the prepare scan and the batch requests share a settings key):
   config.get("sonar.java.source").map(String::trim).filter(v -> !v.isEmpty())
     .ifPresent(v -> settings.put("jvm.java_version", List.of(v.startsWith("1.") ? v.substring(2) : v)));

6. ⚠️ Cross-Repo: SE test helper JParserTestUtils.parse won't compile against this frontend
   Files: java-frontend/src/main/java/org/sonar/java/model/JParser.java:46-60, java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:42-56

   **Fix here and in [`SonarSource/sonar-java-symbolic-execution`](https://github.com/SonarSource/sonar-java-symbolic-execution)**
   
   **Breaks in:** [`SonarSource/sonar-java-symbolic-execution`](https://github.com/SonarSource/sonar-java-symbolic-execution) - [`java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java`](https://github.com/SonarSource/sonar-java-symbolic-execution/blob/HEAD/java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java) - `JParserTestUtils.parse`
   
   **Breaks in:** `SonarSource/sonar-java-symbolic-execution`, in `java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java` (`JParserTestUtils.parse`).
   
   That helper builds an ECJ parser with `JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser()` and passes it to the static `JParser.parse(parser, version, unitName, source)`. This PR turns `JParser` into a final utility class. Its only single-source entry points are `parse(String version, String unitName, String source)` and `parse(String, String, String, List<File>)` (JParser.java:46-93), and both drive the SemSitter analyzer. The re-added `JParserConfig` has no `astParser()` method, and ECJ is no longer how parsing happens. So once the SE repo bumps its pinned java-frontend to a build with this change, its test helper no longer compiles and no SE test can build. The master base already lacked these APIs, but the SE repo still pins a release that has them. Dropping ECJ is the point of this PoC, so the consumer is the side that has to move. Even after it moves, its tests need an analyzer executable, either the packaged one, `-Dsonar.java.semsitter.executable`, or `SEMSITTER_ANALYZER`.
   
   **Here:** Before releasing, list the removal of `JParserConfig#astParser()` and of the static `JParser.parse(ASTParser/JParser, …)` in the release or migration notes. Name `JParser.parse(version, unitName, source, classpath)` as the replacement, and note that `MAXIMUM_SUPPORTED_JAVA_VERSION` lives on `JParserConfig`. Coordinate with sonar-java-symbolic-execution so it does not bump java-frontend before it has migrated.
   
   **There:** In `JParserTestUtils.parse`, remove the `astParser()` call and call the four-argument string-based `JParser.parse` with the classpath. Make sure the test JVM can find a SemSitter analyzer (the packaged binary or `sonar.java.semsitter.executable`).
   
   In SonarSource/sonar-java-symbolic-execution - `java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java`:
   
   ````
   public static CompilationUnitTree parse(String unitName, String source, List<File> classpath) {
     JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
     return JParser.parse(version.effectiveJavaVersionAsString(), unitName, source, classpath);
   }
   ````

7. ℹ️ Cross-PR: JDK release option from [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) loses its consumer and stops applying
   Files: java-frontend/src/main/java/org/sonar/java/model/JParser.java:37, java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:72-81, java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:225-239

   **Breaks when** [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) merges to master, this PR is rebased on top of it, and a project sets `sonar.java.useJdkReleaseOption` to analyze for an older Java release.
   
   **How**
   
   1. [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) sends `shouldUseJdkReleaseOption` through `JParser.Mode.create(..., useRelease)` and `JDTParser.create(...)` (`java-frontend/src/main/java/org/sonar/java/model/JParser.java:34` on [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>)).
   2. This PR turns `JParser` into a final utility class with no `Mode` (`java-frontend/src/main/java/org/sonar/java/model/JParser.java:37`) and deletes the `org.sonar.java.jdt` package. `JParserConfig.Mode.create` takes only `(javaVersion, classpath[, ignoreSplit])` (`JParserConfig.java:72-81`).
   3. `JParserConfig.bridgeClasspath` puts the running JDK's `lib/modules` first, and `openBridge` sends only a numeric Java level (`JParserConfig.java:225-254`). Nothing restricts the JDK API to the target release, so classes newer than the configured release stay visible to the rules.
   
   **Fix**
   
   - **This PR:** Carry `shouldUseJdkReleaseOption` into `JParserConfig.Mode.create` and pass it to the analyzer (for example a `jvm.*` release setting), or record that the option is unsupported under SemSitter.
   - **[#6459](<https://github.com/SonarSource/sonar-java/pull/6459>):** Nothing is needed before this PoC goes further. Keep the option's contract documented so the SemSitter path can implement it.
   
   _Compared `824981b` with [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) at `822819f`_

8. ℹ️ Cross-PR: Sourcepath files from [#6501](<https://github.com/SonarSource/sonar-java/pull/6501>) never reach the server's scan
   Files: java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:225-239, java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java:145-152, java-frontend/src/main/java/org/sonar/java/JavaFrontend.java:168

   **Breaks when** both PRs merge with this PR's AnalyzerSession replacing [#6501](<https://github.com/SonarSource/sonar-java/pull/6501>)'s bridge, and `sonar.java.internal.sourcepath` names sources that are not analyzed.
   
   **How**
   
   1. [#6501](<https://github.com/SonarSource/sonar-java/pull/6501>) adds the sourcepath `.java` files to the `files` passed to `bridge.analyze` in `openBridge` (`java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:224` on [#6501](<https://github.com/SonarSource/sonar-java/pull/6501>)).
   2. In this PR, `JavaFrontend` publishes `SemsitterBridge.scanFiles(allFiles)` and a module root (`JavaFrontend.java:168,269`).
   3. `AnalyzerSession.analyze` scans `scanFiles` and falls back to `request.files` only when `scanFiles` is empty and no module root is set (`AnalyzerSession.java:145-152`). The extra sourcepath files are dropped, so project types they declare stay unresolved.
   
   **Fix**
   
   - **This PR:** Merge the sourcepath files into the list given to `SemsitterBridge.scanFiles`, or have `analyze` union `request.files` with `scanFiles`.
   - **[#6501](<https://github.com/SonarSource/sonar-java/pull/6501>):** Port to the JSON `AnalyzerSession` API: the 4-argument `analyze` and `bridgeClasspath`, with sourcepath files sent through `scanFiles`.
   
   _Compared `824981b` with [#6501](<https://github.com/SonarSource/sonar-java/pull/6501>) at `3e91a4d`_

9. ⚠️ Bug: Library return type now keeps unbound type variables such as List<E>
   Files: java-frontend/src/main/java/org/sonar/java/model/JSema.java:1597-1604, java-frontend/src/main/java/org/sonar/java/model/JSema.java:742-756

   The new block in `memberFromTypeInfo` copies the invocation-time rule at `JSema.java:742-748` but leaves out its `!mentionsTypeVariable(plain)` guard. It only rejects wildcard and capture arguments. So any instance library method whose signature names a class or method type variable now has a parameterized return type that carries a free variable: `List.subList` returns `List<E>`, `Map.entrySet` returns `Set<Map.Entry<K,V>>`, and `Stream.map` returns `Stream<R>`. Before this change the return type was the erased descriptor type.
   
   That stored type reaches call sites through the fallback at line 750 of `invocationReturnType`. If the receiver is raw but not written raw (an unqualified `subList(0, 1)` in `class Foo extends ArrayList`, or a qualifier returned by another call), `classSubstitution` is empty and `SemsitterTranslator` does not call `rawReturn`, so the call now evaluates to `List<E>` instead of `List`. The same fallback also returns the leaking type (e.g. `Stream<R>`) whenever method type inference fails, because the earlier branch rejected it as `leaks`. A free type variable in an expression type is new, incorrect behavior that checks will see.
   
   Fix: add the same `!mentionsTypeVariable(plain)` guard used at line 745, so that only fully concrete arguments, like the `List<String>` in the new test, are kept.

   Fix (Keep the declared arguments only when they are concrete, matching the invocation-time rule.):
   if (!method.isStatic() && retType.has("args")) {
     JType plain = typeFromFact(retType, Map.of(), true);
     if (!plain.isUnknown() && plain.isParameterized() && !mentionsTypeVariable(plain)
       && plain.args.stream().noneMatch(argument -> argument.isCapture() || argument.isWildcard())) {
       method.returnTypeSymbol(plain.symbol());
     }
   }

10. ⚠️ Bug: Lambda overload filter drops functional interfaces with default overrides
   Files: java-frontend/src/main/java/org/sonar/java/model/JSema.java:1971-1984, java-frontend/src/main/java/org/sonar/java/model/JSema.java:1994-2008

   `takesNoFunction` now throws out any declared interface for which `functionalMethod` returns null. `collectAbstractMethods` only ever adds abstract methods with `putIfAbsent`. It never lets a default method in a subinterface hide the abstract method it overrides from a parent. Take Guava's `Predicate<T>`: it declares abstract `apply(T)` and overrides `java.util.function.Predicate.test(T)` with a default method. The code ends up collecting both `apply/1` and `test/1`, so `functionalMethod` returns null and the interface is treated as non-functional.
   
   As a result, a call like `Iterables.filter(list, x -> ...)` loses its `Predicate` overload (the `Class<T>` overload is dropped as well). `declaredMethod` then returns null and a call that used to resolve is now unresolved. Before this commit an interface parameter was never filtered out.
   
   Fix: when `collectAbstractMethods` walks the hierarchy, skip any name/arity key that a nearer (sub)interface already declares as a non-abstract (default) method. Alternatively, only filter the candidate when the interface has no abstract methods at all.

   Fix (Record keys of default methods met first so inherited abstract methods they override are not counted):
   private void collectAbstractMethods(String ownerFqn, Set<String> seen, Map<String, JMethodSymbol> found, Set<String> overridden) {
     ...
     if (memberFromTypeInfo(typeSymbol(ownerFqn), element.getAsJsonObject()) instanceof JMethodSymbol method && !method.isUnknown()) {
       String key = method.name() + "/" + method.parameterTypes().size();
       if (!method.isAbstract()) { overridden.add(key); }
       else if (!overridden.contains(key) && !OBJECT_METHODS.contains(key)) { found.putIfAbsent(key, method); }
     }
     ...
   }

11. 💡 Quality: Test's second assertion never exercises servedConstant
   Files: java-frontend/src/test/java/org/sonar/java/model/JSemaTest.java:153-167, java-frontend/src/main/java/org/sonar/java/model/JSema.java:344-351, java-frontend/src/main/java/org/sonar/java/model/JSema.java:1546-1549

   In `constantOfAFieldFromTypeInfoIsKept`, the first call `sema.memberFromTypeInfo(owner, member)` already sets the constant on the cached `p.Cfg#NAME` symbol through `field.constantValue(...)`. `resolve` then returns that same instance from `symbols`. The later `ingestTypes` + `resolve("p.Cfg#NAME").constantValue()` assertion passes on the cached value without ever reaching `JVariableSymbol.constantValue()` → `servedConstant`. The comment says this part tests "a field only ever read through its symbol", so that path is untested. Fix: move the second part into its own test with a fresh `JSema` and don't call `memberFromTypeInfo` first.

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

Functional validation No results

Rules 2 passed 2 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 5 inspected PRs 1 inspected
Cross-repo 5 repositories inspected 1 open impact

SonarSource/sonar-java-symbolic-execution Inspected
SonarSource/sonar-lits Inspected
SonarSource/sonar-iac Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonarlint-intellij Inspected

Cross-PR 1 PR inspected 2 open impacts

PR 6501: SONARJAVA-7236 Experiment: let semsitter read sourcepath files that are not analyzed 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.
Unblock → Override a blocking verdict and allow merging.

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

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

Was this helpful? React with 👍 / 👎 | Gitar

@antonioaversa antonioaversa changed the title SONARJAVA-7339 [DRAFT][PoC][duplicate of #6514] Serve SonarJava from udg-gen --server get-file/get-types (SemSitter PR #4952) [DO NOT MERGE][PoC] SONARJAVA-7339 Use SemSitter as semantic layer Nov 20, 2026

This branch has not been deployed

No deployments
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