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 recordstatic enumstatic final recordA code snippet to be inserted into the source code.static enumEnum representing the type of comment to be added.static final recordThe result of a duplicate operationstatic final recordInformation about extracting an inline variable to local variablestatic final recordInformation about a renamed variable.static final recordHolder for a setter name and associated valuestatic enumWhere to add a component -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCall(ComponentInfo componentInfo, String function, Object... parameters) Adds a function call to the source code.voidaddComment(ComponentInfo componentInfo, String comment, JavaRewriter.CommentType commentType) Adds a comment to the specified component in the source code.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).addConstructorParameterToClass(ComponentInfo referenceComponent, String parameterTypeName, String suggestedParameterName, boolean storeAsField) Adds a parameter of the given type to the constructor for the class containing the given component.attachComponent(InsertionPoint insertionPoint, JavaComponent component, JavaComponent parent, String layoutVariableName, ComponentInfo referenceComponent, com.github.javaparser.ast.expr.Expression variableNameExpr, String variableName, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where) createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, JavaComponent maybeJavaComponent, boolean attach, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where, JavaSource javaSource) createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, List<JavaComponent> template, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where, JavaSource javaSource) booleandelete(ComponentInfo componentInfo) Deletes a component from the source code.voidduplicate(ComponentInfo component) duplicate(ComponentInfo component, boolean handleAdd) Duplicates a component in the source code.voidextractComponent(ComponentInfo component, IdentityHashMap<ComponentInfo, List<ComponentInfo>> componentChildren, String extractedComponentClassNameSuggestion) Extracts the given component, including its children, to a new component.findInsertionPointForAppend(ComponentInfo component, JavaRewriter.Where where) com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> getParameterList(InsertionPoint insertionPoint, Object value, JavaSource javaSource) getPropertyValue(ComponentInfo componentInfo, String property) Gets the (active) value of a property of a component.getStyles(ComponentInfo componentInfo) Gets the (active) styles of a component.voidimportLitTemplate(JavaSource litTemplateSource, List<JavaComponent> componentDefinitions) Imports the given components that come from a lit template into the given LitTemplate Java class.voidinsertSetter(InsertionPoint insertionPoint, com.github.javaparser.ast.expr.Expression owner, String setterName, Object value, JavaComponent javaComponent, JavaSource javaSource) Insert setters for given java component.voidmergeAndReplace(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.voidmoveComponent(ComponentInfo component, ComponentInfo container, ComponentInfo reference, JavaRewriter.Where where) Moves a component in the source code.voidremoveCalls(ComponentInfo componentInfo, String function) Removes all calls to the given function from the source code.voidremovePropertySetter(ComponentInfo componentInfo, String setter) Removes all property set calls or references in constructors initializationsvoidreplaceCallParameter(com.github.javaparser.ast.nodeTypes.NodeWithArguments<?> call, String oldVariableName, String newVariableName) Replaces a parameter name in a method call.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.voidreplaceOrAddCall(ComponentInfo componentInfo, String function, Object... parameters) Replaces a function call in the source code, if found, otherwise adds the function call.voidsetAlignment(ComponentInfo component, String alignItemsClassName, String justifyContentClassName) voidsetComboBoxDataSource(ComponentInfo comboBox, String serviceClassName, String serviceMethodName, List<JavaReflectionUtil.ParameterTypeInfo> parameterTypes, String itemTypeClassName, UIServiceCreator.FieldInfo displayProperty) Connects the given combo box to the given service method, using the given item type and order for the columns.voidsetGap(ComponentInfo component, String lumoClassAll, String lumoClassColumn, String lumoClassRow) Sets gap to selected componentvoidsetGridDataSource(ComponentInfo grid, String serviceClassName, String serviceMethodName, List<JavaReflectionUtil.ParameterTypeInfo> parameterTypes, String itemTypeClassName, List<UIServiceCreator.FieldInfo> itemPropertiesOrder) Connects the given grid to the given service method, using the given item type and order for the columns.voidsetPadding(ComponentInfo component, String all, String top, String right, String bottom, String left) voidsetSizing(ComponentInfo componentInfo, Map<String, String> changes) Sets sizing properties of the given component using the Style API.voidsetStyle(ComponentInfo component, String property, String value) Sets the given inline style on the given component, replacing an existing style property if present.
-
Constructor Details
-
JavaRewriter
public JavaRewriter()
-
-
Method Details
-
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 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, ornullto remove the function call- Returns:
- the new variable name if the variable name was auto-generated and changed, otherwise null
-
addConstructorParameterToClass
public String addConstructorParameterToClass(ComponentInfo referenceComponent, String parameterTypeName, String suggestedParameterName, boolean storeAsField) Adds a parameter of the given type to the constructor for the class containing the given component.Does not add another parameter, if a parameter of the given type is already present.
Optionally creates a new field for the parameter and copy the value to the field.
- Parameters:
referenceComponent- a component instance created inside the class - used to find the class and its constructorparameterTypeName- the type of parametersuggestedParameterName- a suggested name for the parameterstoreAsField- true to create a field and copy the parameter value there, false to add the parameter to the constructor only- Returns:
- the name of the added field, if storeAsField is true, otherwise the name of the added parameter
-
addCall
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
-
replaceOrAddCall
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
-
removePropertySetter
Removes all property set calls or references in constructors initializations- Parameters:
componentInfo- the component to modifysetter- the name of the property setter.
-
removeCalls
Removes all calls to the given function from the source code.- Parameters:
componentInfo- the component to modifyfunction- the name of the function to remove parameter is not found
-
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
-
getStyles
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
Sets the given inline style on the given component, replacing an existing style property if present.- Parameters:
component- the component to set the style onproperty- the style property to setvalue- the style value to set or null to remove the style
-
setSizing
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 suchbutton.setWidth("")calls.- Parameters:
componentInfo- the component to set stylechanges- changes applied. Having null value for a key means removal, otherwise update/add applies.
-
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(ComponentInfo component, ComponentInfo container, 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
-
duplicate
Duplicates a component in the source code.- Parameters:
component- the component to duplicatehandleAdd- 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 List<ComponentInfo> 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 codewhere- where to add the codetemplate- the code to add, as JSON array of objects with "tag", "props" and "children"options- options that control how the template is added- Returns:
- the created components
-
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 gaplumoClassAll- gap all value. e.g. gap-m. might benulllumoClassColumn- gap column value which starts with gap-x prefix, e.g. gap-x-xs. might benulllumoClassRow- gap row value which starts with gap-y prefix, e.g. gap-y-xs. might benull
-
setPadding
-
mergeAndReplace
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.
-
extractComponent
public void extractComponent(ComponentInfo component, IdentityHashMap<ComponentInfo, List<ComponentInfo>> componentChildren, String extractedComponentClassNameSuggestion) Extracts the given component, including its children, to a new component.- Parameters:
component- The component to extract.componentChildren- Children info for the component and its contents. This is an identity hash map to ensure that the component info is found even after the AST has been modified. A record in Java uses the data for hashcode/equals so they can change when e.g. a block changes, which would make a lookup in a normal hash map to fail.extractedComponentClassNameSuggestion- A suggestion for the name of the new component. Will be modified if it is already in use.
-
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-
-
importLitTemplate
public void importLitTemplate(JavaSource litTemplateSource, List<JavaComponent> componentDefinitions) Imports the given components that come from a lit template into the given LitTemplate Java class.- Parameters:
litTemplateSource- the source of the LitTemplate classcomponentDefinitions- component definitions extracted from the corresponding lit template typescript file
-
createComponentStatements
public List<ComponentInfo> createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, List<JavaComponent> template, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where, JavaSource javaSource) -
createComponentStatements
public ComponentInfo createComponentStatements(InsertionPoint insertionPoint, JavaComponent parent, JavaComponent maybeJavaComponent, boolean attach, String layoutVariableName, ComponentInfo referenceComponent, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where, JavaSource javaSource) -
attachComponent
public AttachExpression attachComponent(InsertionPoint insertionPoint, JavaComponent component, JavaComponent parent, String layoutVariableName, ComponentInfo referenceComponent, com.github.javaparser.ast.expr.Expression variableNameExpr, String variableName, JavaRewriter.AddTemplateOptions options, JavaRewriter.Where where) -
getParameterList
public com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> getParameterList(InsertionPoint insertionPoint, Object value, JavaSource javaSource) -
insertSetter
public void insertSetter(InsertionPoint insertionPoint, com.github.javaparser.ast.expr.Expression owner, String setterName, Object value, JavaComponent javaComponent, JavaSource javaSource) Insert setters for given java component. TODO move this class intoCustomComponentHandleand remove it from here- Parameters:
insertionPoint- Insertion point to add settersowner- owner of the setterssetterName- setterName from JavaComponent propsvalue- setterValue from JavaComponent propsjavaComponent- Java component itselfjavaSource-
-
setGridDataSource
public void setGridDataSource(ComponentInfo grid, String serviceClassName, String serviceMethodName, List<JavaReflectionUtil.ParameterTypeInfo> parameterTypes, String itemTypeClassName, List<UIServiceCreator.FieldInfo> itemPropertiesOrder) Connects the given grid to the given service method, using the given item type and order for the columns.- Parameters:
grid- the grid to connectserviceClassName- The service class to use as data sourceserviceMethodName- the service method to use as data sourceparameterTypes- the service method parameter typesitemTypeClassName- the item type to show in the grid, must correspond to the service method return type argumentitemPropertiesOrder- the order of the properties to show in the grid
-
setComboBoxDataSource
public void setComboBoxDataSource(ComponentInfo comboBox, String serviceClassName, String serviceMethodName, List<JavaReflectionUtil.ParameterTypeInfo> parameterTypes, String itemTypeClassName, UIServiceCreator.FieldInfo displayProperty) Connects the given combo box to the given service method, using the given item type and order for the columns.- Parameters:
comboBox- the combo box to connectserviceClassName- The service class to use as data sourceserviceMethodName- the service method to use as data sourceparameterTypes- the service method parameter typesitemTypeClassName- the item type to show in the grid, must correspond to the service method return type argumentdisplayProperty- the property to display in the combo box
-
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 addedcomment- the comment to addcommentType- the type of comment (LINE, BLOCK, JAVADOC)
-