Annotation attributes with type parameters

前端 未结 5 708
甜味超标
甜味超标 2020-12-14 06:22

When you define a Java interface, it\'s possible to declare a method with type parameters, for example like this:

public interface ExampleInterface {
    <         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 07:27

    They wanted to introduce annotations in order for people only to use them as ,,,well annotations. And prevent developers from putting logic in them. i.e. start programming stuff using annotations, which might have an effect of making Java look like a very different language in my opinion. Hence the context free syntax note in Java Language Specification.

    The following restrictions are imposed on annotation type declarations by virtue of their context free syntax:

    Annotation type declarations cannot be generic.
    No extends clause is permitted. (Annotation types implicitly extend annotation.Annotation.)
    Methods cannot have any parameters
    Methods cannot have any type parameters
    

    (http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html)

    To better understand what I mean, look at what this JVM hacker does: http://ricken.us/research/xajavac/

    He creates And, Or annotations as instructions and processes other annotations using them. Priceless!

提交回复
热议问题