Skip to content

Commit 1858044

Browse files
misrasaurabh1claude
andcommitted
fix: improve Java class name extraction regex to avoid false matches
- Use ^(?:public\s+)?class pattern to match class declaration at start of line - Prevents matching words like "command" or text in comments that contain "class" - Fixes issue where test files were named incorrectly (e.g., "and__perfinstrumented.java") Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a594ff2 commit 1858044

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

codeflash/languages/java/instrumentation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ def instrument_generated_java_test(
652652
653653
"""
654654
# Extract class name from the test code
655-
class_match = re.search(r'\bclass\s+(\w+)', test_code)
655+
# Use pattern that starts at beginning of line to avoid matching words in comments
656+
class_match = re.search(r'^(?:public\s+)?class\s+(\w+)', test_code, re.MULTILINE)
656657
if not class_match:
657658
logger.warning("Could not find class name in generated test")
658659
return test_code

codeflash/optimization/function_optimizer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,12 @@ def _fix_java_test_paths(
676676
package_name = package_match.group(1) if package_match else ""
677677

678678
# Extract class name from behavior source
679-
class_match = re.search(r'\bclass\s+(\w+)', behavior_source)
679+
# Use more specific pattern to avoid matching words like "command" or text in comments
680+
class_match = re.search(r'^(?:public\s+)?class\s+(\w+)', behavior_source, re.MULTILINE)
680681
behavior_class = class_match.group(1) if class_match else "GeneratedTest"
681682

682683
# Extract class name from perf source
683-
perf_class_match = re.search(r'\bclass\s+(\w+)', perf_source)
684+
perf_class_match = re.search(r'^(?:public\s+)?class\s+(\w+)', perf_source, re.MULTILINE)
684685
perf_class = perf_class_match.group(1) if perf_class_match else "GeneratedPerfTest"
685686

686687
# Build paths with package structure

0 commit comments

Comments
 (0)