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.
  • Constructor Details

    • JavaRewriter

      public JavaRewriter(String source)
      Creates a new JavaRewriter instance.
      Parameters:
      source - the Java source code to rewrite
  • Method Details

    • getResult

      public String getResult()
    • getFirstModifiedRow

      public int getFirstModifiedRow()
    • replaceFunctionCall

      public boolean replaceFunctionCall(JavaRewriter.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:
      true if the replacement was successful, false otherwise
    • addCall

      public boolean addCall(JavaRewriter.ComponentInfo componentInfo, String function, Object parameter)
      Adds a function call to the source code.
      Parameters:
      componentInfo - the component to modify
      function - the name of the function to add
      parameter - the parameter for the function
      Returns:
      true if the addition was successful, false otherwise
    • replaceOrAddCall

      public boolean replaceOrAddCall(JavaRewriter.ComponentInfo componentInfo, String function, Object parameter)
      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
      parameter - the new parameter for the function
      Returns:
      true if the replacement was successful, false otherwise
    • getPropertyValue

      public Object getPropertyValue(JavaRewriter.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
    • findComponentInfo

      public JavaRewriter.ComponentInfo findComponentInfo(JavaRewriter.ComponentTypeAndSourceLocation typeAndSourceLocation)
    • delete

      public boolean delete(JavaRewriter.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(JavaRewriter.ComponentInfo component, JavaRewriter.ComponentInfo container, JavaRewriter.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
    • addComponentUsingTemplate

      public void addComponentUsingTemplate(JavaRewriter.ComponentInfo referenceComponent, JavaRewriter.ComponentInfo layout, JavaRewriter.Where where, List<JavaRewriter.JavaComponent> template)
      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 to add the code before, or null if the code should be appended to the layout
      layout - the layout to append the code to
      where - where to add the code
      template - the code to add, as JSON array of objects with "tag", "props" and "children"
    • setAlignment

      public void setAlignment(JavaRewriter.ComponentInfo component, JavaRewriter.AlignmentMode alignmentMode, boolean selected, List<String> lumoClasses) throws IllegalAccessException
      Throws:
      IllegalAccessException
    • getSource

      protected String getSource()
      Returns the source code.
      Returns:
      the source code