Reflectator is a command-line tool that transforms Java source code into an equivalent reflective version. It uses Spoon to parse Java source files, traverse their abstract syntax tree (AST), and produce transformed Java source code ready to be compiled.
The tool is organized as a chain of Spoon processors. Each processor receives an AST, transforms one specific Java syntactic construct into its reflective equivalent, and passes the resulting AST to the next processor. Processors can be enabled selectively, so the same input program can be transformed with different levels of reflection.
Reflectator always runs a small set of base processors that add support code and normalize the transformed classes. Optional processors can then transform field reads, field writes, array operations, casts, method invocations, constructor invocations, and new array expressions.
The generated output also includes an introspector.IntrospectorHelper helper class used by some transformations, especially numeric unary operations.
Reflectator requires:
- Java 21 or higher.
- Maven 3.x.
The project builds a shaded executable JAR that includes Spoon.
To build Reflectator, run:
mvn packageThe executable JAR is generated at:
target/reflectator-1.0.0.jarRun Reflectator with a source folder and an output folder:
java -jar target/reflectator-1.0.0.jar -source path/to/source -out path/to/outputReflectator writes the transformed source code into an RF* subfolder inside the output folder. For example, if the output folder is out, the default generated folder is:
out/RFOptional transformations change the generated folder name. For example, using -fr -mi writes into RF-fr-mi.
-help,--help,-?Display the command-line help.-source <source_folder>,--source <source_folder>,-s <source_folder>Source folder to process. Also accepts-source=<source_folder>.-out <output_folder>,--out <output_folder>,-o <output_folder>Base output folder. Reflectator writes into anRF*subfolder.-exclude <class_name>,--exclude <class_name>,-x <class_name>Class or package to exclude from reflection. Can be repeated.-cTransform casts into reflectiveClass.cast(...)calls.-frTransform field reads into reflective field access.-fwTransform field writes into reflective field updates.-arTransform array reads into reflective array access.-awTransform array writes into reflective array updates.-naTransform new array expressions into reflective array creation.-ciTransform constructor calls into reflective constructor invocations.-miTransform method calls into reflective method invocations.-fullEnable all optional transformations.<class_name> [<class_name> ...]Restrict transformation to the listed classes. Fully qualified names and.javafile names are accepted.
Transform a project with the base processors only:
java -jar target/reflectator-1.0.0.jar -s examples/MyProject/src -o generatedTransform field reads and method invocations:
java -jar target/reflectator-1.0.0.jar -s examples/MyProject/src -o generated -fr -miTransform all supported constructs:
java -jar target/reflectator-1.0.0.jar -s examples/MyProject/src -o generated -fullTransform only selected classes:
java -jar target/reflectator-1.0.0.jar -s examples/MyProject/src -o generated -full com.example.Service com.example.Model.javaExclude a package or class from the transformation:
java -jar target/reflectator-1.0.0.jar -s examples/MyProject/src -o generated -full -x com.example.generated -x LegacyClassReflectator supports the following optional transformations:
| Option | Processor | Transformation |
|---|---|---|
-c |
Cast |
Replaces non-primitive casts with reflective Class.cast(...) calls. |
-fr |
FieldRead |
Replaces instance, static, inherited, and array-length field reads with reflective access. |
-fw |
FieldWrite |
Replaces instance, static, and inherited field writes with reflective updates. |
-ar |
ArrayRead |
Replaces array reads with java.lang.reflect.Array.get(...). |
-aw |
ArrayWrite |
Replaces array writes with java.lang.reflect.Array.set(...). |
-na |
NewArrayInvocation |
Replaces new array expressions with reflective array creation. |
-ci |
ConstructorInvocation |
Replaces constructor calls with Class.forName(...).getConstructor(...).newInstance(...). |
-mi |
MethodInvocation |
Replaces instance, static, and super method calls with reflective invocation. |
The base processors are always enabled. They add helper methods, wrap generated reflective statements when needed, and process method, constructor, class, unary-expression, assignment, try/catch, and block constructs required by the transformation pipeline.
