Skip to content

feat(java): skip inner-class methods in discovery; revert replacement-level workarounds - #1726

Merged
misrasaurabh1 merged 4 commits into
omni-javafrom
fix/java/inner-class-helpers-filter
Mar 3, 2026
Merged

feat(java): skip inner-class methods in discovery; revert replacement-level workarounds#1726
misrasaurabh1 merged 4 commits into
omni-javafrom
fix/java/inner-class-helpers-filter

Conversation

@misrasaurabh1

@misrasaurabh1 misrasaurabh1 commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • parser.py: Add is_class_nested flag to JavaMethodNode; track class_depth in _walk_tree_for_methods (incremented on each type declaration entry). Methods at depth ≥ 2 have is_class_nested = True.
  • discovery.py: Skip methods with is_class_nested = True in _should_include_method. Inner-class methods cannot be reliably instrumented (instrumentation is name-only, not class-aware), and non-static inner classes require an outer instance unavailable in tests.
  • replacement.py: Revert the replacement-level Bug-4 workarounds that are now obsolete — removing target_class_name parameter, restoring simple first-match selection, and removing the class-name filter for helpers.
  • tests: Update existing inner-class tests to reflect the new contract; add TestInnerClassMethodFilter in test_discovery.py with four scenarios (static nested, non-static inner, outer-only, deeply nested).

Test plan

  • uv run python -m pytest tests/test_languages/test_java/test_discovery.py — new TestInnerClassMethodFilter class passes
  • uv run python -m pytest tests/test_languages/test_java/test_replacement.py — all pass (removed obsolete TestInnerClassHelperFilter)
  • uv run python -m pytest tests/test_languages/test_java/test_context.py — updated inner-class context tests pass
  • Full Java suite: 639 passed, 0 new failures

🤖 Generated with Claude Code

misrasaurabh1 and others added 4 commits March 2, 2026 23:59
…(Java)

When the optimisation target lives in a static inner class (e.g.
ObjectUnpacker inside Unpacker<T>), the LLM-generated class often wraps the
inner class inside the full outer class.  Previously, methods belonging to the
outer class were extracted as "helpers" and injected into the inner class,
causing compilation errors:

  - "non-static type variable T cannot be referenced from a static context"
  - "non-static variable offset cannot be referenced from a static context"

Two related fixes:

1. When _parse_optimization_source extracts helpers, it now skips any method
   whose class_name differs from the target method's class_name.

2. The function now accepts an optional target_class_name parameter.  When
   there are multiple methods with the same name in the generated code (e.g.
   an abstract outer-class method and the concrete inner-class override), the
   method in the target class is preferred over outer-class methods.

Fixes the Unpacker.ObjectUnpacker.getString regression from codeflash_all_3.log.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When the LLM optimises a method by introducing a new final field (e.g.
caching Arrays.hashCode in Expression.hashCode, or caching map.values() in
LuaMap.valuesIterator), it also modifies the class constructors to initialise
the field.  Previously codeflash:

  1. Added the new field to the class ✓
  2. Replaced the target method ✓
  3. Did NOT update the constructors ✗

This caused "variable X might not have been initialized" compilation errors.

Changes:
- `JavaAnalyzer.find_constructors` (+ `_walk_tree_for_constructors`,
  `_extract_constructor_info`): new parser methods to locate
  `constructor_declaration` nodes via tree-sitter.
- `JavaMethodNode.formal_parameters_text`: captures the raw parameter list
  text so constructors can be matched by signature.
- `ParsedOptimization.modified_constructors`: new field to carry constructor
  source texts that need to be replaced.
- `_parse_optimization_source`: extract constructors from the same class as
  the target method and store in `modified_constructors`.
- `_replace_constructors`: new helper that replaces constructors in the
  original source by matching on formal parameter signature.
- `replace_function`: call `_replace_constructors` after the main method
  replacement when `modified_constructors` is non-empty.

Fixes regressions observed in codeflash_all_3.log:
  LuaMap.valuesIterator, Expression.hashCode, Bin.hashCode,
  NettyTlsContext.createHandler, Pool.capacity.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…cement

fix(java): replace modified constructors when LLM adds new final fields
…level inner-class workarounds

- parser.py: add `is_class_nested` flag to `JavaMethodNode`; track
  `class_depth` in `_walk_tree_for_methods` (incremented each time a
  type declaration is entered) and set `is_class_nested = True` when
  depth ≥ 2 (method lives inside a nested/inner class)

- discovery.py: add early-exit in `_should_include_method` when
  `method.is_class_nested` is True — inner-class methods cannot be
  reliably instrumented or tested in isolation, so we skip them up-front
  rather than wasting LLM tokens on candidates that will always be
  rejected later

- replacement.py: revert Bug-4 replacement-level workarounds that are
  now obsolete:
  * remove `target_class_name` parameter from `_parse_optimization_source`
  * restore simple first-match `break` in target-method selection
  * remove class_name filter that blocked helpers from "other" classes

- tests: update `TestNestedClasses`, `TestExtractCodeContextWithInnerClasses`
  to reflect the new no-inner-class-discovery contract; remove
  `TestInnerClassHelperFilter` (superseded by discovery filter);
  add `TestInnerClassMethodFilter` in test_discovery.py with four
  scenarios covering static nested, non-static inner, outer-only, and
  deeply-nested classes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 changed the title fix(java): skip outer-class methods when target is in a static inner class feat(java): skip inner-class methods in discovery; revert replacement-level workarounds Mar 3, 2026
@misrasaurabh1
misrasaurabh1 merged commit 400cf06 into omni-java Mar 3, 2026
22 of 29 checks passed
@misrasaurabh1
misrasaurabh1 deleted the fix/java/inner-class-helpers-filter branch March 3, 2026 00:50
KRRT7 added a commit that referenced this pull request Mar 4, 2026
Inner-class methods are intentionally skipped by Java discovery
(PR #1726) since instrumentation is name-only and not class-aware.
Update test to expect False from replacement.
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.

1 participant