Java获取不带扩展的泛型类型
我有这样一个简单的界面Java获取不带扩展的泛型类型,java,hibernate,generics,Java,Hibernate,Generics,我有这样一个简单的界面 public interface EntityClass { public void setId(final Integer id); public Integer getId(); } 还有一节课 public Student implements EntityClass{} 但是这个代码给了我一个错误,我想知道泛型类型 public class MyGenericType<T extends EntityClass>
public interface EntityClass
{
public void setId(final Integer id);
public Integer getId();
}
还有一节课
public Student implements EntityClass{}
但是这个代码给了我一个错误,我想知道泛型类型
public class MyGenericType<T extends EntityClass>
{
private final Class<? extends EntityClass>genericClazz;
public MyGenericType()
{
genericClazz=(Class<? extends EntityClass>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return;
}
}
这段代码我一直在使用它,但从子类扩展,但我不想从任何类扩展我的GenericType
如何才能从此代码中获取学生
类型
final MyGenericType<Student>action = new MyGenericType<>();
final MyGenericTypeaction=new MyGenericType();
因为我需要获得一个Hibernate namedQuery。
1)没有不从任何类继承的东西,每个东西都是对象类的扩展,所以即使您没有指定任何东西,默认情况下它也会扩展对象
2) 在泛型类中获取运行时类型有两种方法,可以对变量使用getClass,也可以在构造函数或方法中传入该类。
示例代码:
方法1(如有可能,首选):
公共类MyGenericType
{
公共void someMethod(最终T someVar)
{
类c=someVar.getClass();
}
}
方法2(如果需要在施工时确定,这是唯一的方法):
公共类MyGenericType
{
private final Class由于擦除,您无法获得有关泛型类型的信息,简而言之,这意味着Java编译器不会在生成的字节码中保留有关实际实例的泛型类型信息
但是,编译器不会删除实际实例的超类的泛型类型信息。因此,您可以使用(ParameterizedType)this.getClass().getGenericSuperclass()
在具有泛型类型参数的类的子体上,为此,需要使用扩展
关键字:
public interface EntityClass {
public void setId(final Integer id);
public Integer getId();
}
public class Student
implements EntityClass {
// getters and setters
}
public abstract class MyGenericType<T extends EntityClass> {
private final Class<T> genericClazz;
@SuppressWarnings("unchecked")
public MyGenericType() {
this.genericClazz = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
protected Class<T> getGenericClazz() {
return this.genericClazz;
}
}
public class MyStudentType // no way to avoid extending a generic type
extends MyGenericType<Student> {
@Override
public String toString() {
return this.getGenericClazz().getSimpleName();
}
}
public class Sample {
public static void main(String[] args) {
final MyStudentType action = new MyStudentType();
System.out.println(action); // Student
}
}
关于您得到的ClassCastException
,这是因为在您的示例中,MyGenericType
没有泛型超类(实际上,它没有任何超类,除了对象
,它不是参数化类型
。因此,当您使用此强制转换时:(参数化类型)getClass().getGenericSuperclass()
,您将得到一个ClassCastException
编辑:
我忘了您曾问过如何在不从任何类扩展的情况下执行此操作。一种方法是在构造函数中传递泛型类型:
genericClazz = (Class<? extends EntityClass>)...
public class MyGenericType<T extends EntityClass> {
private final Class<T> genericClazz;
public MyGenericType(Class<T> clazz) {
this.genericClazz = clazz;
}
@Override
public String toString() {
return this.genericClazz.getSimpleName();
}
}
然后:
public class Sample {
public static void main(String[] args) {
final MyGenericType<Student> action = new MyGenericType<>(Student.class);
System.out.println(action); // Student
}
}
public class Sample {
public static void main(String[] args) {
final MyStudentType action = new MyStudentType();
System.out.println(action); // Student
}
}
在运行时捕获泛型类型信息的唯一方法是通过extends
语句。任何其他类型信息都会被擦除。
genericClazz = (Class<? extends EntityClass>)...
genericClazz = (Class<T>)...
public class MyGenericType<T extends EntityClass> {
private final Class<T> genericClazz;
public MyGenericType(Class<T> clazz) {
this.genericClazz = clazz;
}
@Override
public String toString() {
return this.genericClazz.getSimpleName();
}
}
public class Sample {
public static void main(String[] args) {
final MyGenericType<Student> action = new MyGenericType<>(Student.class);
System.out.println(action); // Student
}
}
public abstract class MyGenericType<T extends EntityClass> {
protected abstract Class<T> getGenericType();
@Override
public String toString() {
return this.getGenericType().getSimpleName();
}
}
public class MyStudentType
extends MyGenericType<Student> {
@Override
protected Class<Student> getGenericType() {
return Student.class;
}
}
public class Sample {
public static void main(String[] args) {
final MyStudentType action = new MyStudentType();
System.out.println(action); // Student
}
}