Class ReflectionHelper

java.lang.Object
io.jmix.core.common.util.ReflectionHelper

public final class ReflectionHelper extends Object
Utility class to simplify work with Java reflection.
  • Method Details

    • loadClass

      public static Class<?> loadClass(String name) throws ClassNotFoundException
      Load class by name.
      Parameters:
      name - class FQN or primitive type name
      Returns:
      class instance
      Throws:
      ClassNotFoundException - if not found
    • getClass

      public static <T> Class<T> getClass(String name)
      Load class by name, wrapping a ClassNotFoundException into unchecked exception.
      Parameters:
      name - class FQN
      Returns:
      class instance
    • newInstance

      public static <T> T newInstance(Class<T> cls, Object... params) throws NoSuchMethodException
      Instantiates an object by appropriate constructor.
      Parameters:
      cls - class
      params - constructor arguments
      Returns:
      created object instance
      Throws:
      NoSuchMethodException - if the class has no constructor matching the given arguments
    • findMethod

      @Nullable public static Method findMethod(Class<?> c, String name, Object... params)
      Searches for a method by its name and arguments.
      Parameters:
      c - class
      name - method name
      params - method arguments
      Returns:
      method reference or null if a suitable method not found
    • invokeMethod

      public static <T> T invokeMethod(Object obj, String name, Object... params) throws NoSuchMethodException
      Invokes a method by reflection.
      Parameters:
      obj - object instance
      name - method name
      params - method arguments
      Returns:
      method result
      Throws:
      NoSuchMethodException - if a suitable method not found
    • getParamTypes

      public static Class[] getParamTypes(Object... params)
      Constructs an array of argument types from an array of actual values. Values can not contain nulls.
      Parameters:
      params - arguments
      Returns:
      the array of argument types
    • getFieldValue

      public static Object getFieldValue(Object entity, String property)
      Useful to get real entity property value and avoid unfetched attribute exception.
      Returns:
      field value