Reflection on records
Tagir Valeev
amaembo at gmail.com
Mon Dec 1 19:17:54 UTC 2025
Hello!
On Mon, Dec 1, 2025, 20:01 Remi Forax <forax at univ-mlv.fr> wrote:
> the current status quo is that IDEs (at least IntelliJ) are able to
> typecheck the creation of Method, Field, MethodHandle, VarHandle, etc so
> it's not in the language but it's good enough.
>
Yes, thank you, Remi! David said that he wants to avoid stringly-typed
code, but what is string is solely a question of a tool you use to
interpret your program. For an old-school text editor, the whole Java
program is simply a string. For example, a typo in a called method name
will be unnoticed by such a tool in the same way, as a typo inside a string
literal. The Java compiler understands proper Java identifiers but doesn't
understand Java strings. A good IDE goes further: it treats certain strings
as full-fledged references. When you type
MyRecord.class.getField("myComponent"), you get code completion, inspection
highlighting of incorrect reference (distinct from compilation error and
suppressable, of course), find usage, navigation to the declaration (you
can jump from "myComponent" string to an actual component and back). You
can even initiate a 'Rename' refactoring inside the string, and the
component will be renamed! From the tool point of view, these strings are
first-class citizens.
So it's not always necessary to change your code to avoid being
stringly-typed. Instead, use proper tools or convince your tool vendor to
add first-class reflective references support.
With best regards,
Tagir Valeev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20251201/451629b5/attachment.htm>
More information about the amber-dev
mailing list