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.
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>
Inner-class methods are intentionally skipped by Java discovery
(PR #1726) since instrumentation is name-only and not class-aware.
Update test to expect False from replacement.
- Fix MockTestConfig missing tests_project_rootdir field
- Fix Python line profiler existence check on wrong path (no .lprof suffix)
- Add --release 11 to javac in Java line profiler tests for JDK compat
- Resolve merge conflicts with omni-java (replacement, tests)
- Add replace_function_definitions method to JavaSupport
- Guard against wrong method names in optimized code (standalone + class)
- Add tests for anonymous inner class method hoisting
Replace substring-based assertions with a single exact string
comparison in test_anonymous_iterator_methods_not_hoisted_to_class,
matching the convention used elsewhere in the test file.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fix min/max_outer_loops → pytest_min/max_loops in Java test_run_and_parse
- Update test_replacement.py for new replace_function_definitions_for_language API
- Update JavaSupport.discover_functions signature to match protocol
- Migrate _get_java_sources_root/_fix_java_test_paths to JavaFunctionOptimizer
- Fix test_java_tests_project_rootdir to use set_current_language
Two bugs in _parse_optimization_source (replacement.py) caused Maven compilation
failures when codeflash optimised aerospike-client-java:
Bug 1 – standalone method with wrong name replaces target
When the LLM generated a standalone method whose name did not match the
optimisation target (e.g. generated `unpackMap` for target `unpackObjectMap`,
or generated `sizeTxn` for target `estimateKeySize`), the function fell back to
using the entire generated snippet as `target_method_source`. This silently
replaced the target with the wrong method, producing:
• a duplicate definition of the wrong method
• removal of the target method (breaking all callers)
Fix: after parsing standalone (class-free) code, verify that at least one
discovered method matches the target name. If no match is found, set
`target_method_source` to the empty string and log a warning. A corresponding
guard in `replace_function` returns the original source unchanged when
`target_method_source` is empty.
The same guard is applied to the full-class path: if the generated class does
not contain the target method, the candidate is also rejected.
Bug 2 – anonymous inner-class methods hoisted as top-level helpers
When an optimised method returned an anonymous class (e.g. `keySetIterator`
returning `new Iterator<LuaValue>() { … }`), tree-sitter's recursive walk
found the anonymous class's `hasNext`, `next`, and `remove` method_declaration
nodes and classified them as helpers to be inserted at the outer-class level.
The inserted methods carried `@Override` annotations that matched nothing in the
outer class and referenced local variables (`it`) that were only in scope inside
the optimised method body, producing compilation errors.
Fix: when extracting helpers from the optimised class, skip any method whose
line range is entirely contained within the target method's line range. Such
methods belong to anonymous/nested classes inside the method body and must not
be hoisted out as standalone class members.
Tests added for both bugs in TestWrongMethodNameGeneration and
TestAnonymousInnerClassMethods.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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>
- Resolve merge conflict in code_replacer.py with Java-specific handling
- Update all Java modules to use FunctionToOptimize instead of FunctionInfo
- Add Language.JAVA to language_enum.py
- Update attribute names: name→function_name, start_line→starting_line, etc.
- Update all Java tests to use correct attribute names
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add index-based tracking for overloaded methods to ensure correct
method is replaced when multiple methods share the same name
- Match target method by line number (with 5-line tolerance) when
multiple overloads exist
- Track overload index to re-find correct method after class member
insertion which shifts line numbers
- Improve error logging in test compilation to show both stdout/stderr
- Use -e flag instead of -q for Maven compilation to show errors
- Add comprehensive test for overloaded method replacement
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add support for Java optimizations that include new class-level members:
- Static fields (e.g., lookup tables like BYTE_TO_HEX)
- Helper methods (e.g., createByteToHex())
- Precomputed arrays
Changes:
- Add _add_java_class_members() in code_replacer.py to detect and insert
new class members from optimized code into the original source
- Update _add_global_declarations_for_language() to handle Java
- Add ParsedOptimization dataclass and supporting functions in replacement.py
- Exclude target functions from being added as helpers (they're replaced)
Tests:
- Add TestOptimizationWithStaticFields (3 tests)
- Add TestOptimizationWithHelperMethods (2 tests)
- Add TestOptimizationWithFieldsAndHelpers (2 tests including real-world
bytesToHexString optimization pattern)
All 28 Java replacement tests and 32 instrumentation tests pass.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>