Skip to content

Commit 8aff48f

Browse files
Merge pull request #1953 from codeflash-ai/cf-1087-field-injection-class-filter
fix: scope field extraction to target class to prevent cross-class injection
2 parents 1ba6af0 + 2b90ee8 commit 8aff48f

2 files changed

Lines changed: 111 additions & 39 deletions

File tree

codeflash/languages/java/replacement.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def _parse_optimization_source(new_source: str, target_method_name: str, analyze
6969
if classes:
7070
# It's a class - extract components
7171
methods = analyzer.find_methods(new_source)
72-
fields = analyzer.find_fields(new_source)
7372

7473
# Find the target method and its index among all methods
7574
target_method = None
@@ -128,7 +127,11 @@ def _parse_optimization_source(new_source: str, target_method_name: str, analyze
128127
ctor_end = c.end_line
129128
modified_constructors.append("".join(ctor_lines[ctor_start:ctor_end]))
130129

131-
# Extract fields
130+
# Extract fields scoped to the target method's class only.
131+
# Without class filtering, fields from inner/anonymous classes would be
132+
# incorrectly injected into the outer target class.
133+
target_class_name = target_method.class_name if target_method else None
134+
fields = analyzer.find_fields(new_source, class_name=target_class_name)
132135
for f in fields:
133136
if f.source_text:
134137
new_fields.append(f.source_text)

0 commit comments

Comments
 (0)