Class ReflectionUtil

java.lang.Object
org.omegazero.common.util.ReflectionUtil

public final class ReflectionUtil extends Object
Contains utility methods for use with reflection.
Since:
2.1
  • Method Details

    • getMethodSignature

      public static String getMethodSignature(Method m)
      Returns the method type signature of the given Method.
      Parameters:
      m - The method
      Returns:
      The type signature of the given method
    • getMethodSignature

      public static String getMethodSignature(Class<?> returnType, Class<?>[] parameterTypes)
      Returns the method type signature of a method with the given returnType and parameterTypes.
      Parameters:
      returnType - The return type
      parameterTypes - The parameter types
      Returns:
      The type signature of a method with the given types
      See Also:
    • getClassesForSignature

      public static Class<?>[] getClassesForSignature(String sig) throws ClassNotFoundException
      Returns the array of Class objects for a given type signature list.

      For example, "ILjava/lang/String;" returns an array containing the Class representing the primitive int type and the Class representing String.

      The given string may also be a method type signature as returned by getMethodSignature(Class, Class[]). The return type signature is ignored.

      Parameters:
      sig - The type signature list
      Returns:
      The array of class objects
      Throws:
      ClassNotFoundException - If a class in the given signature was not found
      Since:
      2.12.0
      See Also:
    • getSignatureOfClass

      public static String getSignatureOfClass(Class<?> cl)
      Returns the type signature string of the given type, for example "I" or "Ljava/lang/String;". The inverse operation of getClassForTypeSignature(String).
      Parameters:
      cl - The class
      Returns:
      The type signature
      See Also:
    • getClassForTypeSignature

      public static Class<?> getClassForTypeSignature(String sig) throws ClassNotFoundException
      Returns the Class object for a given type signature. The inverse operation of getSignatureOfClass(Class).

      For example, "I" returns the Class representing the primitive int type, or "Ljava/lang/String;" returns the String class object.

      Parameters:
      sig - The type signature
      Returns:
      The class object
      Throws:
      ClassNotFoundException - If no class could be found that is represented by the given signature
      Since:
      2.12.0
      See Also:
    • isMethod

      public static boolean isMethod(Method m, String name, Class<?>[] parameterTypes)
      Checks whether the given method has the given name and parameterTypes. A parameter type of the given method may also be a superclass of a given expected parameter type.
      Parameters:
      m - The method
      name - The method name to compare to
      parameterTypes - The parameter types to compare to
      Returns:
      true if the given method has the given name and parameterTypes
    • getTypesFromObjectArray

      public static Class<?>[] getTypesFromObjectArray(Object[] array)
      Returns an array containing the types of each object in the given array. If an element in the given array is null, the returned array will contain the void class at its position.
      Parameters:
      array - The object array
      Returns:
      The array of types
      Since:
      2.7
    • getIntegerFieldNames

      public static String[] getIntegerFieldNames(Class<?> cl, String prefix, int lowest, int length, boolean readableNames) throws IllegalAccessException
      Gets the names of all public static int (possibly also final) fields starting with prefix of the given class.

      The returned array contains all names without the prefix. The index of each string is the value of the integer field minus lowest. If an index is outside of the created string array with the given length, the name is not included in the array.

      If readableNames is true, any _ characters in the field names are converted to spaces, and all characters are set to lowercase, except the first character of each word.

      Parameters:
      cl - The class
      prefix - The prefix
      lowest - The lowest integer value
      length - The length of the returned array
      readableNames - Whether to transform field names
      Returns:
      The string array
      Throws:
      IllegalAccessException - If a field is inaccessible
      Since:
      2.8
    • getClass

      public static Class<?> getClass(String name)
      Returns the Class object for the given class name.

      Similar to Class.forName(String), except that no exception is thrown when the class is not found, returning null instead.

      Parameters:
      name - The class name
      Returns:
      The Class, or null if the class was not found
      Since:
      2.12.0
    • getField

      public static <T> T getField(String identifier, Object instance) throws ReflectiveOperationException
      Reads the field identified by the given identifier, which has the format "<full class name>#<field name>".

      The instance is the instance object of which the field is read, if the field is not static. This method allows accessing private fields.

      For example, getField("java.lang.String#hash", "") returns 0 (the initial value).

      Parameters:
      identifier - The field identifier
      instance - The instance
      Returns:
      The value of the field (may be null)
      Throws:
      ReflectiveOperationException - If the class or field does not exist, or field access failed
      IllegalArgumentException - If instance is not an instance of the class in the identifier
      NullPointerException - If instance is null, but the field is non-static
      Since:
      2.12.0
      See Also:
    • getFieldOptional

      public static <T> Optional<T> getFieldOptional(String identifier, Object instance)
      Reads the field identified by the given identifier. The same as getField(String, Object), except that an empty Optional is returned if an exception is thrown.
      Parameters:
      identifier - The field identifier
      instance - The instance
      Returns:
      An Optional containing the value of the field (may be null), or an empty Optional if the field could not be accessed
      Since:
      2.12.0
      See Also:
    • callMethod

      public static <T> T callMethod(String identifier, Object... arguments) throws ReflectiveOperationException
      Calls the method identified by the given identifier, which has the format "<full class name>::<method signature>", with the given arguments.

      If the target method is non-static, the first argument of arguments is taken as the instance object used to call the method (and not explicitly passed to the method). This method allows accessing private methods.

      The method part of the identifier (the part after the ::) consists of the method name, followed by a parameter type signature of the same format as getClassesForSignature(java.lang.String) accepts, enclosed in parentheses (any return type after the closing parenthesis is ignored). If the method has no parameters, the parameter type signature parentheses may be omitted. If the method name equals the special string "<init>", the constructor of the class is called.

      For example, callMethod("java.lang.String::length", "example") returns 7 (the length of the string).

      Parameters:
      identifier - The method identifier
      arguments - The arguments to pass to the method, including an object instance
      Returns:
      The return value of the method (may be null, for example for void methods)
      Throws:
      ReflectiveOperationException - If the class or method does not exist, method access failed, or the method threw an exception
      IllegalArgumentException - If instance is not an instance of the class in the identifier, or the given arguments do not match the method parameter list
      NullPointerException - If instance is null, but the method is non-static
      Since:
      2.12.0
      See Also:
    • callMethodOptional

      public static <T> Optional<T> callMethodOptional(String identifier, Object... arguments)
      Calls the method identified by the given identifier. The same as callMethod(String, Object[]), except that an empty Optional is returned if an exception is thrown.
      Parameters:
      identifier - The method identifier
      arguments - The arguments to pass to the method, including an object instance
      Returns:
      An Optional containing the return value of the method (may be null), or an empty Optional if the method could not be called
      Since:
      2.12.0
      See Also:
    • get

      public static <T> T get(String identifier, Object... arguments) throws ReflectiveOperationException
      Combines getClass(String), getField(String, Object), and callMethod(String, Object[]) into a single method. Which of the three methods is called is determined by the identifier format.

      The meaning of arguments depends on the identifier. If the identifier is a method, arguments has the meaning as defined by callMethod(String, Object[]). If it is a field, an optional first argument is used as the object instance passed to getField(String, Object). If the identifier is a class, arguments is ignored.

      Parameters:
      identifier - The identifier
      arguments - The arguments
      Returns:
      The value of the identified object
      Throws:
      ReflectiveOperationException - If the class, field, or method does not exist, field or method access failed, or the method threw an exception
      IllegalArgumentException - If instance is not an instance of the class in the identifier, or the given arguments do not match the method parameter list
      NullPointerException - If instance is null, but the field or method is non-static
      Since:
      2.12.0
      See Also:
    • getOptional

      public static <T> Optional<T> getOptional(String identifier, Object... arguments)
      The same as get(String, Object[]), except that an empty Optional is returned if an exception is thrown.
      Parameters:
      identifier - The identifier
      arguments - The arguments
      Returns:
      An Optional containing the value of the identified object, or an empty Optional if the value could not be retrieved
      Since:
      2.12.0