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 7 commits 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 encountered an error —— View job I'll analyze this and get back to you. |
Add -> None return annotations and Path / JavaSupport parameter annotations to every test method + fixture so the prek mypy hook passes when the file is in the CI diff.
Rich renders the banner panel with box-drawing characters (╭, ╮, │, etc.) that cp1252 cannot decode. On Windows, subprocess.run(..., text=True) uses cp1252 by default, so decoding the child stdout raises UnicodeDecodeError and subprocess sets result.stdout to None — breaking the assertion with a misleading "argument of type 'NoneType' is not iterable". Pass encoding="utf-8" explicitly so the test passes on every platform.
ReviewBug premise verified — real. Fix is minimal and correct. Correctness edge cases: top-level siblings, static-nested, and inner classes are handled correctly by CI blockers addressed in the last two commits:
e2e-java |
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