Skip to content

fix: scope field extraction to target class to prevent cross-class injection#1953

Open
mashraf-222 wants to merge 3 commits intomainfrom
cf-1087-field-injection-class-filter
Open

fix: scope field extraction to target class to prevent cross-class injection#1953
mashraf-222 wants to merge 3 commits intomainfrom
cf-1087-field-injection-class-filter

Conversation

@mashraf-222
Copy link
Copy Markdown
Contributor

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 where F is not in scope.

Observed in Guava PR #12 (Iterables.mergeSorted) where inner class fields were injected into the outer Iterables class.

Root Cause

_parse_optimization_source() in replacement.py (line 72) called analyzer.find_fields(new_source) without a class_name filter, 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_name to find_fields() so only fields belonging to the target method's class are extracted. The find_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

  • Bug reproduced: test creates an inner class with badField → it was injected into outer class
  • Fix confirmed: badField no longer injected, only OFFSET (same class as target) is added
  • All 33 replacement tests pass (32 existing + 1 new)

Test Coverage

  • tests/test_languages/test_java/test_replacement.pyTestFieldInjectionClassFiltering::test_inner_class_fields_not_injected_into_outer

Closes CF-1087

…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
Copy link
Copy Markdown
Contributor

claude bot commented Apr 1, 2026

Claude encountered an error —— View job


I'll analyze this and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant