user5182503
user5182503

Reputation:

It is possible to restrict Class<?> with annotation classes?

Lets suppose I have two annotation classes: Annotation1 and Annotation2.

and I have a method:

public void doSomethingWithAnnodation(Class<?> annotationClass) {
...
}

The problem with this method is that any class can be passed as argument. Can I make compiler to allow only annotation classes (Annotation1 and Annotation2)?

Upvotes: 1

Views: 49

Answers (1)

Slaw
Slaw

Reputation: 46315

You can specify java.lang.annotation.Annotation, which all annotations implement, as an upper bound:

public void doSomethingWithAnnotation(Class<? extends Annotation> annotationClass) {
  //...
}

Upvotes: 3

Related Questions