简体   繁体   中英

Java Generics and Annotations: Type-Safe

I know that I can do something like:

public class AbstractDao<T extends Bean> {

}

In this case, Bean is an class or interface.

I was wondering if is there a way to do this with annotations, like:

public class AbstractDao<T extends javax.persistence.Entity> {

}

Is something like this possible?

Thanks in advance!

Unfortunately, there is no inheritance in annotations, so it's not possible.

Have a look at the answers to the similar question here:

Why is not possible to extend annotations in Java?

No. The Java Language Specification says that type variables can only be constrained to extend / implement particular classes or interfaces. Since annotating a class does not cause the class to extend or implement a particular type, you can't constrain by annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM