Class ReflectHelper
java.lang.Object
org.mvplugins.multiverse.core.utils.ReflectHelper
Utility class used to help in doing various reflection actions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable Class
<?> Try to get theClass
based on its classpath.static <C> @Nullable Field
Try to get aField
from a given class.static <C> @Nullable Field
Try to get aField
from a given class.static <C,
V> V getFieldValue
(C classInstance, @Nullable String fieldName, @NotNull Class<V> fieldType) Gets the value of a field name from an instance of the class responsible.static <C,
V> V getFieldValue
(C classInstance, @Nullable Field field, @NotNull Class<V> fieldType) Gets the value of aField
from an instance of the class responsible.static <C> @Nullable Method
Try to get aMethod
from a given class.static <C> @Nullable Method
Try to get aMethod
from a given class.static boolean
Check if theClass
for a give classpath is present/valid.static <C,
R> R invokeMethod
(C classInstance, Method method, Object... parameters) Calls aMethod
.
-
Constructor Details
-
ReflectHelper
public ReflectHelper()
-
-
Method Details
-
getClass
Try to get theClass
based on its classpath.- Parameters:
classPath
- The target classpath.- Returns:
- A
Class
if found, else null.
-
hasClass
Check if theClass
for a give classpath is present/valid.- Parameters:
classPath
- Target classpath.- Returns:
- True if class path is a valid class, else false.
-
getMethod
@Nullable public static <C> @Nullable Method getMethod(Class<C> clazz, String methodName, Class<?>... parameterTypes) Try to get aMethod
from a given class.- Type Parameters:
C
- The class type.- Parameters:
clazz
- The class to search the method on.methodName
- Name of the method to get.parameterTypes
- Parameters present for that method.- Returns:
- A
Method
if found, else null.
-
getMethod
@Nullable public static <C> @Nullable Method getMethod(C classInstance, String methodName, Class<?>... parameterTypes) Try to get aMethod
from a given class.- Type Parameters:
C
- The class type.- Parameters:
classInstance
- Instance of the class to search the method on.methodName
- Name of the method to get.parameterTypes
- Parameters present for that method.- Returns:
- A
Method
if found, else null.
-
invokeMethod
Calls aMethod
.- Type Parameters:
C
- The class type.R
- The return type.- Parameters:
classInstance
- Instance of the class responsible for the method.method
- The method to call.parameters
- Parameters needed when calling the method.- Returns:
- Return value of the method call if any, else null.
-
getField
Try to get aField
from a given class.- Type Parameters:
C
- The class type.- Parameters:
clazz
- The class to search the field on.fieldName
- Name of the field to get.- Returns:
- A
Field
if found, else null.
-
getField
Try to get aField
from a given class.- Type Parameters:
C
- The class type.- Parameters:
classInstance
- Instance of the class to search the field on.fieldName
- Name of the field to get.- Returns:
- A
Field
if found, else null.
-
getFieldValue
@Nullable public static <C,V> V getFieldValue(C classInstance, @Nullable @Nullable Field field, @NotNull @NotNull Class<V> fieldType) Gets the value of aField
from an instance of the class responsible.- Type Parameters:
C
- The class type.V
- The field value type.- Parameters:
classInstance
- Instance of the class to get the field value from.field
- The field to get value from.fieldType
- Type of the field.- Returns:
- The field value if any, else null.
-
getFieldValue
@Nullable public static <C,V> V getFieldValue(C classInstance, @Nullable @Nullable String fieldName, @NotNull @NotNull Class<V> fieldType) Gets the value of a field name from an instance of the class responsible.- Type Parameters:
C
- The class type.V
- The field value type.- Parameters:
classInstance
- Instance of the class to get the field value from.fieldName
- Name of the field to get value from.fieldType
- Type of the field.- Returns:
- The field value if any, else null.
-