Skip to content

Comments

fix: correct field ordering, helper placement, indentation, and blank lines in Java code replacer#1555

Merged
misrasaurabh1 merged 1 commit intoomni-javafrom
fix/java-code-replacer-injection-bugs
Feb 20, 2026
Merged

fix: correct field ordering, helper placement, indentation, and blank lines in Java code replacer#1555
misrasaurabh1 merged 1 commit intoomni-javafrom
fix/java-code-replacer-injection-bugs

Conversation

@misrasaurabh1
Copy link
Contributor

Summary

Fixes four bugs in _insert_class_members / replace_function in codeflash/languages/java/replacement.py that caused incorrect output when an optimisation adds new static fields or helper methods to a Java class.

  • Bug 1 – Extra indentation on injected methods: Extracted class members retained their original class-level whitespace prefix. When _apply_indentation re-indented them it treated that existing whitespace as relative indentation and stacked the base indent on top, producing 12-space bodies instead of 8. Fixed by running textwrap.dedent() on each member source before re-indenting.

  • Bug 2 – New fields inserted before existing ones: Fields were always injected at body_node.start_byte + 1 (right after {), pushing existing fields down. Fixed by finding the last existing field declaration via analyzer.find_fields(source, class_name=class_name) and inserting after it.

  • Bug 3 – Helper methods always appended at end of class: All helpers landed after every existing method regardless of their intended position. Fixed by tracking each helper's index relative to the target method in the optimised code (helpers_before_target / helpers_after_target in ParsedOptimization). Before-target helpers are now inserted immediately before the target method; after-target helpers are still appended at the end.

  • Bug 4 – No blank lines between consecutively injected helpers: Multiple helpers were concatenated without separators. Fixed by appending "\n" after each injected helper.

Test plan

  • Updated the 6 affected full-string equality assertions in tests/test_languages/test_java/test_replacement.py to reflect the now-correct output
  • All 29 tests in that file pass
  • Full tests/test_languages/ suite: 1241 passed, 0 failed

🤖 Generated with Claude Code

… lines in Java code replacer

Four bugs in _insert_class_members / replace_function:
1. Extra indentation on injected methods (textwrap.dedent now normalises source before re-indenting)
2. New fields were prepended before existing ones (now inserted after the last existing field)
3. Helper methods were always appended at end of class (now placed before/after target based on their position in the optimised code)
4. No blank lines between consecutively injected helpers (each helper is now followed by a blank line)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 merged commit a851ec6 into omni-java Feb 20, 2026
23 of 30 checks passed
@misrasaurabh1 misrasaurabh1 deleted the fix/java-code-replacer-injection-bugs branch February 20, 2026 00:46
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