Class ConstructorMatch<T>
java.lang.Object
org.wpilib.util.ConstructorMatch<T>
- Type Parameters:
T- the type of the class to find the constructor for
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 Summary
ConstructorsConstructorDescriptionConstructorMatch(Constructor<T> constructor, Class<?>... parameterTypes) Constructs a ConstructorMatch with the given constructor and parameter types. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<ConstructorMatch<T>> findBestConstructor(Class<T> clazz, Class<?>... parameterTypes) Finds the best matching constructor for the given class and parameter types.Returns the constructor that was matched.Returns the parameter types for the constructor.newInstance(Object... args) Creates a new instance of the constructor's class using the given arguments.
-
Constructor Details
-
ConstructorMatch
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 matchparameterTypes- 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 forparameterTypes- 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
-