Reflection on records

Archie Cobbs archie.cobbs at gmail.com
Mon Dec 1 17:13:48 UTC 2025


I'm guessing what underlies David's question is this thought: Why can't the
compiler provide more compile-time checking of Field and Method reflection
objects?

After all, it does this for Class objects by providing us with Class
literals (remember in the old days you could only obtain them via
Class.forName()).

I have wondered the same thing, though I also appreciate that the devil is
in the details.

For example, David's question could be addressed by adding Method and Field
literals:

public class Foo {
    public static void meth(int x) { ... }
}

Method m = Foo::meth.method;
m.invoke(123);

Of course, this is still imprecise because Method is not generic and so
this would fail if there were overloads, among other problems.

So I think the intuition is valid. Whether a practical solution exists is
another question.

-Archie

On Mon, Dec 1, 2025 at 10:46 AM Chen Liang <chen.l.liang at oracle.com> wrote:

> Hi David, before we investigate solutions, what is the problem you want to
> resolve?
> If you want to obtain a Method on a record class, you can do
> recordClass.getMethod("variable") to find that accessor method.
> Note that the Java Virtual Machine does not validate a record, that it may
> recognize a class with the record attribute but has no corresponding
> accessor methods or canonical constructor as a record class.
>
-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20251201/c72beda9/attachment-0001.htm>


More information about the amber-dev mailing list