micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.07k stars 1.07k forks source link

Ability to generate AnnotationMetadata for arbitrary types #1585

Open damianw opened 5 years ago

damianw commented 5 years ago

A number of built-in annotations, such as Introspected, SIngleton, etc trigger compile-time AnnotationMetadata generation. Although Introspected is the most general, it is somewhat restrictive because it only applies to beans and cannot be used on, for example, enums or Kotlin objects. It may be desirable to generate AnnotationMetadata for arbitrary types if reflection is not a good option (or not possible, such as when using GraalVM).

jameskleeh commented 5 years ago

@damianw

Although Introspected is the most general, it is somewhat restrictive because it only applies to beans

That isn't true. You can apply @Introspected to a non bean. I'm not aware of a place where the annotation is applied to a bean.

cannot be used on, for example, enums

It doesn't make sense to apply the annotation to enums. They don't need reflection to create instances.

or Kotlin objects

If the annotation isn't working on objects in Kotlin and it makes sense that it should, please file an issue.

It may be desirable to generate AnnotationMetadata for arbitrary types if reflection is not a good option (or not possible, such as when using GraalVM).

I'm not sure what you're suggesting here. How we would know whether a given arbitrary type should have annotation metadata generated? What would it be used for?

damianw commented 5 years ago

That isn't true. You can apply @Introspected to a non bean. I'm not aware of a place where the annotation is applied to a bean.

Fair enough. I meant more specifically "classes that I don't need to create instances of", like objects and enums (or even any class being used as just an annotation holder).

It doesn't make sense to apply the annotation to enums. They don't need reflection to create instances.

True - this feature request is about generating just annotation metadata, not a full BeanIntrospection. Essentially, separating out the annotation metadata feature from other features of BeanIntrospection, since for things like enums or objects, one may have a need to use annotation metadata, but constructor introspection / property introspection / instantiation aren't required (or possible).

If the annotation isn't working on objects in Kotlin and it makes sense that it should, please file an issue.

Good news is that it seems to be working fine considering objects are similar to enums in that they can't be instantiated externally.

I'm not sure what you're suggesting here. How we would know whether a given arbitrary type should have annotation metadata generated?

Some kind of annotation which triggers the generation I suppose, similarly to @Introspected.

What would it be used for?

Similar to Introspected, really - any use case where one might want to introspect AnnotationMetadata. That's not possible with @Introspected because it requires that the type also be instantiable.