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.
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:
- 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. - 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.
- 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.
- 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
andgen
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
is0.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.
- 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.
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.
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
.
<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 |
install the plugin locally
mvn clean install
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.
mvn clean package
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
- support protocol buffer generation for
.proto
files - bug fixes and improvements based on real-world usage
This project is licensed under the Apache License 2.0.