Skip to content

Conversation

@mashraf-222
Copy link
Contributor

Problems fixed

The Java formatter (JavaFormatter class in codeflash/languages/java/formatter.py) exists and is fully functional, capable of formatting Java code using google-java-format. However, the project detection pipeline in detector.py returned [], "not supported for Java" when asked to detect a formatter for Java projects. This meant optimized Java code was never formatted before being submitted, potentially causing diff noise in PRs and inconsistent code style.

Root causes

The _detect_formatter function had a hard-coded early return for Java:

if language == "java":
    # Java formatter support not implemented yet
    return [], "not supported for Java"

This was a placeholder that was never replaced with actual detection logic, even though the JavaFormatter class was already implemented with full google-java-format support.

Solutions implemented

Added _detect_java_formatter(project_root) function that:

  1. Finds the Java executable via JAVA_HOME environment variable or shutil.which("java")
  2. Locates the google-java-format JAR in standard locations:
    • <project_root>/.codeflash/google-java-format-<version>-all-deps.jar
    • ~/.codeflash/google-java-format-<version>-all-deps.jar
    • <tempdir>/codeflash/google-java-format-<version>-all-deps.jar
  3. Returns formatter commands when both Java and JAR are available (e.g., java -jar <path> --replace $file)
  4. Falls back gracefully with descriptive messages when either is missing (no errors, just empty list)

The version constant is reused from JavaFormatter.GOOGLE_JAVA_FORMAT_VERSION to stay in sync.

Code changes

codeflash/setup/detector.py:

  • Added import os, shutil, tempfile at module level
  • Updated _detect_formatter to call _detect_java_formatter for Java
  • Added _detect_java_formatter function with java and JAR detection logic

tests/test_languages/test_java/test_formatter.py:

  • Added TestDetectJavaFormatter class with 7 tests:
    • Returns commands when both java and JAR available
    • Returns empty list when java not available
    • Returns empty list when JAR not found
    • Finds java via JAVA_HOME
    • Finds JAR in ~/.codeflash/ directory
    • Python formatter detection still works (non-regression)
    • JavaScript formatter detection still works (non-regression)

Testing

$ uv run pytest tests/test_languages/test_java/test_formatter.py -v
# 25 passed (18 existing + 7 new)

$ uv run pytest tests/test_languages/test_java/ -v --ignore=tests/test_languages/test_java/test_build_tools.py
# 390 passed (test_build_tools.py has pre-existing import error unrelated to this change)

$ uv run ruff check codeflash/setup/detector.py
# All checks passed!

$ uv run ruff format --check codeflash/setup/detector.py
# 1 file already formatted

Impact

Java projects will now have their optimized code formatted with google-java-format when the JAR is available. This produces cleaner optimization PRs with consistent code style. When the JAR is not available, behavior is unchanged (no formatting, no errors).

mashraf-222 and others added 4 commits February 6, 2026 13:27
- 14 new tests in TestConcurrencyPatterns class
- synchronized blocks/methods preserved after transformation
- volatile field reads, AtomicInteger ops preserved
- ConcurrentHashMap, Thread.sleep, wait/notify patterns preserved
- ReentrantLock, CountDownLatch patterns preserved
- Real-world TokenBucket and CircularBuffer patterns validated
- AssertJ assertion on synchronized method call validated
- Total: 71 tests (57 existing + 14 new), all passing
…g coverage

- Add TestComparatorEdgeCases: float, NaN, Infinity, empty collections, large numbers, null vs empty, booleans
- Add TestComparatorErrorHandling: missing DBs, schema mismatch, None return values, error type comparison
- Add TestComparatorJavaEdgeCases: EPSILON tolerance, NaN handling, empty tables, Infinity handling
- 29 new tests (52 total), all passing

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
… routing

- Add TestSqlitePathSelection: file existence checks for Java comparison path
- Add TestPassFailFallbackBehavior: pass_fail_only ignores return values, detects failure changes
- Add TestDecisionPointDocumentation: canary tests for decision logic code pattern
- 12 tests covering SQLite path selection, pass_fail_only behavior, and code pattern stability

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Replace 'not supported for Java' stub with actual formatter detection
- Check for java executable via JAVA_HOME and shutil.which
- Check for google-java-format JAR in project/.codeflash, ~/.codeflash, and tempdir
- Return formatter commands when both java and JAR available
- Graceful fallback with descriptive message when not available
- Add 7 tests covering all detection paths and non-regression for Python/JS

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mashraf-222
Copy link
Contributor Author

Verification Results

Tests

✅ All 25 formatter tests passed

pytest tests/test_languages/test_java/test_formatter.py -v
========================= 25 passed in 0.95s =========================

Code Review

Main changes:

  • Added _detect_java_formatter function in codeflash/setup/detector.py (54 lines)
    • Detects Java executable from JAVA_HOME or PATH
    • Checks for google-java-format JAR in standard locations (.codeflash/, ~/.codeflash/, /tmp/codeflash/)
    • Returns graceful fallback messages when components missing
  • Added 7 new test cases in test_formatter.py covering detection scenarios

⚠️ Unrelated changes detected:

  • codeflash/verification/concolic_testing.py: Removed 3 lines of timeout documentation comments
  • codeflash/verification/test_runner.py: Removed timeout documentation comments, some changed back to "TODO: Make this dynamic"

These timeout-related changes are outside the scope of formatter wiring and should either be:

  1. Moved to a separate commit/PR focused on timeout documentation cleanup, or
  2. Reverted from this PR

E2E Verification

✅ Formatter detection logic works correctly:

  • Gracefully handles missing Java executable: ([], "no Java formatter found (java not available)")
  • Gracefully handles missing JAR: ([], "no Java formatter found (install google-java-format)")
  • Integration with detector.py works as expected

Recommendation

The core formatter wiring functionality is solid and well-tested. Please address the unrelated timeout documentation changes before merging.

Restore timeout documentation comments in concolic_testing.py and
test_runner.py that were unintentionally removed. These comments
document the timeout parameter behavior and environment variable
overrides.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mashraf-222
Copy link
Contributor Author

Update: The unrelated timeout documentation changes have been reverted in commit 5a85fef. The PR now only contains the Java formatter wiring changes.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mashraf-222 mashraf-222 merged commit a732a69 into omni-java Feb 10, 2026
17 of 28 checks passed
@mashraf-222 mashraf-222 deleted the fix/wire-java-formatter branch February 10, 2026 17:33
mashraf-222 added a commit that referenced this pull request Feb 10, 2026
Merge latest changes from base branch including:
- Java compilation error detection (PR #1394)
- Java formatter detection via google-java-format (PR #1400)
- Enhanced test coverage for comparator logic

Conflict resolution:
- tests/test_languages/test_java/test_comparison_decision.py: Used PR version
  that enforces strict correctness (no pass_fail_only fallback tests)
  to align with PR 1401's goal of removing pass_fail_only mode entirely.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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