You're viewing an older version of Vaadin JavaDoc. Please see version 24.7.0 for the latest.
com.vaadin.copilot.javarewriter.

Class JavaRewriter

java.lang.Object
com.vaadin.copilot.javarewriter.JavaRewriter
public class JavaRewriter extends Object

Rewrites Java source code to add or replace constructor parameters, method invocations and more.

  • Field Details

    • compilationUnit

      protected com.github.javaparser.ast.CompilationUnit compilationUnit
  • Constructor Details

    • JavaRewriter

      public JavaRewriter(String source)

      Creates a new JavaRewriter instance.

      Parameters:

      source - the Java source code to rewrite

    • JavaRewriter

      public JavaRewriter(String source, boolean solveJdkClassesBySimpleName)

      Creates a new JavaRewriter instance.

      Parameters:

      source - the Java source code to rewrite

      solveJdkClassesBySimpleName - true to try to resolve unqualified types using a set of standard JDK packages?

  • Method Details

    • getCompilationUnit

      public com.github.javaparser.ast.CompilationUnit getCompilationUnit()
    • getResult

      public String getResult()
    • getFirstModifiedRow

      public int getFirstModifiedRow()
    • replaceFunctionCall

      public JavaRewriter.ReplaceResult replaceFunctionCall(ComponentInfo componentInfo, String function, Object value)

      Replaces a constructor parameter (if it is mapped to the given setter function) or a function call in the source code.

      Parameters:

      componentInfo - the component to modify

      function - the name of the function to replace or add, if the constructor parameter is not found

      value - the new value for the constructor parameter or function call

      Returns:

      the new variable name if the variable name was auto-generated and changed, otherwise null

    • addImport

      public void addImport(String qualifiedName, boolean isStatic, boolean isAsterisk)

      Adds the given import if it is not already imported.

      Parameters:

      qualifiedName - the qualified name of the import

      isStatic - whether the import is static

      isAsterisk - whether the import end with ".*"

    • addCall

      public void addCall(ComponentInfo componentInfo, String function, Object... parameters)

      Adds a function call to the source code.

      Parameters:

      componentInfo - the component to modify

      function - the name of the function to add

      parameters - parameters for the function

    • replaceOrAddCall

      public void replaceOrAddCall(ComponentInfo componentInfo, String function, Object... parameters)

      Replaces a function call in the source code, if found, otherwise adds the function call.

      Parameters:

      componentInfo - the component to modify

      function - the name of the function call to add or replace

      parameters - new parameters for the function

    • getPropertyValue

      public Object getPropertyValue(ComponentInfo componentInfo, String property)

      Gets the (active) value of a property of a component.

      The property value is determined by looking for a setter method call in the source code. If the property is not set using a setter, the constructor is checked.

      If the property is not set using a setter or in the constructor, null is returned.

      If the property is set using a method call, the method call expression is returned.

      Parameters:

      componentInfo - the component to get the property value from

      property - the property name

      Returns:

      the property value, or null if the property is not set

    • getStyles

      public List<JavaStyleRewriter.StyleInfo> getStyles(ComponentInfo componentInfo)

      Gets the (active) styles of a component.

      Parameters:

      componentInfo - the component to get the styles of

      Returns:

      the styles, as a list of style names and values

    • setStyle

      public void setStyle(ComponentInfo component, String property, String value)

      Sets the given inline style on the given component, replacing an existing style property if present.

      Parameters:

      component - the component to set the style on

      property - the style property to set

      value - the style value to set or null to remove the style

    • setSizing

      public void setSizing(ComponentInfo componentInfo, Map<String,String> changes)

      Sets sizing properties of the given component using the Style API. Replaces existing ones and removes the keys which have null values.
      For removal, it also looks for Component API to remove such button.setWidth("") calls.

      Parameters:

      componentInfo - the component to set style

      changes - changes applied. Having null value for a key means removal, otherwise update/add applies.

    • findComponentInfo

      public ComponentInfo findComponentInfo(ComponentTypeAndSourceLocation typeAndSourceLocation)

      Finds a component in the source code.

      Note that this will create a constructor if the component is a route class and there are no constructors.

      Parameters:

      typeAndSourceLocation - the type and source location of the component

      Returns:

      the component info

    • delete

      public boolean delete(ComponentInfo componentInfo)

      Deletes a component from the source code.

      Parameters:

      componentInfo - the component to delete

      Returns:

      true if the deletion was successful, false otherwise

    • moveComponent

      public void moveComponent(ComponentInfo component, ComponentInfo container, ComponentInfo reference, JavaRewriter.Where where)

      Moves a component in the source code.

      Parameters:

      component - the component to move

      container - the new container for the component, if where is Where.APPEND.

      reference - the reference component to move the component before, if where is Where.BEFORE.

      where - where to move the component

    • duplicate

      public void duplicate(ComponentInfo component)
    • duplicate

      public JavaRewriter.DuplicateInfo duplicate(ComponentInfo component, boolean handleAdd)

      Duplicates a component in the source code.

      Parameters:

      component - the component to duplicate

      handleAdd - true to automatically add the new component next to the old one, false to handle add calls like any other method call

      Returns:

      a map from old component name to new component name

    • addComponentUsingTemplate

      public void addComponentUsingTemplate(ComponentInfo referenceComponent, JavaRewriter.Where where, List<JavaComponent> template, JavaRewriter.AddTemplateOptions options)

      Adds the given code snippet to the source code either before the reference component (Where.BEFORE) or by appending to the layout (Where.APPEND).

      Parameters:

      referenceComponent - the reference component (BEFORE) or container (APPEND) to add the code

      where - where to add the code

      template - the code to add, as JSON array of objects with "tag", "props" and "children"

      options - options that control how the template is added

    • findInsertionPointForAppend

      public InsertionPoint findInsertionPointForAppend(ComponentInfo component, JavaRewriter.Where where)
    • setAlignment

      public void setAlignment(ComponentInfo component, String alignItemsClassName, String justifyContentClassName)
    • setGap

      public void setGap(ComponentInfo component, String lumoClassAll, String lumoClassColumn, String lumoClassRow)

      Sets gap to selected component

      Parameters:

      component - component to set gap

      lumoClassAll - gap all value. e.g. gap-m. might be null

      lumoClassColumn - gap column value which starts with gap-x prefix, e.g. gap-x-xs. might be null

      lumoClassRow - gap row value which starts with gap-y prefix, e.g. gap-y-xs. might be null

    • setPadding

      public void setPadding(ComponentInfo component, String all, String top, String right, String bottom, String left)
    • mergeAndReplace

      public void mergeAndReplace(List<ComponentInfo> components, JavaComponent wrapperComponent)

      Merges all the components and wraps them using the given component and places the result in place of the first component.

      Parameters:

      components - The components to merge. The first component will be replaced with the wrapper component

      wrapperComponent - The component to wrap the merged components in.

    • replaceCallParameter

      public void replaceCallParameter(com.github.javaparser.ast.nodeTypes.NodeWithArguments<?> call, String oldVariableName, String newVariableName)

      Replaces a parameter name in a method call.

      Parameters:

      call -

      oldVariableName -

      newVariableName -

    • createComponentStatements

      public void createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, List<JavaComponent> template, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options)
    • createComponentStatements

      public List<com.github.javaparser.ast.body.VariableDeclarator> createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, JavaComponent maybeJavaComponent, boolean attach, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options)
    • attachComponent

      public void attachComponent(InsertionPoint insertionPoint, JavaComponent component, JavaComponent parent, String layoutVariableName, ComponentInfo referenceComponent, com.github.javaparser.ast.expr.Expression variableNameExpr, String variableName)
    • getParameterList

      public com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> getParameterList(InsertionPoint insertionPoint, Object value)
    • insertSetter

      public void insertSetter(InsertionPoint insertionPoint, com.github.javaparser.ast.expr.Expression owner, String setterName, Object value, com.github.javaparser.ast.CompilationUnit compilationUnit, JavaComponent javaComponent)

      Insert setters for given java component. TODO move this class into CustomComponentHandle and remove it from here

      Parameters:

      insertionPoint - Insertion point to add setters

      owner - owner of the setters

      setterName - setterName from JavaComponent props

      value - setterValue from JavaComponent props

      compilationUnit - Compilation unit of the file

      javaComponent - Java component itself

    • getSource

      protected String getSource()

      Returns the source code.

      Returns:

      the source code

    • addComment

      public void addComment(ComponentInfo componentInfo, String comment, JavaRewriter.CommentType commentType)

      Adds a comment to the specified component in the source code.

      Parameters:

      componentInfo - the component to which the comment should be added

      comment - the comment to add

      commentType - the type of comment (LINE, BLOCK, JAVADOC)