Class ConstructorMatch<T>

java.lang.Object
org.wpilib.util.ConstructorMatch<T>
Type Parameters:
T - the type of the class to find the constructor for

public class ConstructorMatch<T> extends Object
Utility class to find the best matching constructor for a given set of parameter types. The constructor must have parameter types that are assignable from the given parameter types, and the parameter types must not be assignable to each other. If multiple constructors match, the one with the most specific parameter types is chosen. If there is still a tie, the one with the most specific first parameter type is chosen, then the second parameter type, and so on.
  • Constructor Details

    • ConstructorMatch

      public ConstructorMatch(Constructor<T> constructor, Class<?>... parameterTypes)
      Constructs a ConstructorMatch with the given constructor and parameter types. The parameter types must not be assignable to each other.
      Parameters:
      constructor - the constructor to match
      parameterTypes - the parameter types for the constructor
  • Method Details

    • newInstance

      Creates a new instance of the constructor's class using the given arguments. The arguments must match the parameter types of the constructor, and must not be assignable to each other. The order of the arguments does matter, as they will be matched to the constructor parameter types in order.
      Parameters:
      args - the arguments to pass to the constructor
      Returns:
      a new instance of the constructor's class
      Throws:
      ReflectiveOperationException - if the constructor cannot be invoked
    • findBestConstructor

      public static <T> Optional<ConstructorMatch<T>> findBestConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Finds the best matching constructor for the given class and parameter types. The constructor must have parameter types that are assignable from the given parameter types, and the parameter types must not be assignable to each other. If multiple constructors match, the one with the most specific parameter types is chosen. If there is still a tie, the one with the most specific first parameter type is chosen, then the second parameter type, and so on. The order of the parameter types does matter, as they will be matched to the constructor parameter types in order.
      Type Parameters:
      T - the type of the class to find the constructor for
      Parameters:
      clazz - the class to find the constructor for
      parameterTypes - the parameter types to match
      Returns:
      an Optional containing the best matching ConstructorMatch, or empty if no match is found
    • getConstructor

      Returns the constructor that was matched.
      Returns:
      the constructor that was matched
    • getParameterTypes

      Returns the parameter types for the constructor.
      Returns:
      the parameter types for the constructor