简体   繁体   English

寻找Iterable的类以使用反射创建构造函数

[英]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.

相关问题 使用Reflection-with构造函数参数在Abstract Class中创建实例 - Create an instance within Abstract Class using Reflection-with constructor parameter 使用反射使用特定的 class 构造函数并创建新的 object - Using reflection to use a specific class constructor and create a new object Java反射创建未知类和构造函数的实例 - Java reflection create instance of unknown class and constructor 如何使用反射调用内部类的构造函数 - How to invoke constructor of inner class using reflection 使用反射在构造函数中创建带参数的对象 - Using reflection to create object with parameters in constructor 使用反射在类内调用构造函数 - Invoke Constructor inside a class using Reflection 使用Reflection使用基类构造函数重写超类构造函数 - Override super class constructor with base class constructor using Reflection 是否有可能在java中使用反射创建没有arg构造函数的类的“空白”实例? - Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection? 如何使用零参数构造函数创建具有通过Java反射绑定的上下文的Scala类的新实例? - How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor? 在构造函数中的类数组与反射 - Class array in constructor with reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM