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 18 ### Resources 19 20 1. [Article] [Code Models](https://openjdk.org/projects/babylon/articles/code-models) 21 2. [Article] [Emulating C# LINQ in Java using Code Reflection 22 ](https://openjdk.org/projects/babylon/articles/linq) 23 3. [Video] [Project Babylon - Code Reflection @JVMLS 2024](https://www.youtube.com/watch?v=6c0DB2kwF_Q) 24 4. [Video] [Java and GPUs using Code Reflection @JVMLS 2023](https://www.youtube.com/watch?v=lbKBu3lTftc) 25 26 ### How to build with project? 27 28 #### 1. Build Babylon JDK 29 30 We need to use the JDK build that enables the code reflection API (Babylon). 31 32 ```bash 33 git clone https://github.com/openjdk/babylon 34 cd babylon 35 bash configure --with-boot-jdk=${JAVA_HOME} 36 ``` 37 38 Then, we use the built JDK as `JAVA_HOME` 39 40 ```bash 41 export JAVA_HOME=/$HOME/repos/babylon/build/macosx-aarch64-server-release/jdk/ 42 export PATH=$JAVA_HOME/bin:$PATH 43 ``` 44 45 #### 2. Build examples 46 47 ```bash 48 mvn clean package 49 ``` 50 51 #### 3. Run the examples 52 53 ##### Run HelloCodeReflection 54 55 ```bash 56 java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.HelloCodeReflection 57 ``` 58 59 ##### Run MathOptimizer 60 61 ```bash 62 java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizer 63 ```