fix: scope field extraction to target class to prevent cross-class injection#1953
fix: scope field extraction to target class to prevent cross-class injection#1953mashraf-222 wants to merge 1 commit intomainfrom
Conversation
…jection find_fields() was called without a class_name filter, causing fields from inner/anonymous classes to be injected into the outer target class. Now scoped to target_method.class_name using the existing filter parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @mashraf-222's task in 2m 2s —— View job PR Review Summary
Prek ChecksAll checks passed. No issues. Code ReviewThe fix is correct and well-targeted. What changed: One minor edge case to note: When Slight redundancy: Related unfiltered call at line 472: Duplicate DetectionNo duplicates detected. The fix is Java-specific and uses the existing Overall this is a clean, minimal fix that addresses the root cause. The test accurately reproduces the Guava |
Problem
When the LLM generates optimization code containing inner/anonymous classes with fields, those fields are incorrectly injected into the target method's enclosing class. This produces uncompilable code — e.g., type parameters like
Iterator<? extends F>injected whereFis not in scope.Observed in Guava PR #12 (
Iterables.mergeSorted) where inner class fields were injected into the outerIterablesclass.Root Cause
_parse_optimization_source()inreplacement.py(line 72) calledanalyzer.find_fields(new_source)without aclass_namefilter, extracting ALL fields from ALL classes in the generated code — including inner classes, static nested classes, and anonymous classes.Fix
Pass
class_name=target_method.class_nametofind_fields()so only fields belonging to the target method's class are extracted. Thefind_fields()API already supported this filter via its_walk_tree_for_fields()implementation — it just wasn't being used.The field extraction was also moved after the target method lookup (previously it ran before we knew which class the target was in).
Validation
badField→ it was injected into outer classbadFieldno longer injected, onlyOFFSET(same class as target) is addedTest Coverage
tests/test_languages/test_java/test_replacement.py—TestFieldInjectionClassFiltering::test_inner_class_fields_not_injected_into_outerCloses CF-1087