Search code examples
javareflectionbukkititerablenms

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


Solution

  • Generally with Java reflection the type arguments for retrieving a method (or constructor) are the arguments after type erasure i.e 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;
    }