Alex Martian
Alex Martian

Reputation: 3782

java: what does it mean to invoke interface type?

I'm reading java language specifications (JLS): annotations

An annotation denotes a specific invocation of an annotation type (§9.6)

And in 9.6:

An annotation type declaration specifies a new annotation type, a special kind of interface type.

So e.g. @annotation1 should invoke annotation type annotation1. I could not find info what it means by web search or questions here. All I've found only about invocations of methods of interfaces, not interface types. I've read what some build-in annotations do of cause, e.g. infamous @Override, however I want clear knowledge preferably with links to JLS what interface type invocation is as I've read annotations are used by many useful frameworks which I want to use efficiently.

Upvotes: 1

Views: 131

Answers (2)

Child Detektiv
Child Detektiv

Reputation: 237

As far as my understanding of the link jls you posted, annotations types are special interface types and one should not think that invocation in that context means same invocation as say for a method. Compiler inserts markers with code and they can be used later (or right during compilation as with @override) .

Upvotes: 1

Thomas
Thomas

Reputation: 88707

If you write @SomeAnnotation you basically create in instance of that annotation type (there might be some caching but I'm not aware of this). This becomes especially apparent when an annotation has data, e.g. @SomeAnnotation(name="Alexei"). In that case you could get the annotation instance of type SomeAnnotation and then invoke name() on it to get the value "Alexei").

I've read annotations are used by many useful frameworks which I want to use efficiently.

Most frameworks use reflection to inspect your classes and collect information on the annotations, e.g. via Class.getAnnotation(SomeAnnotation.class) and many other similar methods. That information is then used to do whatever the framework needs them for, e.g. CDI would use the scope annotations to build its internal bean repository.

To use those frameworks you don't have to know about the specifics of how they use the annotations or what invocation actually means. Just use the annotations themselves as the framework requires you to do.

If you want to develop your own framework then you might need some more information, especially on the reflection capabilities.

Upvotes: 2

Related Questions