Commit graph

16 commits

Author SHA1 Message Date
Mohamed Ashraf
efbd34159c test: annotate test_replacement.py for mypy prek hook
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.
2026-04-28 15:22:42 +00:00
Mohamed Ashraf
3bd0255040 fix: scope field extraction to target class to prevent cross-class injection
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>
2026-04-01 16:37:23 +00:00
Kevin Turcios
b43b37b6dd fix: update nested class replacement test to match PR #1726 design
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.
2026-03-04 00:29:46 -05:00
Kevin Turcios
881b4e9b35 fix: resolve merge conflicts and fix test failures
- 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
2026-03-02 19:48:06 -05:00
Kevin Turcios
dc25d12e3c one more 2026-03-02 19:38:11 -05:00
misrasaurabh1
cd02dec79f test: use full string equality in anonymous iterator test
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>
2026-03-02 23:38:42 +00:00
Kevin Turcios
f7fd593de3 fix: resolve remaining test failures after main sync
- 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
2026-03-02 15:47:23 -05:00
Kevin Turcios
f37b37209c fix: update Java test_replacement import for moved code_replacer module 2026-03-02 15:32:57 -05:00
misrasaurabh1
2371540386 fix: guard Java replacement against wrong-method-name candidates and anonymous-class method hoisting
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>
2026-03-02 03:35:42 +00:00
Kevin Turcios
c74782757b Merge commit '6346c740' into sync-main-batch-4
# Conflicts:
#	.github/workflows/windows-unit-tests.yml
#	codeflash/code_utils/config_consts.py
#	codeflash/code_utils/instrument_existing_tests.py
#	codeflash/languages/python/context/unused_definition_remover.py
#	codeflash/languages/python/static_analysis/code_replacer.py
#	codeflash/optimization/function_optimizer.py
#	codeflash/optimization/optimizer.py
#	pyproject.toml
2026-02-19 21:26:23 -05:00
misrasaurabh1
72afada84c fix: correct field ordering, helper placement, indentation, and blank 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>
2026-02-19 16:35:50 -08:00
misrasaurabh1
520a1ff08e fix: resolve merge conflict and standardize Java to use FunctionToOptimize
- 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>
2026-02-02 19:08:09 -08:00
Saurabh Misra
c9503e2916 fix: handle overloaded Java methods correctly in code replacement
- 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>
2026-02-02 04:03:37 +00:00
Saurabh Misra
79fbd2bdc9 feat: support Java optimizations with static fields and helper methods
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>
2026-02-02 01:31:41 +00:00
misrasaurabh1
090e77571f thorough tests for code replacement 2026-01-30 18:43:09 -08:00
misrasaurabh1
29f266ee63 wip java support 2026-01-30 00:37:24 -08:00