Class ReflectionUtils
java.lang.Object
com.thecsdev.common.util.ReflectionUtils
Utility methods for
java.lang.reflect.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanconstructorMatchesArgs(Constructor<?> constructor, Object... args) Compares aConstructor's parameter types against anObjectarray, checking if the arguments match the constructor.static <T> TcreateClassInstance(Class<T> type, Object... params) Creates an instance of aClassusing the first constructor that matches the given parameters.static final @NotNull Field[]getAllDeclaredFields(@Nullable Class<?> clazz) static booleanisMethodOverridden(Class<?> topClass, String methodName, Class<?> returnType, Class<?>... paramTypes) Checks if the givenClassoverrides a method with the specified signature.
-
Method Details
-
getAllDeclaredFields
-
constructorMatchesArgs
Compares aConstructor's parameter types against anObjectarray, checking if the arguments match the constructor.- Parameters:
constructor- TheConstructorto check.args- The arguments for saidConstructor.- Returns:
- The result of the comparison.
trueif it's a match. - API Note:
- Depends on Guava -
com.google.*.
-
createClassInstance
public static <T> T createClassInstance(Class<T> type, Object... params) throws NullPointerException Creates an instance of aClassusing the first constructor that matches the given parameters. Pass no parameters to use a constructor that does not take any arguments. Non-primitive argument types may be assignednull.
The constructor must also be accessible. If a matching and accessible constructor cannot be found,nullis returned.- Type Parameters:
T- TheClasstype.- Parameters:
type- TheClasstype.params- TheClassconstructor parameters.- Throws:
NullPointerException- If an argument isnull.- API Note:
- Depends on Guava -
com.google.*.
-
isMethodOverridden
@Experimental public static boolean isMethodOverridden(Class<?> topClass, String methodName, Class<?> returnType, Class<?>... paramTypes) throws NullPointerException Checks if the givenClassoverrides a method with the specified signature.- Parameters:
topClass- Class to start checking from.methodName- Name of the method.returnType- Expected return type.paramTypes- Expected parameter types.- Returns:
trueif the method is overridden in theClasshierarchy,falseotherwise.- Throws:
NullPointerException- If an argument isnull.- API Note:
- This does not account for
interfaces yet!
-