[code-reflection] RFR: New example suite for code reflection

Paul Sandoz psandoz at openjdk.org
Fri Aug 8 14:52:31 UTC 2025


On Wed, 6 Aug 2025 10:39:43 GMT, Juan Fumero <duke at openjdk.org> wrote:

> New example suite for code reflection. It includes two programs that illustrate different functionality of the code reflection API. 
> 
> ### How to build?
> 
> This module is built using the code-reflection fork of OpenJDK. Then, update the `JAVA_HOME` to point to the code-reflection port, and compile with maven. In detail: 
> 
> #### 1. Build Babylon JDK
> 
> We need to use the JDK build that enables the code reflection API (Babylon).
> 
> 
> git clone https://github.com/openjdk/babylon 
> cd babylon
> bash configure --with-boot-jdk=${JAVA_HOME}
> 
> 
> Then, we use the built JDK as `JAVA_HOME`
> 
> 
> export JAVA_HOME=/$HOME/repos/babylon/build/macosx-aarch64-server-release/jdk/
> export PATH=$JAVA_HOME/bin:$PATH
> 
> 
> #### 2. Build the example suite
> 
> 
> mvn clean package
> 
> 
> #### 3. Run the examples
> 
> ##### Run HelloCodeReflection
> 
> 
> java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.HelloCodeReflection
> 
> 
> ##### Run MathOptimizer
> 
> 
> java --enable-preview -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizer
> 
> 
> More example to appear in this module.

Looks good just one minor comment inline you could address now or later up to you.

In subsequent PRs we could add more examples e.g., taken from say [`TestLocalTransformationsAdaption`
](https://github.com/openjdk/babylon/blob/code-reflection/test/jdk/java/lang/reflect/code/TestLocalTransformationsAdaption.java
)

cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java line 87:

> 85: 
> 86:     // if pow(x, 2) then substitute for this function
> 87:     private static double functionMult(double x) {

A more complex example to consider later after this PR would be to make these methods reflectable, and then you can inline these methods into the transformed model so `Math.pow` is substituted with direct expressions.

cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java line 129:

> 127:                 case JavaOp.InvokeOp invokeOp when whenIsMathPowFunction(invokeOp) -> {
> 128:                     // The idea here is to create a new JavaOp.invoke with the optimization and replace it.
> 129:                     List<Value> operands = blockBuilder.context().getValues(op.operands());

Strictly speaking `operands` are the _output_ operands of values in output model we are building. We get them from the _input_ operands, values in the input model. This works because the transformation has already mapped those input values to output values, since it already encountered them (when copying of the func op and its parameters).

cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java line 176:

> 174:                         // invoke function.
> 175:                         List<Value> newOperandList2 = new ArrayList<>();
> 176:                         newOperandList2.add(operands.get(0));

You don't need to create list (or a sub list). The `Java.invoke` method is overloaded to accept a var args or a list. In this case we can use the former and just pass `operands.get(0)` directly, or more clearly `operands.getFirst()`.

cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java line 260:

> 258: 
> 259:     // Inspect a value for a parameter
> 260:     static boolean inspectParameterRecursive(JavaOp.ConvOp convOp, int valToMatch) {

At some point we will get better at pattern matching, we need the language enhancements for pattern matching.

In the interim see the experiment in `jdk.incubator.code.analysis.Patterns` and its use [here](https://github.com/openjdk/babylon/blob/code-reflection/test/jdk/java/lang/reflect/code/ad/ExpressionElimination.java#L58). Not suggesting you use it here, just for information which you might find interesting.

-------------

Marked as reviewed by psandoz (Lead).

PR Review: https://git.openjdk.org/babylon/pull/513#pullrequestreview-3101083514
PR Review Comment: https://git.openjdk.org/babylon/pull/513#discussion_r2263169163
PR Review Comment: https://git.openjdk.org/babylon/pull/513#discussion_r2263177769
PR Review Comment: https://git.openjdk.org/babylon/pull/513#discussion_r2263159978
PR Review Comment: https://git.openjdk.org/babylon/pull/513#discussion_r2263189072


More information about the babylon-dev mailing list