fix(java): replace modified constructors when LLM adds new final fields#1727
Merged
misrasaurabh1 merged 1 commit intofix/java/inner-class-helpers-filterfrom Mar 3, 2026
Conversation
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>
d455afb
into
fix/java/inner-class-helpers-filter
21 of 29 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the LLM optimises a method by introducing a new
finalfield (e.g. cachingArrays.hashCodeinExpression.hashCode, or cachingmap.values()inLuaMap.valuesIterator), it also updates the class constructors to initialise that field. Previously codeflash:This caused
variable X might not have been initializedcompilation errors.Changes
parser.pyJavaAnalyzer.find_constructors+_walk_tree_for_constructors+_extract_constructor_info: new methods to locateconstructor_declarationnodes via tree-sitterJavaMethodNode.formal_parameters_text: captures the raw parameter list so constructors can be matched by signaturereplacement.pyParsedOptimization.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 indentationreplace_function: call_replace_constructorsafter the main method replacementTest plan
TestConstructorReplacement::test_constructor_updated_when_new_final_field_added— reproducesExpression.hashCodeTestConstructorReplacement::test_constructor_updated_for_cached_collection_view— reproducesLuaMap.valuesIteratortest_replacement.pytests passFixes regressions from
codeflash_all_3.logLuaMap.valuesIterator—variable valuesView might not have been initializedExpression.hashCode—variable cachedHash might not have been initializedBin.hashCode—variable cachedHash might not have been initializedNettyTlsContext.createHandler—variable cipherSet might not have been initializedPool.capacity—variable cachedCapacity might not have been initialized🤖 Generated with Claude Code