简体   繁体   中英

Looking for the class of an Iterable to create a constructor using reflection

Good day,

I've got a question regarding reflection in Java. I want to instanciate a constructor for a class PacketPlayOutPlayerInfo using the following constructor:

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?)

Thanks in advance and have a nice day,

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.

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. For example trying to declare the following two methods is NOT legal in a Java class:

public int sameName(List<String> stringList) {
  return 1;
}

public int sameName(List<Integer> intList) {
  return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM