Class ReflectionUtils

java.lang.Object
io.hypersistence.utils.hibernate.util.ReflectionUtils

public final class ReflectionUtils extends Object
ReflectionUtils - Reflection utilities holder.
Since:
1.0
Author:
Vlad Mihalcea
  • Method Details

    • newInstance

      public static <T> T newInstance(String className)
      Instantiate a new Object of the provided type.
      Type Parameters:
      T - class type
      Parameters:
      className - The fully-qualified Java class name of the Object to instantiate
      Returns:
      new Java Object of the provided type
    • newInstance

      public static <T> T newInstance(Class clazz)
      Instantiate a new Object of the provided type.
      Type Parameters:
      T - class type
      Parameters:
      clazz - The Java Class of the Object to instantiate
      Returns:
      new Java Object of the provided type
    • newInstance

      public static <T> T newInstance(Class clazz, Object[] args, Class[] argsTypes)
      Instantiate a new Object of the provided type.
      Type Parameters:
      T - class type
      Parameters:
      clazz - The Java Class of the Object to instantiate
      args - The arguments that need to be passed to the constructor
      argsTypes - The argument types that need to be passed to the constructor
      Returns:
      new Java Object of the provided type
    • getField

      public static Field getField(Class targetClass, String fieldName)
      Get the Field with the given name belonging to the provided Java Class.
      Parameters:
      targetClass - the provided Java Class the field belongs to
      fieldName - the Field name
      Returns:
      the Field matching the given name
    • getFieldOrNull

      public static Field getFieldOrNull(Class targetClass, String fieldName)
      Get the Field with the given name belonging to the provided Java Class or null if no Field was found.
      Parameters:
      targetClass - the provided Java Class the field belongs to
      fieldName - the Field name
      Returns:
      the Field matching the given name or null
    • getFieldValueOrNull

      public static <T> T getFieldValueOrNull(Class targetClass, String fieldName)
      Get the the value of Field with the given name belonging to the provided Java Class or null if no Field was found.
      Parameters:
      targetClass - the provided Java Class the field belongs to
      fieldName - the Field name
      Returns:
      the value of Field matching the given name or null
    • getFieldValue

      public static <T> T getFieldValue(Object target, String fieldName)
      Get the value of the field matching the given name and belonging to target Object.
      Type Parameters:
      T - field type
      Parameters:
      target - target Object whose field we are retrieving the value from
      fieldName - field name
      Returns:
      field value
    • getFieldValueOrNull

      public static <T> T getFieldValueOrNull(Object target, String fieldName)
      Get the value of the field matching the given name and belonging to target Object or null if no Field was found..
      Type Parameters:
      T - field type
      Parameters:
      target - target Object whose field we are retrieving the value from
      fieldName - field name
      Returns:
      field value matching the given name or null
    • setFieldValue

      public static void setFieldValue(Object target, String fieldName, Object value)
      Set the value of the field matching the given name and belonging to target Object.
      Parameters:
      target - target object
      fieldName - field name
      value - field value
    • getMethod

      public static Method getMethod(Object target, String methodName, Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object.
      Parameters:
      target - target Object
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      return Method matching the provided signature
    • getMethodOrNull

      public static Method getMethodOrNull(Object target, String methodName, Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
      Parameters:
      target - target Object
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      return Method matching the provided signature or null
    • getMethod

      public static Method getMethod(Class targetClass, String methodName, Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Class.
      Parameters:
      targetClass - target Class
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      the Method matching the provided signature
    • getMethodOrNull

      public static Method getMethodOrNull(Class targetClass, String methodName, Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
      Parameters:
      targetClass - target Class
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      return Method matching the provided signature or null
    • getDeclaredMethodOrNull

      public static Method getDeclaredMethodOrNull(Class targetClass, String methodName, Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Class, excluding inherited ones, or null if no Method was found.
      Parameters:
      targetClass - target Class
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      return Method matching the provided signature or null
    • hasMethod

      public static boolean hasMethod(Class<?> targetClass, String methodName, Class... parameterTypes)
      Check if the provided Java Class contains a method matching the given signature (name and parameter types).
      Parameters:
      targetClass - target Class
      methodName - method name
      parameterTypes - method parameter types
      Returns:
      the provided Java Class contains a method with the given signature
    • getSetter

      public static Method getSetter(Object target, String propertyName, Class<?> parameterType)
      Get the property setter Method with the given signature (name and parameter types) belonging to the provided Java Object.
      Parameters:
      target - target Object
      propertyName - property name
      parameterType - setter property type
      Returns:
      the setter Method matching the provided signature
    • getGetter

      public static Method getGetter(Object target, String propertyName)
      Get the property getter Method with the given name belonging to the provided Java Object.
      Parameters:
      target - target Object
      propertyName - property name
      Returns:
      the getter Method matching the provided name
    • invokeMethod

      public static <T> T invokeMethod(Object target, Method method, Object... parameters)
      Invoke the provided Method on the given Java Object.
      Type Parameters:
      T - return value object type
      Parameters:
      target - target Object whose method we are invoking
      method - method to invoke
      parameters - parameters passed to the method call
      Returns:
      the value return by the Method invocation
    • invokeMethod

      public static <T> T invokeMethod(Object target, String methodName, Object... parameters)
      Invoke the method with the provided signature (name and parameter types) on the given Java Object.
      Type Parameters:
      T - return value object type
      Parameters:
      target - target Object whose method we are invoking
      methodName - method name to invoke
      parameters - parameters passed to the method call
      Returns:
      the value return by the method invocation
    • invokeGetter

      public static <T> T invokeGetter(Object target, String propertyName)
      Invoke the property getter with the provided name on the given Java Object.
      Type Parameters:
      T - return value object type
      Parameters:
      target - target Object whose property getter we are invoking
      propertyName - property name whose getter we are invoking
      Returns:
      the value return by the getter invocation
    • invokeSetter

      public static void invokeSetter(Object target, String propertyName, Object parameter)
      Invoke the property setter with the provided signature (name and parameter types) on the given Java Object.
      Parameters:
      target - target Object whose property setter we are invoking
      propertyName - property name whose setter we are invoking
      parameter - parameter passed to the setter call
    • invokeSetter

      public static void invokeSetter(Object target, String propertyName, boolean parameter)
      Invoke the boolean property setter with the provided name on the given Java Object.
      Parameters:
      target - target Object whose property setter we are invoking
      propertyName - property name whose setter we are invoking
      parameter - boolean parameter passed to the setter call
    • invokeSetter

      public static void invokeSetter(Object target, String propertyName, int parameter)
      Invoke the int property setter with the provided name on the given Java Object.
      Parameters:
      target - target Object whose property setter we are invoking
      propertyName - property name whose setter we are invoking
      parameter - int parameter passed to the setter call
    • invokeStaticMethod

      public static <T> T invokeStaticMethod(Method method, Object... parameters)
      Invoke the static Method with the provided parameters.
      Type Parameters:
      T - return value object type
      Parameters:
      method - target static Method to invoke
      parameters - parameters passed to the method call
      Returns:
      the value return by the method invocation
    • getClass

      public static <T> Class<T> getClass(String className)
      Get the Java Class with the given fully-qualified name.
      Type Parameters:
      T - Class type
      Parameters:
      className - the Java Class name to be retrieved
      Returns:
      the Java Class object
    • getClassOrNull

      public static <T> Class<T> getClassOrNull(String className)
      Get the Java Class with the given fully-qualified name or or null if no Class was found matching the provided name.
      Type Parameters:
      T - Class type
      Parameters:
      className - the Java Class name to be retrieved
      Returns:
      the Java Class object or null
    • getWrapperClass

      public static Class<?> getWrapperClass(Class<?> clazz)
      Get the Java Wrapper Class associated to the given primitive type.
      Parameters:
      clazz - primitive class
      Returns:
      the Java Wrapper Class
    • getFirstSuperClassFromPackage

      public static <T> Class<T> getFirstSuperClassFromPackage(Class clazz, String packageName)
      Get the first super class matching the provided package name.
      Type Parameters:
      T - class generic type
      Parameters:
      clazz - Java class
      packageName - package name
      Returns:
      the first super class matching the provided package name or null.
    • getGenericTypes

      public static Set<Class> getGenericTypes(ParameterizedType parameterizedType)
      Get the generic types of a given Class.
      Parameters:
      parameterizedType - parameterized Type
      Returns:
      generic types for the given Class.
    • getMemberOrNull

      public static Member getMemberOrNull(Class targetClass, String memberName)
      Get the Member with the given name belonging to the provided Java Class or null if no Member was found.
      Parameters:
      targetClass - the provided Java Class the field or method belongs to
      memberName - the Field or Method name
      Returns:
      the Field or Method matching the given name or null
    • getMemberGenericTypeOrNull

      public static Type getMemberGenericTypeOrNull(Class targetClass, String memberName)
      Get the generic Type of the Member with the given name belonging to the provided Java Class or null if no Member was found.
      Parameters:
      targetClass - the provided Java Class the field or method belongs to
      memberName - the Field or Method name
      Returns:
      the generic Type of the Field or Method matching the given name or null
    • getMemberType

      public static Class getMemberType(Member member)
      Get the Member type.
      Parameters:
      member - member
      Returns:
      member type