fix: guard Java replacement against wrong-method-name candidates and anonymous-class method hoisting#1704
fix: guard Java replacement against wrong-method-name candidates and anonymous-class method hoisting#1704misrasaurabh1 wants to merge 2 commits intoomni-javafrom
Conversation
…anonymous-class method hoisting
Two bugs in _parse_optimization_source (replacement.py) caused Maven compilation
failures when codeflash optimised aerospike-client-java:
Bug 1 – standalone method with wrong name replaces target
When the LLM generated a standalone method whose name did not match the
optimisation target (e.g. generated `unpackMap` for target `unpackObjectMap`,
or generated `sizeTxn` for target `estimateKeySize`), the function fell back to
using the entire generated snippet as `target_method_source`. This silently
replaced the target with the wrong method, producing:
• a duplicate definition of the wrong method
• removal of the target method (breaking all callers)
Fix: after parsing standalone (class-free) code, verify that at least one
discovered method matches the target name. If no match is found, set
`target_method_source` to the empty string and log a warning. A corresponding
guard in `replace_function` returns the original source unchanged when
`target_method_source` is empty.
The same guard is applied to the full-class path: if the generated class does
not contain the target method, the candidate is also rejected.
Bug 2 – anonymous inner-class methods hoisted as top-level helpers
When an optimised method returned an anonymous class (e.g. `keySetIterator`
returning `new Iterator<LuaValue>() { … }`), tree-sitter's recursive walk
found the anonymous class's `hasNext`, `next`, and `remove` method_declaration
nodes and classified them as helpers to be inserted at the outer-class level.
The inserted methods carried `@Override` annotations that matched nothing in the
outer class and referenced local variables (`it`) that were only in scope inside
the optimised method body, producing compilation errors.
Fix: when extracting helpers from the optimised class, skip any method whose
line range is entirely contained within the target method's line range. Such
methods belong to anonymous/nested classes inside the method body and must not
be hoisted out as standalone class members.
Tests added for both bugs in TestWrongMethodNameGeneration and
TestAnonymousInnerClassMethods.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Duplicate Code Detection ResultsFound 1 duplicate requiring attention: 1. JAR Finder Functions (HIGH Confidence)What's duplicated: Three functions search for the Locations:
Shared logic:
Suggestion: Extract to a shared utility function in Not Duplicates (Intentional Parallel Implementation)The following similar patterns are NOT duplicates — they're intentionally parallel implementations for different languages:
|
PR Review SummaryPrek Checks
Mypy
Code ReviewNo critical issues found. The changes are well-structured:
Test Coverage
Coverage analysis for changed code:
Optimization PRsChecked 6 codeflash-ai[bot] PRs targeting Last updated: 2026-03-02T03:47Z |
Summary
This PR fixes two bugs in
_parse_optimization_source(Java replacement logic) that caused Maven compilation failures when running codeflash on aerospike-client-java.Bug 1 — Standalone method with wrong name replaces target
Root cause: When the LLM generates a standalone method whose name does not match the optimisation target (e.g. generates
unpackMapfor targetunpackObjectMap, orsizeTxnfor targetestimateKeySize), the code fell back to using the entire generated snippet astarget_method_source. This silently replaced the target with the wrong method body, producing:Observed errors in aerospike-client-java log:
Fix: After parsing standalone (class-free) code, verify at least one discovered method matches
target_method_name. If no match is found, settarget_method_source = ""and log a warning. A guard inreplace_functionreturns the original source unchanged whentarget_method_sourceis empty. The same guard applies to the full-class path (class found, but target method absent).Bug 2 — Anonymous inner-class methods hoisted as top-level helpers
Root cause: When an optimised method returned an anonymous class (e.g.
keySetIteratorreturningnew Iterator<LuaValue>() { ... }), tree-sitter's recursive walk found the anonymous class'shasNext,next, andremovemethod_declarationnodes and classified them as helpers to insert at the outer-class level. The hoisted methods:@Overrideannotations matching nothing in the outer classit) only in scope inside the optimised method bodyObserved errors in aerospike-client-java log:
Fix: When extracting helpers, skip any method whose line range is entirely contained within the target method's line range — such methods belong to anonymous/nested classes inside the method body.
Test plan
TestWrongMethodNameGeneration::test_standalone_wrong_method_name_leaves_source_unchanged— verifies the Unpacker bug is fixedTestWrongMethodNameGeneration::test_class_wrapper_with_wrong_target_method_leaves_source_unchanged— verifies the Command bug is fixedTestAnonymousInnerClassMethods::test_anonymous_iterator_methods_not_hoisted_to_class— verifies the LuaMap bug is fixed🤖 Generated with Claude Code