Package com.vaadin.copilot.javarewriter
Class JavaRewriter
java.lang.Object
com.vaadin.copilot.javarewriter.JavaRewriter
Rewrites Java source code to add or replace constructor parameters, method
invocations and more.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic final recordA code snippet to be inserted into the source code.static final recordstatic final recordInformation about a component in the source code.static final recordInformation about extracting an inline variable to local variablestatic classRepresents a point in the source code where new code can be inserted.static final recordRepresents a Java component to be added to the source code.static final recordHolder for a setter name and associated valuestatic enumWhere to add a component -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.github.javaparser.ast.CompilationUnit -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddCall(JavaRewriter.ComponentInfo componentInfo, String function, Object... parameters) Adds a function call to the source code.voidaddComponentUsingTemplate(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).booleandelete(JavaRewriter.ComponentInfo componentInfo) Deletes a component from the source code.voidduplicate(JavaRewriter.ComponentInfo component) Duplicates a component in the source code.findComponentInfo(JavaRewriter.ComponentTypeAndSourceLocation typeAndSourceLocation) Finds a component in the source code.intgetPropertyValue(JavaRewriter.ComponentInfo componentInfo, String property) Gets the (active) value of a property of a component.protected StringReturns the source code.voidmergeAndReplace(List<JavaRewriter.ComponentInfo> components, JavaRewriter.JavaComponent wrapperComponent) Merges all the components and wraps them using the given component and places the result in place of the first component.voidmoveComponent(JavaRewriter.ComponentInfo component, JavaRewriter.ComponentInfo container, JavaRewriter.ComponentInfo reference, JavaRewriter.Where where) Moves a component in the source code.booleanreplaceFunctionCall(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.booleanreplaceOrAddCall(JavaRewriter.ComponentInfo componentInfo, String function, Object... parameters) Replaces a function call in the source code, if found, otherwise adds the function call.voidsetAlignment(JavaRewriter.ComponentInfo component, JavaRewriter.AlignmentMode alignmentMode, boolean selected, List<String> lumoClasses) voidsetGap(JavaRewriter.ComponentInfo component, String newValue) Sets gap to selected component
-
Field Details
-
compilationUnit
protected com.github.javaparser.ast.CompilationUnit compilationUnit
-
-
Constructor Details
-
JavaRewriter
Creates a new JavaRewriter instance.- Parameters:
source- the Java source code to rewrite
-
-
Method Details
-
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 modifyfunction- the name of the function to replace or add, if the constructor parameter is not foundvalue- the new value for the constructor parameter or function call- Returns:
trueif the replacement was successful,falseotherwise
-
addCall
public boolean addCall(JavaRewriter.ComponentInfo componentInfo, String function, Object... parameters) Adds a function call to the source code.- Parameters:
componentInfo- the component to modifyfunction- the name of the function to addparameters- parameters for the function- Returns:
trueif the addition was successful,falseotherwise
-
replaceOrAddCall
public boolean replaceOrAddCall(JavaRewriter.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 modifyfunction- the name of the function call to add or replaceparameters- new parameters for the function- Returns:
trueif the replacement was successful,falseotherwise
-
getPropertyValue
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,
nullis 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 fromproperty- the property name- Returns:
- the property value, or null if the property is not set
-
findComponentInfo
public JavaRewriter.ComponentInfo findComponentInfo(JavaRewriter.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
Deletes a component from the source code.- Parameters:
componentInfo- the component to delete- Returns:
trueif the deletion was successful,falseotherwise
-
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 movecontainer- 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
Duplicates a component in the source code.- Parameters:
component- the component to duplicate
-
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 layoutlayout- the layout to append the code towhere- where to add the codetemplate- 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) -
setGap
Sets gap to selected component- Parameters:
component- component to set gapnewValue- lumo utility gap variable literal.
-
mergeAndReplace
public void mergeAndReplace(List<JavaRewriter.ComponentInfo> components, JavaRewriter.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 componentwrapperComponent- The component to wrap the merged components in.
-
getSource
Returns the source code.- Returns:
- the source code
-