Class AutowireUtils

java.lang.Object
io.jmix.flowui.sys.autowire.AutowireUtils

public final class AutowireUtils extends Object
Helper class for component dependencies autowiring.
  • Method Details

    • findMethodTarget

      @Nullable public static Object findMethodTarget(com.vaadin.flow.component.Composite<?> composite, String targetId, BiFunction<com.vaadin.flow.component.Component,String,Optional<com.vaadin.flow.component.Component>> componentFinder)
      Finds the target of the annotated method in passed composite by ID.
      Parameters:
      composite - composite for search
      targetId - target ID
      componentFinder - hook to find components in the composite
      Returns:
      found target object or null if target no found
      Throws:
      IllegalStateException - if the view content is not a container
    • getViewTargetInstance

      @Nullable public static <A extends Annotation> Object getViewTargetInstance(A annotation, View<?> view, @Nullable String targetId, Target targetType)
      Finds the target for the annotated method in passed view by targetType and targetId.
      Type Parameters:
      A - type of the annotation
      Parameters:
      annotation - annotation of the method for which target will be found
      view - view for search
      targetId - target ID
      targetType - type of the target
      Returns:
      found target object or null if target not found
      Throws:
      UnsupportedOperationException - if the targetId is null and the targetType is not Target.COMPONENT, Target.CONTROLLER or Target.DATA_CONTEXT
    • getViewSubscribeTargetInstance

      @Nullable public static Object getViewSubscribeTargetInstance(View<?> view, @Nullable String targetId, Target targetType)
      Find the target fot the Subscribe annotated method in passed view by targetType and targetId.
      Parameters:
      view - view for search
      targetId - target ID
      targetType - type of the target
      Returns:
      found target object or null if target not found
      Throws:
      UnsupportedOperationException - if the targetId is null and the targetType is not Target.COMPONENT, Target.CONTROLLER or Target.DATA_CONTEXT
    • getFragmentTargetInstance

      @Nullable public static <A extends Annotation> Object getFragmentTargetInstance(A annotation, Fragment<?> fragment, @Nullable String targetId, Target targetType)
      Find the target for the annotated method is passed fragment component by targetType and targetId.
      Type Parameters:
      A - type of the annotation
      Parameters:
      annotation - annotation of the method for which target will be found
      fragment - fragment for search
      targetId - target ID
      targetType - type of the target
      Returns:
      found target object or null if target not found
      Throws:
      UnsupportedOperationException - if the target is not supported
    • createAnnotatedMethod

      @Nullable public static <A extends Annotation> ReflectionCacheManager.AnnotatedMethod<A> createAnnotatedMethod(Class<A> annotationClass, Method method)
      Creates ReflectionCacheManager.AnnotatedMethod instance based on the passed annotationClass and Method instance.
      Type Parameters:
      A - type of the annotation
      Parameters:
      annotationClass - the annotation class with which the method should be annotated
      method - the method to search for annotation
      Returns:
      ReflectionCacheManager.AnnotatedMethod instance or null if the passed method doesn't have required annotation
      Throws:
      RuntimeException - if it fails to create a method handle
    • compareMethods

      public static <A extends Annotation> int compareMethods(ReflectionCacheManager.AnnotatedMethod<A> am1, ReflectionCacheManager.AnnotatedMethod<A> am2)
      Compares two ReflectionCacheManager.AnnotatedMethod by class name, Order annotation value, and method name. Can be used as a sort comparator.
      Type Parameters:
      A - type of the annotation
      Parameters:
      am1 - first ReflectionCacheManager.AnnotatedMethod for comparison
      am2 - second ReflectionCacheManager.AnnotatedMethod for comparison
      Returns:
      the value 0 if the first annotated method is equal to the second one; a value less than 0 if the first annotated method is less by comparison logic; and a value greater than 0 if the first annotated method is greater by comparison logic
    • getOverrideHierarchy

      public static Set<Method> getOverrideHierarchy(Method method)
      Returns the override hierarchy of the passed method.
      Parameters:
      method - method to introspect its hierarchy
      Returns:
      set of overridden methods
    • getAutowiringName

      public static String getAutowiringName(ReflectionCacheManager.AutowireElement autowireElement)
      Gets the name of the field or setter method for the autowire element.
      Parameters:
      autowireElement - the element to find for a name for autowiring
      Returns:
      the name for autowiring
      Throws:
      IllegalStateException - if the search target is not a field or setter method
    • getAutowiringType

      public static Class<?> getAutowiringType(ReflectionCacheManager.AutowireElement autowireElement)
      Gets the type of the field or setter method for the autowire element.
      Parameters:
      autowireElement - the element to find for a type for autowiring
      Returns:
      the type for autowiring
      Throws:
      IllegalStateException - if the target method doesn't have only a parameter or the autowire element is not a field or setter method
    • assignValue

      public static void assignValue(AnnotatedElement element, Object value, com.vaadin.flow.component.Component component)
      Assigns the passed value to a component field or method.
      Parameters:
      element - annotated element that should be autowired, can be a field or setter method
      value - value to be set
      component - the component for which it is necessary to assign a value to the element
      Throws:
      RuntimeException - if it is not possible to assign value to a field or setter method
    • getComponentEventListener

      public static com.vaadin.flow.component.ComponentEventListener<?> getComponentEventListener(Class<?> callerClass, com.vaadin.flow.component.Component component, ReflectionCacheManager.AnnotatedMethod<Subscribe> annotatedMethod, Class<?> eventType, ReflectionCacheManager reflectionCacheManager)
      Creates a component event listener method using the factory if the component class has not been reloaded by hot-deploy, otherwise uses the MethodHandle
      Parameters:
      callerClass - caller class
      component - owner class for target component event listener method handle
      annotatedMethod - annotated method
      eventType - event class
      reflectionCacheManager - reflection cache manager to get a method factory
      Returns:
      lambda-proxy for listener
      See Also:
    • getValueChangeEventListener

      public static com.vaadin.flow.component.HasValue.ValueChangeListener<?> getValueChangeEventListener(Class<?> callerClass, com.vaadin.flow.component.Component component, ReflectionCacheManager.AnnotatedMethod<Subscribe> annotatedMethod, Class<?> eventType, ReflectionCacheManager reflectionCacheManager)
      Creates a component value change listener method using the factory if the component class has not been reloaded by hot-deploy, otherwise uses the MethodHandle
      Parameters:
      callerClass - caller class
      component - owner class for target component value change event listener method handle
      annotatedMethod - annotated method
      eventType - event class
      reflectionCacheManager - reflection cache manager to get a method factory
      Returns:
      lambda-proxy for listener
      See Also:
    • getConsumerListener

      public static Consumer<?> getConsumerListener(Class<?> callerClass, com.vaadin.flow.component.Component component, ReflectionCacheManager.AnnotatedMethod<Subscribe> annotatedMethod, Class<?> eventType, ReflectionCacheManager reflectionCacheManager)
      Creates a consumer listener method using the factory if the component class has not been reloaded by hot-deploy, otherwise uses the MethodHandle
      Parameters:
      callerClass - caller class
      component - owner class for target consumer listener method handle
      annotatedMethod - annotated method
      eventType - event class
      reflectionCacheManager - reflection cache manager to get a method factory
      Returns:
      lambda-proxy for listener
      See Also:
    • getInstallTargetSetterMethod

      public static MethodHandle getInstallTargetSetterMethod(Install annotation, com.vaadin.flow.component.Component component, Class<?> instanceClass, Method provideMethod, ReflectionCacheManager reflectionCacheManager)
      Finds a corresponding setter method for install annotation.
      Parameters:
      annotation - annotation for search
      component - origin component
      instanceClass - class to search annotation
      provideMethod - provideMethod
      reflectionCacheManager - reflection cache manager to get target install method
      Returns:
      setter method handle
    • createInstallHandler

      public static Object createInstallHandler(Class<?> callerClass, Class<?> targetObjectType, com.vaadin.flow.component.Component component, Method method)
      Created proxy instance for install method handler.
      Parameters:
      callerClass - caller class
      targetObjectType - target object type
      component - component to be passed
      method - method to be invoked
      Returns:
      proxy instance for install handler