mercredi 30 mars 2022

Java: Runtime reflection in compilation phase (?!)

In Element#getAnnotation(Class<A> annotationType) javadoc it is stated that

Note: This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation types currently loaded into the VM — rather than on the representations defined by and used throughout these interfaces. Consequently, calling methods on the returned annotation object can throw many of the exceptions that can be thrown when calling methods on an annotation object returned by core reflection. This method is intended for callers that are written to operate on a known, fixed set of annotation types.

Yet, this method is frequently used in annotation processing which is part of the compilation phase. What I want to understand is what, how, and why things gets loaded to VM at compilation time, and what are the pros and cons.

For example, in the case of Element#getAnnotation(Class<A> annotationType), is there any drawbacks (except possibly not being able to access values of type class in the annotation), compared to if we get the same information using mirrors (which is usually the longer code)?





Aucun commentaire:

Enregistrer un commentaire