Preview features and class file formats
Alex Buckley
alex.buckley at oracle.com
Mon Apr 21 21:21:54 UTC 2025
On 4/21/2025 1:53 PM, Chen Liang wrote:
> Per JEP 12, reflective preview APIs do not allow preview features to be
> used; rather, they allow programs compiled without preview to run
> correctly in a Java Runtime Environment running with preview features
> enabled.
Yes, that's one way of looking at it. The program compiled without
preview is typically a framework that wants to run everywhere. If a user
chooses to exploit preview features in their code, by passing
--enable-preview at compile time and run time, then the framework when
running on this user's machine can (via reflective preview APIs)
recognize and adapt to the user's preview-using classes.
For example, when records were previewing in Java 14, a framework that
recognized a user class as a record (via Class::isRecord, which today
we'd call a reflective preview API) could deserialize it more
efficiently than normal classes, since records can't self-reference.
> I just reviewed the preview APIs introduced for Value Objects JEP: it
> has a few, such as IdentityException, Objects.requireIdentity, that
> perform inspection on object instances but are not marked as reflective.
> I believe they should be marked reflective, as they are critical for
> non-preview programs to run correctly when preview is enabled (when they
> can receive value objects, and JVM can throw IdentityException for
> certain bytecode instructions).
Yes. There's little downside in marking a preview API concerned with
reflection as a reflective (rather than normal) preview API.
> Meanwhile, the constructor for IdentityException are all essential
> preview APIs - they are necessary for the Value Objects feature, but
> not for non-preview programs to run correctly when preview features
> are enabled.
This sounds right.
> In particular, can inspection on object instances via
> getClass() be considered reflection as well?
It is reflection, but I don't see how it connects to the Value Objects
API you mentioned earlier.
Alex
More information about the compiler-dev
mailing list