Class JavaReflectionUtil

java.lang.Object
com.vaadin.copilot.JavaReflectionUtil

public class JavaReflectionUtil extends Object
  • Constructor Details

    • JavaReflectionUtil

      public JavaReflectionUtil()
  • Method Details

    • getParameterTypes

      public static List<JavaReflectionUtil.ParameterTypeInfo> getParameterTypes(Class<?> clazz, String methodName)
      Finds the first method matches with given methodName in the class and invokes getParameterTypes(Method, Class)
      Parameters:
      clazz - Class that has the method
      methodName - Method to be found in the class
      Returns:
      the list of parameters
      Throws:
      CopilotException - is thrown when method cannot be found in the class.
    • getParameterTypes

      public static List<JavaReflectionUtil.ParameterTypeInfo> getParameterTypes(Method method, Class<?> cls)
    • getReturnType

      public static JavaReflectionUtil.TypeInfo getReturnType(Method method, Class<?> cls)
    • getClassName

      public static String getClassName(Class<?> aClass)
    • isParamFlowComponentType

      public static boolean isParamFlowComponentType(Parameter parameter)
      Checks whether the given method parameter is (or extends) a Component type, either directly or through inheritance. If the parameter is an array, the component type of the array is used.

      This method walks up the class hierarchy to determine if the parameter type (or its component type in case of an array) is a subclass of Component.

      Parameters:
      parameter - the method parameter to inspect
      Returns:
      true if the parameter type or its component type is a subclass of Component, false otherwise
    • getMethodInvocationResult

      public static Optional<Object> getMethodInvocationResult(Object object, Method getterMethod)
      Calls the method of given component instance to obtain result value
      Parameters:
      object - Component that method will be executed
      getterMethod - Method that returns a value
      Returns:
      Optional.empty() if no value is returned or an exception is thrown, returns the value otherwise
    • getClass

      public static Class<?> getClass(String name) throws IllegalArgumentException
      Finds the class for the given source type.
      Parameters:
      name - the class name
      Returns:
      the class for the given name
      Throws:
      IllegalArgumentException - if the class is not found
    • isArrayArgument

      public static boolean isArrayArgument(String className, String methodName, int argumentIndex)
      Uses reflection API for finding class name, method and the argument to find whether argument is array.
      Includes the methods from extended Composite class if it is extended.
      Parameters:
      className - full class name of a class.
      methodName - method name
      argumentIndex - argument to look up. It can exceed the method parameters, in that case the last argument is taken into account
      Returns:
      true if argument is an array, false otherwise
    • getClassHierarchy

      public static List<String> getClassHierarchy(String className)
      Returns a list of fully qualified class names representing the class hierarchy for the specified class name, starting from the given class up to java.lang.Object.

      This method attempts to load the class using getClass(String) and then traverses its superclasses, collecting their fully qualified names into a list.

      Parameters:
      className - the fully qualified name of the class whose hierarchy is to be retrieved
      Returns:
      a list of fully qualified class names in the hierarchy, starting with the specified class
      Throws:
      RuntimeException - if the class cannot be found by getClass(String)
    • getFieldValue

      public static <T> Optional<Object> getFieldValue(Class<? extends T> clazz, T instance, String fieldName)
      Retrieves the value of a specified field from the given target object by searching through the class hierarchy of the provided class.

      This method attempts to find the field with the specified name in the given class or any of its superclasses. If found, it makes the field accessible and returns its value wrapped in an Optional. If the field is not found or cannot be accessed, an empty Optional is returned.

      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the class to start the field lookup from (can be a superclass of target)
      instance - the instance from which to retrieve the field value
      fieldName - the name of the field to retrieve
      Returns:
      an Optional containing the field value if found and accessible; otherwise, an empty Optional
    • getFieldValue

      public static Optional<Object> getFieldValue(Object targetInstance, String fieldName)
      Retrieves the value of a specified field from the given target object.

      This method searches for the specified field in the class hierarchy of the target object. If the field is found, it is made accessible and its value is retrieved.

      Parameters:
      targetInstance - The object from which the field value is retrieved.
      fieldName - The name of the field whose value is to be accessed.
      Returns:
      An Optional containing the field value if found; otherwise, an empty Optional.
    • instantiateClass

      public static <T> T instantiateClass(Class<?> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
      Instantiates a new object of the given class using its no-argument constructor.

      This method uses reflection to create a new instance of the specified class. The class must have a public no-argument constructor, otherwise a NoSuchMethodException will be thrown. The returned object is cast to the expected type T, so callers should ensure that the class type matches the expected return type.

      Type Parameters:
      T - the type of the object to return
      Parameters:
      clazz - the Class object representing the class to instantiate
      Returns:
      a new instance of the specified class
      Throws:
      NoSuchMethodException - if the class does not have a public no-argument constructor
      InvocationTargetException - if the constructor throws an exception
      InstantiationException - if the class represents an abstract class
      IllegalAccessException - if the constructor is not accessible
      ClassCastException - if the created instance cannot be cast to type T
    • hasField

      public static boolean hasField(Class<?> clazz, String fieldName)
      Checks whether the specified class declares a field with the given name.

      This method attempts to retrieve the field using clazz.getDeclaredField(fieldName), which only checks for fields declared directly in the class (not inherited fields). If the field exists, the method returns true. If the field is not found, it returns false.

      Parameters:
      clazz - the Class object to inspect
      fieldName - the name of the field to check for
      Returns:
      true if the field is declared in the class; false otherwise
      Throws:
      NullPointerException - if clazz or fieldName is null
    • exists

      public static boolean exists(String className)
      Checks whether a class with the specified fully qualified name exists and is loadable.

      This method attempts to load the class using getClass(String). If the class can be successfully loaded, the method returns true. If any exception occurs during the loading process (such as ClassNotFoundException), the method returns false.

      Parameters:
      className - the fully qualified name of the class to check
      Returns:
      true if the class exists and can be loaded; false otherwise
    • getStaticFieldValue

      public static Optional<Object> getStaticFieldValue(String qualifiedClassName, String fieldName)
      Gets static value of a field from given class name. Method returns Optional.empty() if
      • Class name is null
      • Class not found
      • Field not found in class
      • Field is not static
      • Illegal access happens
      Parameters:
      qualifiedClassName - full class name e.g. com.vaadin.classes.MyClass
      fieldName - field name
      Returns:
      the object value
    • getEnumConstant

      public static <E extends Enum<E>> E getEnumConstant(Class<?> enumClass, String name)
      Returns the enum constant of the specified enum type with the specified name.
      Type Parameters:
      E - the type of the enum
      Parameters:
      enumClass - the Class object of the enum type from which to return a constant
      name - the name of the enum constant to be returned
      Returns:
      the enum constant of the specified enum type with the specified name
      Throws:
      IllegalArgumentException - if the specified class object does not represent an enum type, or if no constant with the specified name is found
    • getChildAddableMethods

      public static List<JavaReflectionUtil.ComponentAddableMethod> getChildAddableMethods(Class<?> clazz)
      Scans the public methods of the given class and identifies those that can be used to add child components, based on a predefined predicate.

      The method filters class methods using checkMethodIsAddMethod(Method), and constructs a list of JavaReflectionUtil.ComponentAddableMethod instances that capture the essential metadata for each matching method.

      Parameters:
      clazz - the class to inspect for addable child component methods.
      Returns:
      a list of JavaReflectionUtil.ComponentAddableMethod records representing valid add methods.