Class CustomComponentHelper

java.lang.Object
com.vaadin.copilot.customcomponent.CustomComponentHelper

public class CustomComponentHelper extends Object
Helper class for custom components
  • Method Details

    • generateResponse

      public static CustomComponentResponseData generateResponse(com.vaadin.flow.server.VaadinSession vaadinSession, Map<com.vaadin.flow.component.Component,ComponentTypeAndSourceLocation> allComponents, List<com.vaadin.flow.component.Component> componentsInProject)
      Generates the response that is used in the UI
      Parameters:
      allComponents - Component and source location of all components that are found in the UI
      componentsInProject - Components that have source in the project.
      Returns:
      response that contains required metadata for the custom component feature
    • isDrilledDownComponent

      public static boolean isDrilledDownComponent(com.vaadin.flow.server.VaadinSession vaadinSession, tools.jackson.databind.JsonNode data, tools.jackson.databind.JsonNode component)
      Checks if the given component is drilled down
      Parameters:
      vaadinSession - Vaadin session to access component
      data - Sent data from client. It requires ACTIVE_CLASS_JSON_KEY and ACTIVE_NODE_ID_JSON_KEY
      component - JSON data that contains uiId and nodeId
      Returns:
      true if drilled down component, false otherwise
    • isDrilledDownComponent

      public static boolean isDrilledDownComponent(com.vaadin.flow.server.VaadinSession vaadinSession, tools.jackson.databind.JsonNode data, int uiId, int nodeId)
      Checks if the given component is drilled down.
      Parameters:
      vaadinSession - Vaadin session to access component
      data - Sent data from client. It requires ACTIVE_CLASS_JSON_KEY and ACTIVE_NODE_ID_JSON_KEY
      uiId - uiId
      nodeId - nodeId of the given component
      Returns:
      true if drilled down component, false otherwise
    • getClassHierarchyOfJavaClassNameOrReactTagFromRequest

      public static List<String> getClassHierarchyOfJavaClassNameOrReactTagFromRequest(tools.jackson.databind.JsonNode data)
      Returns the hierarchy of dropped element onto a custom component. Extracts the relevant parts from the data object.
      Parameters:
      data - should contain either javaClassName or reactTag to find relevant Java Class
      Returns:
      list of
      Throws:
      CopilotException - when something goes wrong with looking for class hierarchy
    • filterMethodBasedOnDraggedComponentHierarchy

      public static List<JavaReflectionUtil.ComponentAddableMethod> filterMethodBasedOnDraggedComponentHierarchy(List<JavaReflectionUtil.ComponentAddableMethod> methods, List<String> draggedElementClassHierarchy)
      Filters the method in the custom component based on the dragged element hierarchy. e.g. when you drop an Image onto Custom Component, the listed methods should be in the hierarchy of Image. Methods that gets Button or any other component as parameter should be filtered out.
      Parameters:
      methods - List of methods that accept Component as parameter.
      draggedElementClassHierarchy - Hierarchy of dragged component. Strongly related to getClassHierarchyOfJavaClassNameOrReactTagFromRequest(JsonNode)
      Returns:
      List of filtered components
    • addInfoFromComponentSource

      public static List<CustomComponentAddMethodInfo> addInfoFromComponentSource(ComponentTypeAndSourceLocation componentTypeAndSourceLocation, List<CustomComponentAddMethodInfo> methods, ComponentSourceFinder sourceFinder) throws IOException
      Updates add/replace flags based on its usage
      Parameters:
      componentTypeAndSourceLocation - Component type and source location
      methods - Methods belonging to the given component
      Returns:
      methods enriched with source information. Methods that represent distinct containers are returned once per existing call.
      Throws:
      IOException
    • callsInJavaSource

      public static List<ExistingCall> callsInJavaSource(ComponentTypeAndSourceLocation componentTypeAndSourceLocation, List<CustomComponentAddMethodInfo> methods, ComponentSourceFinder sourceFinder) throws IOException
      Every call this component's Java source makes, among the names asked about.

      Split out so the assembly below can run for any language: it is reflection and quirks apart from this one step, which is the only part that needs a parser. #9828.

      Takes the same arguments as callsInKotlinSource(com.vaadin.copilot.rewriter.ComponentTypeAndSourceLocation, java.util.List<com.vaadin.copilot.customcomponent.CustomComponentAddMethodInfo>, com.vaadin.copilot.ComponentSourceFinder) and answers the same question, so the caller picks between them on language alone and neither has to be read to know what it wants. Raised in review of #9868.

      Parameters:
      componentTypeAndSourceLocation - the component the dialog was opened on
      methods - the candidate methods, from reflection; only calls to these are returned, which is all the assembly ever looks at
      sourceFinder - how to reach the component's siblings, needed to tell apart several components created on one line
      Returns:
      the calls, in source order
      Throws:
      IOException - if the source cannot be read
    • addInfoFromComponentInfo

      public static List<CustomComponentAddMethodInfo> addInfoFromComponentInfo(ComponentInfo componentInfo, Class<?> componentType, List<CustomComponentAddMethodInfo> methods)
      Enriches API methods from an already located component source, including preview sources.
    • callsInKotlinSource

      public static List<ExistingCall> callsInKotlinSource(ComponentTypeAndSourceLocation componentTypeAndSourceLocation, List<CustomComponentAddMethodInfo> methods, ComponentSourceFinder sourceFinder) throws IOException
      Every call this component's Kotlin source makes, among the names asked about.

      The mirror of callsInJavaSource(com.vaadin.copilot.rewriter.ComponentTypeAndSourceLocation, java.util.List<com.vaadin.copilot.customcomponent.CustomComponentAddMethodInfo>, com.vaadin.copilot.ComponentSourceFinder). Reading only, so it builds its own provider as the Java side does rather than needing the write-mode one. Without it the dialog's query went through JavaParser on a .kt file and threw, so dropping onto a component in a Kotlin view failed instead of opening the dialog. #9828.

      Parameters:
      componentTypeAndSourceLocation - the component the dialog was opened on
      methods - the candidate methods, from reflection; only calls to these are returned, because that is how the Kotlin locator is asked
      sourceFinder - how to reach the component's siblings, needed to tell apart several components created on one line
      Returns:
      the calls, grouped by the name they were asked under
      Throws:
      IOException - if the source cannot be read
    • addInfoFromComponentSource

      public static List<CustomComponentAddMethodInfo> addInfoFromComponentSource(ComponentTypeAndSourceLocation componentTypeAndSourceLocation, List<CustomComponentAddMethodInfo> methods, List<ExistingCall> existingCalls)
      As above, told the calls rather than reading them, so a non-Java source can answer for itself.
      Parameters:
      componentTypeAndSourceLocation - the component the dialog was opened on
      methods - the candidate methods, from reflection
      existingCalls - the calls its source already makes
      Returns:
      the methods, marked with what the source shows
    • extractFromRequest

      public static CustomComponentApiSelection extractFromRequest(tools.jackson.databind.JsonNode data)
      Calls extractFromRequest(JsonNode, String) with customComponentApiSelection key
      Parameters:
      data - sent from the Client
      Returns:
      Null if customComponentApiSelection is not present in the JSON or not an object or unable to parse
    • extractFromRequest

      public static CustomComponentApiSelection extractFromRequest(tools.jackson.databind.JsonNode data, String key)
      Extract CustomComponentApiSelection from the data witht the given key if present.
      Parameters:
      data - sent from the client
      key - JSON key to get the value
      Returns:
      CustomComponentApiSelection if present, or null if customComponentApiSelection is not present in the JSON or not an object or unable to parse