Class JavaReflectionUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents a method in a component class that can be used to add child components.static final recordstatic final record -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanChecks whether a class with the specified fully qualified name exists and is loadable.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.static Class<?> Finds the class for the given source type.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 tojava.lang.Object.static StringgetClassName(Class<?> aClass) static <E extends Enum<E>>
EgetEnumConstant(Class<?> enumClass, String name) Returns the enum constant of the specified enum type with the specified name.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.getFieldValue(Object targetInstance, String fieldName) Retrieves the value of a specified field from the given target object.getMethodInvocationResult(Object object, Method getterMethod) Calls the method of given component instance to obtain result valuegetParameterTypes(Class<?> clazz, String methodName) Finds the first method matches with givenmethodNamein the class and invokesgetParameterTypes(Method, Class)getParameterTypes(Method method, Class<?> cls) static JavaReflectionUtil.TypeInfogetReturnType(Method method, Class<?> cls) getStaticFieldValue(String qualifiedClassName, String fieldName) Gets static value of a field from given class name.static booleanChecks whether the specified class declares a field with the given name.static <T> TinstantiateClass(Class<?> clazz) Instantiates a new object of the given class using its no-argument constructor.static booleanisArrayArgument(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.static booleanisParamFlowComponentType(Parameter parameter) Checks whether the given method parameter is (or extends) aComponenttype, either directly or through inheritance.
-
Constructor Details
-
JavaReflectionUtil
public JavaReflectionUtil()
-
-
Method Details
-
getParameterTypes
public static List<JavaReflectionUtil.ParameterTypeInfo> getParameterTypes(Class<?> clazz, String methodName) Finds the first method matches with givenmethodNamein the class and invokesgetParameterTypes(Method, Class)- Parameters:
clazz- Class that has the methodmethodName- 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
-
getClassName
-
isParamFlowComponentType
Checks whether the given method parameter is (or extends) aComponenttype, 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:
trueif the parameter type or its component type is a subclass ofComponent,falseotherwise
-
getMethodInvocationResult
Calls the method of given component instance to obtain result value- Parameters:
object- Component that method will be executedgetterMethod- Method that returns a value- Returns:
Optional.empty()if no value is returned or an exception is thrown, returns the value otherwise
-
getClass
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
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 nameargumentIndex- argument to look up. It can exceed the method parameters, in that case the last argument is taken into account- Returns:
trueif argument is an array,falseotherwise
-
getClassHierarchy
Returns a list of fully qualified class names representing the class hierarchy for the specified class name, starting from the given class up tojava.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 bygetClass(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 emptyOptionalis returned.- Type Parameters:
T- the type of the class- Parameters:
clazz- the class to start the field lookup from (can be a superclass oftarget)instance- the instance from which to retrieve the field valuefieldName- the name of the field to retrieve- Returns:
- an
Optionalcontaining the field value if found and accessible; otherwise, an emptyOptional
-
getFieldValue
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.
-
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
NoSuchMethodExceptionwill be thrown. The returned object is cast to the expected typeT, 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- theClassobject 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 constructorInvocationTargetException- if the constructor throws an exceptionInstantiationException- if the class represents an abstract classIllegalAccessException- if the constructor is not accessibleClassCastException- if the created instance cannot be cast to typeT
-
hasField
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 returnstrue. If the field is not found, it returnsfalse.- Parameters:
clazz- theClassobject to inspectfieldName- the name of the field to check for- Returns:
trueif the field is declared in the class;falseotherwise- Throws:
NullPointerException- ifclazzorfieldNameisnull
-
exists
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 returnstrue. If any exception occurs during the loading process (such asClassNotFoundException), the method returnsfalse.- Parameters:
className- the fully qualified name of the class to check- Returns:
trueif the class exists and can be loaded;falseotherwise
-
getStaticFieldValue
Gets static value of a field from given class name. Method returnsOptional.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.MyClassfieldName- field name- Returns:
- the object value
-
getEnumConstant
Returns the enum constant of the specified enum type with the specified name.- Type Parameters:
E- the type of the enum- Parameters:
enumClass- theClassobject of the enum type from which to return a constantname- 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 ofJavaReflectionUtil.ComponentAddableMethodinstances 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.ComponentAddableMethodrecords representing valid add methods.
-