[英]Create instance of generic type in Java when parameterized type is contained?
This is a follow-up to my question:这是我的问题的后续行动:
Create instance of generic type in Java when parameterized type passes through hierarchies? 当参数化类型通过层次结构时,在 Java 中创建泛型类型的实例?
For attempting to create a new generic from a contained class, I tried to adapt Steve B's approach of creating an anonymous subclass:为了尝试从包含的 class 创建一个新的泛型,我尝试采用 Steve B 创建匿名子类的方法:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class ParameterizedTypeEg {
ParameterizedTypeEg () {
ContainsParameterized<String> containString = new ContainsParameterized<String>();
}
public class Parameterized<E> {
Parameterized () {
}
public Class<E> getTypeParameterClass() {
Type type = getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) type;
return (Class<E>) paramType.getActualTypeArguments()[0];
}
public Constructor<E> getTypeParameterConstructor() {
Constructor<E> constructor = null;
try {
constructor = getTypeParameterClass().getConstructor(QueriedColor.class);
} catch (NoSuchMethodException e) { System.err.println(e); }
return constructor;
}
}
class ContainsParameterized<E> {
ContainsParameterized () {
Parameterized<E> contained = new Parameterized<E>(){};
try {
E element = contained.getTypeParameterConstructor().newInstance();
}
catch (InstantiationException e) { System.err.println(e); }
catch (IllegalAccessException e) { System.err.println(e); }
catch (InvocationTargetException e) { System.err.println(e); }
}
}
public static void main(String[] args) {
new ParameterizedTypeEg();
}
}
Please note the line Parameterized contained = new Parameterized(){};请注意 Parameterized contains = new Parameterized(){};
Here I am attempting to create the anonymous subclass, as suggested by Steve B in the other post.在这里,我正在尝试创建匿名子类,正如 Steve B 在另一篇文章中所建议的那样。 However, I get a ClassCastException in the getTypeParameterClass() method.
但是,我在 getTypeParameterClass() 方法中得到了 ClassCastException。 This is the same type of exception as in my other posting.
这与我的其他帖子中的异常类型相同。 That lead me to think that I could use the same solution as Steve B suggested for that problem.
这使我认为我可以使用与史蒂夫 B 为该问题建议的相同的解决方案。
The anonymous class must have the actual type parameter hard-coded in its declaration for it to work. 匿名类必须在其声明中使用硬编码的实际类型参数才能起作用。
new Parameterized<E>(){};
does not work. 不起作用。 You are just hard-coding "E" (a type variable).
您只是硬编码“ E”(类型变量)。 So when you look at the metadata using reflection all you see is "E" (a type variable).
因此,当您使用反射查看元数据时,您看到的只是“ E”(类型变量)。
You must create a subclass which hard-codes its superclass with an actual class as type argument at compile-time: 您必须创建一个子类,该子类在编译时使用实际的类作为类型参数将其超类硬编码:
new Parameterized<String>(){};
I tried to adapt Steve B's approach of creating an anonymous subclass:
我试图适应史蒂夫·B(Steve B)创建匿名子类的方法:
You missed the whole point of that. 您错过了整个要点。 The point was not to create a subclass.
关键不是要创建子类。 The point was to create a subclass with the actual class of the type argument hard-coded in the superclass .
关键是要创建一个具有超类中硬编码的类型实参类型的实际类的子类 。
What you're trying to do can work so long as E is parameterized in a type definition somewhere. 只要在某处的类型定义中对E进行了参数化,您要尝试执行的操作就可以起作用。 For example:
例如:
Parameterized<E> pe = new Parameterized<E>();
This will not allow you to resolve E
since it's not part of a type definition. 因为它不是类型定义的一部分,所以这将不允许您解析
E
On the otherhand, this: 另一方面,这是:
class StringE extends Parameterized<String> {}
or this: 或这个:
Parameterized<String> ps = new Parameterized<String>(){};
will work since we're specifying the value of E as part of a type definition. 将起作用,因为我们将E的值指定为类型定义的一部分。 To resolve the value of
E
, you might use TypeTools : 要解析
E
的值,可以使用TypeTools :
Class<?> stringType = TypeResolver.resolveRawArgument(Parameterized.class, ps.getClass());
assert stringType == String.class;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.