1 ## Code Reflection Examples 2 3 List of examples to learn and play with the Code Reflection API. 4 5 - GitHub repo: [https://github.com/openjdk/babylon](https://github.com/openjdk/babylon) 6 7 ### Learning Code Reflection? 8 9 Here's an ordered list of code example to start learning code reflection and some of its features. 10 Each example is self-contained, and it can be used within the IDE to explore and understand how code reflection 11 transforms and executes code. 12 13 1. [`HelloCodeReflection`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/HelloCodeReflection.java): Just start using code reflection and lowering code models. 14 2. [`MathOptimizer`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java): First code transformations to optimize a math function. 15 3. [`InlineExample`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/InliningExample.java): Simple example to illustrate the inlining. 16 4. [`MathOptimizerWithInlining`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizerWithInlining.java): Follow up of the [`MathOptimizer`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java) to inline optimize calls into the code model. 17 6. [`DialectWithInvoke`][https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DialectWithInvoke.java]: 18 Example of creating a dialect that replaces `Invoke` `Op` with a specific signature with a new `Op`. The dialect is handled as an intrinsic replacement. 19 6. [`DialectFMAOp`][https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DialectFMAOp.java]: Example of how to extend the code reflection `Op` to create a new dialect. It analysis the code for substitution of Add(Mult) to create a new `FMA` Op. 20 7. [`DynamicFunctionBuild`][https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DynamicFunctionBuild.java]: Example of how to create a new function dynamically to compute the inverse of a square root. The code model is built dynamically for a new method and it is evaluated in the `Interpreter`. 21 8. [`CodeReflectionProcessor`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/CodeReflectionProcessor.java): A simple code model-based annotation processor 22 23 ### Resources 24 25 1. [Article] [Code Models](https://openjdk.org/projects/babylon/articles/code-models) 26 2. [Article] [Emulating C# LINQ in Java using Code Reflection](https://openjdk.org/projects/babylon/articles/linq) 27 3. [Video] [Project Babylon - Code Reflection @JVMLS 2024](https://www.youtube.com/watch?v=6c0DB2kwF_Q) 28 4. [Video] [Java and GPUs using Code Reflection @JVMLS 2023](https://www.youtube.com/watch?v=lbKBu3lTftc) 29 30 ### How to build with project? 31 32 #### 1. Build Babylon JDK 33 34 We need to use the JDK build that enables the code reflection API (Babylon). 35 36 ```bash 37 git clone https://github.com/openjdk/babylon 38 cd babylon 39 bash configure --with-boot-jdk=${JAVA_HOME} 40 ``` 41 42 Then, we use the built JDK as `JAVA_HOME` 43 44 ```bash 45 export JAVA_HOME=/$HOME/repos/babylon/build/macosx-aarch64-server-release/jdk/ 46 export PATH=$JAVA_HOME/bin:$PATH 47 ``` 48 49 #### 2. Build examples 50 51 ```bash 52 mvn clean package 53 ``` 54 55 #### 3. Run the examples 56 57 58 ##### Run HelloCodeReflection 59 60 ```bash 61 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.HelloCodeReflection 62 ``` 63 64 ##### Run MathOptimizer 65 66 ```bash 67 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizer 68 ``` 69 70 ##### Run InlineExample 71 72 ```bash 73 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.InlineExample 74 ``` 75 76 ##### Run MathOptimizerWithInlining 77 78 ```bash 79 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizerWithInlining 80 ``` 81 82 ##### Run DialectWithInvoke 83 84 ```bash 85 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DialectWithInvoke 86 ``` 87 88 ##### Run DialectFMAOp 89 90 ```bash 91 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DialectFMAOp 92 ``` 93 94 ##### Run DynamicFunctionBuild 95 96 ```bash 97 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DynamicFunctionBuild 98 ``` 99 100 ##### Compile with CodeReflectionProcessor 101 102 ```bash 103 javac --add-modules jdk.incubator.code --processor-path target/crsamples-1.0-SNAPSHOT.jar -processor oracle.code.samples.CodeReflectionProcessor <.java files to compile> 104 ```