Skip to content

fix(java): link new-expression to constructor and class instantiation#658

Merged
vitali87 merged 2 commits into
mainfrom
fix/java-constructor-calls
Jul 8, 2026
Merged

fix(java): link new-expression to constructor and class instantiation#658
vitali87 merged 2 commits into
mainfrom
fix/java-constructor-calls

Conversation

@vitali87

@vitali87 vitali87 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Found by dogfooding the dead-code eval on google/gson (Java's first dead-code dogfood). Drives gson core from 114 to 92 false positives by fixing the single largest class.

The class: Java new X(...) never linked to constructors

SPEC_JAVA_CALL_TYPES collected only method_invocation, never object_creation_expression, so new X(...) produced no INSTANTIATES edge to the class and no CALLS edge to the constructor. Every constructor reached only via new therefore looked dead (45 of gson's 114 false positives).

Fix (mirrors the JS new_expression / Python class-call path)

  1. Add object_creation_expression to SPEC_JAVA_CALL_TYPES.
  2. _get_call_target_name names a Java object_creation_expression by its type field (strips generic args, e.g. new ArrayList<T>() -> ArrayList; a scoped Outer.Inner is left for the resolver), routing construction through the normal resolve loop.
  3. At the emit site, a resolved class node gets INSTANTIATES plus a CALLS edge to each declared constructor via the new java_constructor_targets helper. A Java constructor is registered as a method named like its class (Foo.Foo(int)); the helper matches only constructors DIRECTLY on the class (a nested class's constructor has an extra qn segment and is excluded). Argument-type overload selection is not attempted, which is unnecessary for reachability and never fabricates a call to a non-constructor.

Tests

test_java_constructor_calls.py: a new X(...) both instantiates the class and calls the constructor; an overloaded class has every constructor reached.

Verified: gson 114 -> 92, full suite green (4427 passed), 522 Java tests pass, no cross-language effect (every new path is language == JAVA-gated).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for tracking Java object creation expressions (new Foo(...)) as call nodes. It updates constants, handles TS_OBJECT_CREATION_EXPRESSION in the call processor to resolve the class name, and routes CALLS edges to all declared constructors of the instantiated class. Additionally, a new test suite is added to verify these relationships. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR links Java new X(...) expressions to class instantiation and constructor reachability. The main changes are:

  • Adds object_creation_expression to Java call node collection.
  • Captures Java object creation types broadly, including generic and scoped type nodes.
  • Resolves Java new expressions through the existing call resolver path.
  • Emits INSTANTIATES edges to classes and CALLS edges to declared constructors.
  • Adds tests for simple, overloaded, generic, and nested constructor expressions.

Confidence Score: 5/5

Safe to merge with minimal risk.

The change is tightly scoped to Java object creation handling and keeps the new behavior language-gated. The constructor path includes guards for non-class targets and tests for the key simple, overloaded, generic, and scoped cases.

No files require special attention.

T-Rex T-Rex Logs

What T-Rex did

  • Verified the initial tool and language environment; uv 0.11.27 and Python 3.11.6 were available (per general-contract-validation-proof).
  • Attempted to run the narrow pytest command; CPython 3.12.13 was downloaded and environment creation started, but building pymgclient==1.5.1 failed due to cmake tooling being inaccessible.
  • As a result, pytest execution did not occur and constructor instantiation/CALLS edge behavior could not be validated in this environment.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
codebase_rag/constants.py Adds Java object creation expressions to the call-node types so constructor invocations enter call processing.
codebase_rag/language_spec.py Broadens the Java object-creation query to capture any type node, covering generic and scoped constructor names.
codebase_rag/parsers/call_processor.py Names Java new expressions from their type field and emits instantiation plus constructor call relationships for resolved class targets.
codebase_rag/parsers/call_resolver.py Adds direct Java constructor target discovery under a resolved class qualified name.
codebase_rag/tests/test_java_constructor_calls.py Adds tests for simple, overloaded, generic, and scoped Java constructor instantiation paths.
uv.lock Updates the editable package version from 0.0.253 to 0.0.256.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant JavaAST as Java AST
participant Query as LANGUAGE_SPECS call_query
participant Processor as CallProcessor
participant Resolver as CallResolver
participant Graph as Graph relationships

JavaAST->>Query: object_creation_expression type capture
Query->>Processor: call_node new X(...)
Processor->>Processor: extract type name and strip generic args
Processor->>Resolver: resolve_function_call(type_name)
Resolver-->>Processor: resolved class qualified name
Processor->>Graph: "INSTANTIATES caller -> class"
Processor->>Resolver: java_constructor_targets(class_qn)
Resolver-->>Processor: direct declared constructors
Processor->>Graph: "CALLS caller -> constructor(s)"
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant JavaAST as Java AST
participant Query as LANGUAGE_SPECS call_query
participant Processor as CallProcessor
participant Resolver as CallResolver
participant Graph as Graph relationships

JavaAST->>Query: object_creation_expression type capture
Query->>Processor: call_node new X(...)
Processor->>Processor: extract type name and strip generic args
Processor->>Resolver: resolve_function_call(type_name)
Resolver-->>Processor: resolved class qualified name
Processor->>Graph: "INSTANTIATES caller -> class"
Processor->>Resolver: java_constructor_targets(class_qn)
Resolver-->>Processor: direct declared constructors
Processor->>Graph: "CALLS caller -> constructor(s)"
Loading

Reviews (2): Last reviewed commit: "fix(java): capture generic and scoped ne..." | Re-trigger Greptile

Comment thread codebase_rag/constants.py
@codecov-commenter

codecov-commenter commented Jul 8, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@vitali87

vitali87 commented Jul 8, 2026

Copy link
Copy Markdown
Owner Author

@greptile review

@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

@vitali87
vitali87 merged commit 1bf491b into main Jul 8, 2026
22 checks passed
@vitali87
vitali87 deleted the fix/java-constructor-calls branch July 8, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants