Skip to content

fix(java): a call reaches the same-named class the calling file can see - #3347

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-2976-java-class-visibility
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-2976-java-class-visibility

Conversation

@colbymchenry

Copy link
Copy Markdown
Owner

Summary

When two packages declare a Java class of the same name (com.x.a.Util, com.x.b.Util), a call now reaches the one the calling file can see, the way javac looks the name up: the file's own type, a single-type import, the file's package, then its on-demand (.*) imports. A receiver written with its package, com.x.b.Util.log(…), is read as that class. A name two on-demand imports both supply is ambiguous (javac rejects it), so such a call links nothing.

Fixes #2976
Fixes #3290
Fixes #3291

Root cause

Under import com.x.b.*; the first-indexed Util won in three separate paths, and a fourth ignored the package:

Fix (src/resolution/name-matcher.ts)

  • javaClassInScope(name, file): javac's order over the project's top-level classes of the name. Null for two on-demand packages (ambiguous); undefined (existing rules decide) when a nested type shares the name or one step holds copies of one fully qualified name.
  • javaQualifiedReceiverType(written): the lowercase segments before the first capitalized one are the package (com.x.b + Util, com.x.c + Outer.Inner), looked up by qualified name.
  • Used by the class strategy (null ends the call: no edge), javaTypeRef's declared-type pin, importedFqnOf (Java only) and the typed-receiver pin, and matchJavaCsharpNewReceiverCall, which now matches a class by its fully qualified name before its path.

Validation

repo files target changed same target, new metadata new / lost
jsoup (0c508f17) 332 0 2 (fully qualified org.jsoup.Jsoup.parse, 0.65 → 0.85) 0 / 0
TheAlgorithms/Java (b42e5df8) 1,693 0 0 0 / 0
elasticsearch server action + cluster + common (edac141f) 1,710 0 60 (enum and fully qualified receivers now resolve by their class, 0.65/0.7 → 0.85) 0 / 0
nowinandroid (control, Kotlin) 715 byte-identical dump
gin (control, Go) 130 byte-identical dump

These repos don't combine project wildcard imports with duplicate class names; the tests cover that.

  • Index time on the elasticsearch subtree, median of 3 interleaved runs: 34.3 s on main, 31.9 s with the fix.
  • Full suite: see below.

Contributor PRs

PRs #3138 and #3139 by @johnhldiver-beep each target half of #2976 (the static call and the chained new). This change is written from the issues and also covers #3290 and #3291 and the ambiguous case; credited in the CHANGELOG.

🤖 Generated with Claude Code

@colbymchenry
colbymchenry force-pushed the claude/issue-2976-java-class-visibility branch 9 times, most recently from 16f94a2 to 41ce332 Compare November 18, 2026 06:13
With com.x.a.Util and com.x.b.Util both in a project, a Java call under
`import com.x.b.*;` went to whichever Util was indexed first:

- `Util.log("x")`: the class strategy took every class named Util with no
  check of what the file can see (C#, VB.NET and PHP had one, Java none);
- `Util u = new Util(); u.run()`: the declared type was pinned only by an
  explicit import, and the import mappings skip on-demand imports;
- `new Util().run()`: same gap, so no class was chosen and no edge made;
- `com.x.b.Util.log("q")`: the package was never read, and the method
  was guessed by its name, whatever the file imported.

javaClassInScope now finds the class a simple name means in a file the
way javac does: the file's own top-level type, a single-type import, the
file's package, then the packages its on-demand imports name. Two
on-demand packages that both supply the name make it ambiguous, which
javac rejects, so such a call links nothing. It feeds the class strategy
(matchMethodCall), the declared-type pin (javaTypeRef), importedFqnOf
(the chained `new` and the other typed paths) and
matchJavaCsharpNewReceiverCall, which now matches a class by its fully
qualified name before its path. javaQualifiedReceiverType reads a
receiver written with its package, `com.x.b.Util` or
`com.x.c.Outer.Inner`, as that type. It decides nothing when a nested
type shares the name, or a step holds copies of one fully qualified
name, leaving those to the existing rules.

Validation: java-class-visibility.test.ts fails on main (4 of 5) and
passes with the fix; the 40 Java, Kotlin, Scala, Spring and Play test
files pass. A/B against v1.6.3 (147fe29): jsoup 2 calls, an
elasticsearch subtree (server action, cluster, common: 1,710 files) 60
calls keep their target and now resolve through the class they name
(fully qualified receivers and enum constants' types, 0.65 to 0.85),
TheAlgorithms/Java (1,693 files) unchanged; no edge moved, appeared or
went. nowinandroid (Kotlin) and gin (Go) dumps byte-identical. Index
time on the elasticsearch subtree, median of 3 interleaved: 34.3 s on
main, 31.9 s with the fix.

Fixes #2976
Fixes #3290
Fixes #3291

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry
colbymchenry force-pushed the claude/issue-2976-java-class-visibility branch from 41ce332 to 88632f1 Compare November 18, 2026 06:15
@colbymchenry
colbymchenry merged commit bc81faa into main Nov 18, 2026
@colbymchenry
colbymchenry deleted the claude/issue-2976-java-class-visibility branch November 18, 2026 06:16
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Nov 18, 2026
Upstream landed our colbymchenry#3330 (b286279), colbymchenry#3278 (df20853) and colbymchenry#3249, plus its own
fixes for issues the fork carried: Python typing.overload (colbymchenry#3349, colbymchenry#3216's
approach), Java wildcard visibility (colbymchenry#3347), colbymchenry#3272 (colbymchenry#3364), colbymchenry#2951/colbymchenry#2953.

Conflicts:
- name-matcher.ts: upstream's side for Rust (colbymchenry#3381-colbymchenry#3384) and Java (colbymchenry#3347).
  The fork keeps its module-receiver check (namespace-only defaults, a default
  that is a module's own function) and adds upstream's colbymchenry#2951 clause and
  isImportedFunctionReceiver guard. Upstream's colbymchenry#3272 block (a module default
  made with `new`) runs first, but defers when the module exports the name or
  its exports can't be read for certain, so the fork's alias edges stay.
  A parameter or local shadowing the binding is the receiver (colbymchenry#2953).
- import-resolver.ts: upstream's member-of-imported-function block, then the
  fork's guard for the remaining cases (default imports), excluding
  call/apply/bind.
- python-overloads.ts, index.ts: upstream's (duplicate memo clear dropped).
- module-importers.ts, workspace-packages.ts: both imports.
- CHANGELOG: upstream's [Unreleased] plus the fork's PHP line (colbymchenry#3327).

Tests: member-call-not-receiver's static-property case now expects
User::all (upstream colbymchenry#3380 indexes `User.all = function` as a static method).
parse-deadline's two wasm-hang cases are skipped on the fork: its patched
runtime (tree-sitter#5997) parses the input instead of hanging.

README: colbymchenry#3249, colbymchenry#3278, colbymchenry#3330, colbymchenry#3216 and colbymchenry#3138 moved to "now in upstream".

Checks: tsc 0; suite 11415 passed, 2 failed before the parse-deadline gate
(both from the patched runtime); fork-only and upstream colbymchenry#3272/colbymchenry#3380 tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant