interface BaseDao<T> { }
interface UserDao extends BaseDao<User> { }
interface DeptDao extends BaseDao<Department> { }
I need a function get Entity
class via Dao
class, like this:
func(UserDao.class) return User.class
func(DeptDao.class) return Deparment.class
and I don't like add annotation
todao
, like this:
@Entity(User.class)
interface UserDao extends BaseDao
---------------------------------------------------------
inspired by dimo414 's suggestion, this is my solution:
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Main {
public static void main(String[] args) {
// if A and B are Class, then it be 'B.class.getGenericSuperclass()'
Type genType = B.class.getGenericInterfaces()[0];
if (genType instanceof ParameterizedType) {
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
Class<?> clz = (Class) params[0];
System.out.println(clz); // class java.lang.String
}
}}
interface A<T> {}
interface B extends A<String> {}