[英]Looking for the class of an Iterable to create a constructor using reflection
Good day, 美好的一天,
I've got a question regarding reflection in Java. 我有一个关于Java反射的问题。 I want to instanciate a constructor for a class
PacketPlayOutPlayerInfo
using the following constructor: 我想使用以下构造函数为类
PacketPlayOutPlayerInfo
实例化一个构造函数:
public PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction paramEnumPlayerInfoAction, Iterable<EntityPlayer> paramIterable){
[...]
}
To build the constructor i use this method 要构建构造函数,我使用此方法
Constructor<?> packetPlayerInfoConstructor = ReflectionHandler.getNMSClass("PacketPlayOutPlayerInfo").getConstructor(
ReflectionHandler.getNMSClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction"), _____);
The first argument is working perfectly fine, but I somehow have to get the class of the interface Iterable to get the constructor to work... (or do I?) 第一个参数工作得非常好,但是我必须以某种方式获取接口的类Iterable才能使构造函数正常工作(或者是吗?)
Thanks in advance and have a nice day, 预先感谢,祝您有美好的一天,
rapt0r rapt0r
Generally with Java reflection the type arguments for retrieving a method (or constructor) are the arguments after type erasure ie in your example the type arguments are PacketPlayOutPlayerInfo.EnumPlayerInfoAction and Iterable after type erasure, so the answer to your comment "(or do I)" should be No, you should only need the generic type. 通常,通过Java反射,用于检索方法(或构造函数)的类型参数是类型擦除之后的参数,即在您的示例中,类型参数是PacketPlayOutPlayerInfo.EnumPlayerInfoAction和类型擦除之后的Iterable,因此,注释的答案是“(或我)” ”应该为否,您只需要通用类型。
You CANNOT create two methods in a class that differ only by their parameterized type argument, hence the above does not make any valid Java methods inaccessible. 您不能在一个类中创建两个方法,它们的唯一区别在于它们的参数化类型参数,因此,上述方法不会使任何有效的Java方法均不可访问。 For example trying to declare the following two methods is NOT legal in a Java class:
例如,尝试声明以下两个方法在Java类中是不合法的:
public int sameName(List<String> stringList) {
return 1;
}
public int sameName(List<Integer> intList) {
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.