TL;DR
This paper introduces TRAM, a novel framework for automatically translating entire code repositories from Java to Python. TRAM significantly improves translation quality and reliability by combining Retrieval-Augmented Generation (RAG) for context-aware type resolution with a robust mock-based in-isolation validation technique, overcoming limitations of previous approaches.
What Problem Does This Paper Address?
Migrating large software projects between programming languages (repository-level code translation) is crucial for modernizing systems and reducing technical debt. However, ensuring the translated code is functionally equivalent to the original is a major challenge. Existing methods often struggle with:
- Scalability: Formal methods are too slow for entire repositories.
- Test Coupling: Traditional test-based validation can fail due to dependencies between methods, making it hard to pinpoint translation bugs.
- In-Isolation Validation Limitations: Previous attempts using tools like GraalVM faced issues like incompleteness, manual effort for setup, false positives (reporting success for incorrect translations), and false negatives (reporting failure for correct translations).
- Type Resolution: Accurately mapping types between languages is critical, as incorrect mappings can lead to runtime errors. Existing methods often rely on manual effort or less precise automated approaches.
TRAM aims to address these challenges by providing a more automated, reliable, and scalable solution for validating translated code.
Methods
TRAM's approach is built around two core innovations:
- RAG-based Context-Aware Type Resolution: Before translation, TRAM analyzes API documentation and the surrounding code context for each type. It then uses a Retrieval-Augmented Generation (RAG) approach to prompt a Large Language Model (LLM) to infer the most semantically appropriate type mapping between Java and Python. This goes beyond simple, static mappings by considering how types are actually used in the codebase.
- Mock-based In-Isolation Validation: This is TRAM's key validation technique. Instead of running translated code within a full execution environment or relying on translated tests (which can be brittle), TRAM:
- Captures detailed input-output behavior and side effects of original Java method executions using AspectJ.
- Generates Python unit tests that isolate a single translated "focal" method.
- Uses mocks to simulate the behavior of all other methods the focal method depends on.
- Deserializes Java objects into Python equivalents for comparison.
- This allows for precise validation of individual method translations without the complexities of cross-language execution or brittle test translation.
TRAM integrates these components into a pipeline that first transforms and decomposes the Java project, then resolves types, and finally translates and validates fragments in isolation before recomposing the program.
Datasets
The paper evaluates TRAM on ten real-world Java projects from the AlphaTrans benchmark. These projects are described as diverse and representative of challenges in repository-level code translation.
Key Insights and Ideas
- Context is King for Type Resolution: Simply mapping types statically is insufficient. TRAM's RAG approach, leveraging code context and API docs, significantly improves the accuracy and semantic correctness of type mappings, even leading to different Python types for the same Java type depending on its usage (e.g.,
Listmapping tolistvs.tuple). - Mocking is More Reliable Than GraalVM for Validation: TRAM's mock-based approach significantly reduces false positives and false negatives compared to GraalVM-based validation, which is prone to issues arising from glue code and interop limitations.
- In-Isolation Validation is Crucial: Validating code fragments in isolation, rather than relying on end-to-end tests, is key to overcoming test coupling and achieving higher validation success rates.
- LLM Agnosticism: TRAM's core components are designed to be largely independent of the specific LLM used, suggesting its effectiveness can generalize to various models, including smaller ones.
Key Results
- Higher Functional Equivalence: TRAM achieves a 43.10% functional equivalence rate for method fragments, a substantial improvement over AlphaTrans's 25.14%.
- More Validatable Fragments: TRAM validates 69.98% of application method fragments, compared to AlphaTrans's 56.57%, partly due to its ability to handle complex types and its integration with EvoSuite for test augmentation.
- Reduced Errors: Mock-based validation results in significantly fewer false positives (0.9% vs. 2.4% for GraalVM) and false negatives (0.9% vs. 7.6% for GraalVM).
- No Manual Intervention: TRAM eliminates the need for manual type resolution or glue code, which was a significant bottleneck in previous approaches.
- Generalizability: TRAM shows comparable performance with different LLMs, including the smaller Devstral-24B, indicating robustness.
Limitations and Future Work
- External Validity: While TRAM is designed to be language-agnostic, the current implementation targets Java-to-Python. Extending to other languages might require custom serialization logic (e.g., for C).
- Internal Validity: The reliability of mock-based validation could be affected by issues like overly strict equivalence checking or missing side effects, though this is mitigated by manual analysis and EvoSuite's test generation. Flaky tests from EvoSuite could also be a concern.
- Future Work: The authors suggest exploring more fine-grained semantic analysis (control-flow, data-flow) to further enhance type inference and translation coherence. They also aim to make the TRAM pipeline more PL-agnostic and enable adaptation to more language pairs with minimal human effort.
Summary updated