<div dir="auto">Thanks for putting this together Tagir. It's exciting to see what babylon will be able to provide us if stuff like this is fairly easy to do.</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Tue, Dec 2, 2025, 4:59 AM Tagir Valeev <<a href="mailto:amaembo@gmail.com">amaembo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello!<div><br></div><div>A recent discussion in amber-dev [1] lead me to the following example how people may use Code Reflection.</div><div>The problem:</div><div>Retrieve a RecordComponent object from the corresponding record accessor method reference. Expect the following API:</div><div><br></div><div>private record MyRecord(int comp, double comp2) {}</div><div>...</div><div>RecordComponent component = ReflectUtil.component(MyRecord::comp);</div><div><br></div><div>It looks like it's possible to solve this with Code Reflection. My solution is the following:</div><div><br></div><div>package org.example;<br><br>import jdk.incubator.code.Quoted;<br>import jdk.incubator.code.Reflect;<br>import jdk.incubator.code.dialect.core.CoreOp;<br>import jdk.incubator.code.dialect.java.JavaOp;<br>import jdk.incubator.code.dialect.java.MethodRef;<br><br>import java.lang.invoke.MethodHandles;<br>import java.lang.reflect.Method;<br>import java.lang.reflect.RecordComponent;<br>import java.util.Arrays;<br><br>public final class ReflectUtil {<br> @Reflect<br> @FunctionalInterface<br> public interface RecordAccessor<T extends Record> {<br> /**<br> * @param r record type<br> * @return result of the record component<br> * @param <R> an unused generic parameter to avoid accidental use with lambda<br> * (lambdas cannot bind to generic SAM). <br> * Only method references are supported. <br> */<br> <R> Object get(T r);<br> }<br><br> /**<br> * @param accessor a method reference bound to a record accessor<br> * @return record component that corresponds to a given accessor <br> * @param <T> record type<br> * @throws IllegalArgumentException if the supplied argument is not a record accessor method reference<br> * @throws RuntimeException if the record accessor cannot be resolved (e.g., due to insufficient access rights)<br> */<br> public static <T extends Record> RecordComponent component(RecordAccessor<T> accessor) {<br> Quoted quoted = CoreOp.QuotedOp.ofQuotable(accessor)<br> .orElseThrow(() -> new IllegalArgumentException("Accessor must be quotable"));<br> if (!(quoted.op() instanceof JavaOp.LambdaOp lambda)) {<br> throw new IllegalArgumentException("Expected method reference");<br> }<br> JavaOp.InvokeOp invokeOp = lambda.methodReference()<br> .orElseThrow(() -> new IllegalArgumentException("Expected method reference"));<br> if (invokeOp.invokeKind() != JavaOp.InvokeOp.InvokeKind.INSTANCE) {<br> throw new IllegalArgumentException("Method reference bound to record accessor is expected");<br> }<br> MethodRef descriptor = invokeOp.invokeDescriptor();<br> Method method;<br> try {<br> method = descriptor.resolveToMethod(MethodHandles.privateLookupIn(accessor.getClass(), MethodHandles.lookup()));<br> } catch (ReflectiveOperationException e) {<br> throw new RuntimeException("Cannot resolve descriptor "+descriptor, e);<br> }<br> Class<?> recordClass = method.getDeclaringClass();<br> if (!recordClass.isRecord()) {<br> throw new IllegalArgumentException("Not a record accessor method");<br> }<br> return Arrays.stream(recordClass.getRecordComponents()).filter(rc -> rc.getAccessor().equals(method))<br> .findFirst()<br> .orElseThrow(() -> new IllegalArgumentException("Not a record accessor: "+method));<br> }<br>}<br></div><div><br></div><div>I'm not completely sure if privateLookupIn is the right thing to do here. Otherwise, it's quite a pleasant experience: the code is simple and self-explanatory. LambdaOp::methodReference was very helpful.</div><div><br></div><div>With best regards,</div><div>Tagir Valeev</div><div><br></div><div><br></div><div>[1] <a href="https://mail.openjdk.org/pipermail/amber-dev/2025-November/009472.html" target="_blank" rel="noreferrer">https://mail.openjdk.org/pipermail/amber-dev/2025-November/009472.html</a></div></div>
</blockquote></div>