Skip to content

fix(java): replace modified constructors when LLM adds new final fields#1727

Merged
misrasaurabh1 merged 1 commit intofix/java/inner-class-helpers-filterfrom
fix/java/constructor-replacement
Mar 3, 2026
Merged

fix(java): replace modified constructors when LLM adds new final fields#1727
misrasaurabh1 merged 1 commit intofix/java/inner-class-helpers-filterfrom
fix/java/constructor-replacement

Conversation

@misrasaurabh1
Copy link
Copy Markdown
Contributor

Summary

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 updates the class constructors to initialise that 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

parser.py

  • JavaAnalyzer.find_constructors + _walk_tree_for_constructors + _extract_constructor_info: new methods to locate constructor_declaration nodes via tree-sitter
  • JavaMethodNode.formal_parameters_text: captures the raw parameter list so constructors can be matched by signature

replacement.py

  • ParsedOptimization.modified_constructors: new field to carry constructor sources that need replacing
  • _parse_optimization_source: extract constructors from the same class as the target method
  • _replace_constructors: new helper that replaces constructors by matching on formal parameter signature, preserving indentation
  • replace_function: call _replace_constructors after the main method replacement

Test plan

  • TestConstructorReplacement::test_constructor_updated_when_new_final_field_added — reproduces Expression.hashCode
  • TestConstructorReplacement::test_constructor_updated_for_cached_collection_view — reproduces LuaMap.valuesIterator
  • All 35 test_replacement.py tests pass

Fixes regressions from codeflash_all_3.log

  • LuaMap.valuesIteratorvariable valuesView might not have been initialized
  • Expression.hashCodevariable cachedHash might not have been initialized
  • Bin.hashCodevariable cachedHash might not have been initialized
  • NettyTlsContext.createHandlervariable cipherSet might not have been initialized
  • Pool.capacityvariable cachedCapacity might not have been initialized

🤖 Generated with Claude Code

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>
@misrasaurabh1 misrasaurabh1 merged commit d455afb into fix/java/inner-class-helpers-filter Mar 3, 2026
21 of 29 checks passed
@misrasaurabh1 misrasaurabh1 deleted the fix/java/constructor-replacement branch March 3, 2026 00:15
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