Skip to content

decster/thrift-java-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build codecov

Java Thrift Compiler (Experimental)

This project is an experimental Java-based compiler for Apache Thrift IDL files, similar to the official Apache Thrift compiler.

A significant portion of this codebase(90%) was generated by AI (specifically, Gemini/Claude Sonnet 3.7 via GitHub Copilot Chat, and potentially Google Jules), serving as an experiment to evaluate AI's capabilities in generating moderately complex, practical software.

Background & Motivation

Apache Thrift is a powerful and widely-used software framework for scalable cross-language services development.

This project attempts to replicate the Thrift compiler's core functionality (parsing and Java code generation) within Java itself. The motivation is twofold:

  1. To create a pure java tool to convert thrift file to java code, previously, this requires a c++ binary thrift to be build/installed, which may become very difficult in some environments (e.g., Windows, or when using a different language runtime). This project aims to provide a Java-based solution that can be easily integrated into Java projects without external dependencies.
  2. Primarily, to serve as a case study on the effectiveness of AI code generation for a non-trivial, real-world inspired task. The development process heavily relied on AI to translate cpp code in ref-src into src/java, then the code is reviewed and guided.

Features

  • Thrift IDL Parsing: Uses ANTLR v4 (Thrift.g4) to parse .thrift files.
  • Abstract Syntax Tree (AST): Builds a Java-based AST (see java/com/github/decster/ast/) mirroring the structure of Thrift definitions (e.g., TProgram, TStruct, TService, TEnum, TField).
  • Java Code Generation:
    • Generates Java classes for enums, structs, unions, exceptions, and constants.
    • Generates Java interfaces and client/server stubs for services.
    • Supports various Java generator options (e.g., beans, fullcamel, option_type).
  • Command-Line Interface: Provides a ThriftCompiler main class to invoke the parsing and generation process.
  • Include Handling: Supports recursive parsing of included .thrift files.

Current Status & Limitations

  • Experimental: This project is an ongoing experiment. While it can parse many common Thrift constructs and generate Java code, it may not support all edge cases or advanced features of the official Apache Thrift compiler.
  • AI-Assisted Development: The code, especially in the ast and gen packages, has been significantly bootstrapped by AI. This means it may contain idiomatic (or unidiomatic) patterns influenced by the AI's training data. Thorough review and testing are ongoing.
  • Focus on Java Generation: Currently, the primary generation target is Java.
  • Compatibility: Aims to be compatible with Apache Thrift concepts, but generated code might differ from the official generator in style or specific helper methods. The internal version string used by the JavaGenerator is 0.20.0
  • Error Handling: Error reporting and recovery during parsing/generation might be less robust than the official compiler.
  • Original C++ Code: The directories ref-src/ (C++) contain code from the original Apache Thrift compiler. These are not part of the Java build of this project but serve as the reference and inspiration for the Java AST and compiler logic.

How It Works (Simplified)

  • Parse: The input .thrift file (and any includes) are parsed using an ANTLR-generated parser (Thrift.g4).
  • AST Construction: The parser events are used to build an internal Abstract Syntax Tree (AST) representing the Thrift definitions in Java objects.
  • Code Generation: The JavaGenerator traverses the AST and uses templates/logic to produce the corresponding Java source files.

Thrift Java Maven Plugin

A Maven plugin for generating Java code from Thrift IDL files. This plugin leverages the functionality of the Thrift Java Compiler to parse Thrift files and generate corresponding Java classes.

Example

A minimal setup in your pom.xml would look like:

<dependencies>
    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.20.0</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.36</version>
    </dependency>
</dependencies>

<plugin>
    <groupId>io.github.decster</groupId>
    <artifactId>thrift-java-maven-plugin</artifactId>
    <version>0.1.1</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

This will search for Thrift files in src/main/thrift and generate Java code in target/generated-sources/thrift.

Configuration

<build>
    <plugins>
        <plugin>
            <groupId>io.github.decster</groupId>
            <artifactId>thrift-java-maven-plugin</artifactId>
            <version>0.1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- Optional configuration parameters -->
                <sourceDirectory>${project.basedir}/src/main/thrift</sourceDirectory>
                <outputDirectory>${project.build.directory}/generated-sources/thrift</outputDirectory>
                <includeDirectories>
                    <includeDirectory>${project.basedir}/src/main/thrift-includes</includeDirectory>
                </includeDirectories>
                <generatorOptions>beans=true,private_members=true</generatorOptions>
                <fileExtension>.thrift</fileExtension>
            </configuration>
        </plugin>
    </plugins>
</build>
Parameter Description Default Value
sourceDirectory Directory containing Thrift IDL files ${project.basedir}/src/main/thrift
outputDirectory Directory where generated Java files will be placed ${project.build.directory}/generated-sources/thrift
includeDirectories List of directories to search for included Thrift files Empty list
generatorOptions String containing generator options in format "key1=value1,key2=value2" Empty string
fileExtension File extension for Thrift files .thrift
skip Flag to skip the execution of the plugin false

Building

install the plugin locally

mvn clean install

Standalone Usage

A standalone JAR is provided for those who want to use the Thrift Java Compiler without integrating it into a Maven project. This allows you to run the compiler directly from the command line.

Building

mvn clean package

Running

Once built, you can run the compiler from the command line:

java -jar target/thrift-java-maven-plugin-0.1.1-standalone.jar src/test/resources/include_tests/BackendService.thrift   -o genoutput

TODO

  • support protocol buffer generation for .proto files
  • bug fixes and improvements based on real-world usage

License

This project is licensed under the Apache License 2.0.

About

Pure java based thrift compiler, compile thrift to java

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •