Package com.vaadin.copilot.javarewriter
Class JavaRewriterUtil
java.lang.Object
com.vaadin.copilot.javarewriter.JavaRewriterUtil
Util methods for rewriting Java
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddFieldAfter(com.github.javaparser.ast.body.FieldDeclaration newField, com.github.javaparser.ast.body.FieldDeclaration reference) static voidAdds an import to the given compilation unit if it is not already imported.static booleanappendExpressionAsNextSiblingInBlockAncestor(com.github.javaparser.ast.Node original, com.github.javaparser.ast.stmt.Statement newExpression) Appends new expression to original one as sibling in a block statement.static <T extends com.github.javaparser.ast.Node>
Tclone(T node) extractInlineVariableToLocalVariable(ComponentInfo componentInfo) Extracts an inline variable to local variable with a new variable name.static intfindBlockStatementIndex(com.github.javaparser.ast.Node node) Finds the index of the statement that wraps the given node in the closest block statement.static Optional<Constructor<?>>findConstructor(Class<? extends Component> componentType, com.github.javaparser.ast.expr.ObjectCreationExpr objectCreationExpr) Finds constructor for the given componentstatic intfindDeclarationIndex(com.github.javaparser.ast.Node node) static com.github.javaparser.ast.body.FieldDeclarationfindFieldDeclaration(com.github.javaparser.ast.Node nodeInClass, String fieldName) Finds the field declaration for the given field name in the class of the given node.static StringfindFreeVariableName(ComponentInfo componentInfo, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the component type.static StringfindFreeVariableName(String base, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration block) Finds a free field name based on the given base name.static StringfindFreeVariableName(String base, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the given base name.static InsertionPointfindLocationAfter(com.github.javaparser.ast.expr.Expression expr) Finds the insert location after of given expression.static InsertionPointfindLocationAtEnd(com.github.javaparser.ast.stmt.Statement statement) static InsertionPointfindLocationBefore(com.github.javaparser.ast.expr.Expression expr) Finds the insert location before of given expression.static List<com.github.javaparser.ast.expr.MethodCallExpr>findMethodCallNonStatements(ComponentInfo componentDefinition) static List<com.github.javaparser.ast.expr.MethodCallExpr>findMethodCallStatements(ComponentInfo componentDefinition) static List<com.github.javaparser.ast.expr.Expression>findParameterUsage(ComponentInfo componentDefinition) static Optional<com.github.javaparser.ast.expr.Expression>findReference(com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> nodes, ComponentInfo componentDefinition) Finds a node among the nodes which refer to the given component.static ObjectfromExpression(com.github.javaparser.ast.expr.Expression arg, com.github.javaparser.resolution.types.ResolvedType expectedType) Parses the given expression and returns the object it represents.static Optional<com.github.javaparser.ast.expr.Expression>getAttachArgument(ComponentInfo component) static com.github.javaparser.ast.expr.ExpressiongetAttachArgumentOrThrow(ComponentInfo component) Find the attach argument reference of given component or throwsIllegalArgumentExceptionif not found.static Class<?>Finds the class for the given source type.static StringgetFieldOrVariableName(ComponentInfo componentInfo) Gets the field or local variable name for the given component.static StringgetJavaIdentifier(String str) Converts the given string into a valid Java identifier.static Optional<com.github.javaparser.ast.expr.Expression>getScopeIgnoreComposite(ComponentInfo componentInfo, com.github.javaparser.ast.expr.MethodCallExpr expr) Returns the scope by ignoring thegetContent()if component is in a composite container.static JavaRewriter.SetterAndValuegetSetterAndValue(Class<?> componentType, String property, Object value) Gets the setter name and value for the given component type, property and value.static StringgetSetterName(String property, Class<?> type, boolean includeReactConversions) Gets the setter name for the given property.static booleanChecks if the given component type has the given method.static booleanhasSingleParameterMethod(Class<? extends Component> type, String func) Checks if the given component type has a method with the given name, taking one parameter.static booleanisArrayArgument(String className, String methodName, int argumentIndex) Uses reflection API for finding class name, method and the argument to find whether argument is array.
Includes the methods from extended Composite class if it is extended.static booleanisNodeInCompositeClass(com.github.javaparser.ast.Node node) Checks if the given node is a child of a Compositestatic booleanisRouteClass(ComponentTypeAndSourceLocation typeAndSourceLocation, com.github.javaparser.ast.CompilationUnit compilationUnit) static booleanremoveArgumentCalls(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) static voidremoveArgumentCalls(List<com.github.javaparser.ast.expr.MethodCallExpr> methods, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) static booleanremoveFromStringConcatenation(com.github.javaparser.ast.Node node) Removes the given node from a string concatenation expression.static voidremoveStatement(com.github.javaparser.ast.Node node) Removes the statement that wraps the given node.static booleansetNameExprScope(com.github.javaparser.ast.expr.MethodCallExpr newCall, com.github.javaparser.ast.expr.NameExpr nameExpr) Sets the name expression scope for the given method call.static booleantypesEqual(com.github.javaparser.resolution.types.ResolvedType javaParserType, Class<?> javaReflectionType) Checks if the given type is equal to the given reflection type.
-
Method Details
-
fromExpression
public static Object fromExpression(com.github.javaparser.ast.expr.Expression arg, com.github.javaparser.resolution.types.ResolvedType expectedType) Parses the given expression and returns the object it represents.If the argument is a null literal, it is returned as is. Method call expressions are also returned as is.
- Parameters:
arg- the expression to parseexpectedType- the expected type of the object or null if unknown- Returns:
- the object represented by the expression
-
findMethodCallStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(ComponentInfo componentDefinition) -
findMethodCallNonStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(ComponentInfo componentDefinition) -
findParameterUsage
public static List<com.github.javaparser.ast.expr.Expression> findParameterUsage(ComponentInfo componentDefinition) -
extractInlineVariableToLocalVariable
public static JavaRewriter.ExtractInlineVariableResult extractInlineVariableToLocalVariable(ComponentInfo componentInfo) Extracts an inline variable to local variable with a new variable name.- Parameters:
componentInfo- Component info- Returns:
- Block, new variable name and index, null if attach call is not found.
-
isRouteClass
public static boolean isRouteClass(ComponentTypeAndSourceLocation typeAndSourceLocation, com.github.javaparser.ast.CompilationUnit compilationUnit) -
hasSingleParameterMethod
Checks if the given component type has a method with the given name, taking one parameter.- Parameters:
type- The component typefunc- The method name- Returns:
trueif the method exists,falseotherwise
-
findBlockStatementIndex
public static int findBlockStatementIndex(com.github.javaparser.ast.Node node) Finds the index of the statement that wraps the given node in the closest block statement.- Parameters:
node- The node to find the wrapping statement for- Returns:
- The index of the statement in the block statement
-
findDeclarationIndex
public static int findDeclarationIndex(com.github.javaparser.ast.Node node) -
findFreeVariableName
public static String findFreeVariableName(ComponentInfo componentInfo, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the component type.- Parameters:
componentInfo- The component infoblock- The block the variable will be used in- Returns:
- A free variable name
-
findFreeVariableName
public static String findFreeVariableName(String base, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the given base name.- Parameters:
base- The base nameblock- The block the variable will be used in- Returns:
- A free variable name
-
findFreeVariableName
public static String findFreeVariableName(String base, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration block) Finds a free field name based on the given base name.- Parameters:
base- The base nameblock- The ClassOrInterfaceDeclaration the variable will be used in- Returns:
- A free variable name
-
removeStatement
public static void removeStatement(com.github.javaparser.ast.Node node) Removes the statement that wraps the given node.- Parameters:
node- the node to remove
-
removeFromStringConcatenation
public static boolean removeFromStringConcatenation(com.github.javaparser.ast.Node node) Removes the given node from a string concatenation expression.- Parameters:
node- the node to remove- Returns:
trueif the node was removed,falseotherwise
-
findReference
public static Optional<com.github.javaparser.ast.expr.Expression> findReference(com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> nodes, ComponentInfo componentDefinition) Finds a node among the nodes which refer to the given component.- Parameters:
nodes- the nodes to searchcomponentDefinition- the component definition- Returns:
- the first node that refers to the component, if any
-
findFieldDeclaration
public static com.github.javaparser.ast.body.FieldDeclaration findFieldDeclaration(com.github.javaparser.ast.Node nodeInClass, String fieldName) Finds the field declaration for the given field name in the class of the given node.- Parameters:
nodeInClass- the node in the classfieldName- the field name- Returns:
- the field declaration
- Throws:
IllegalArgumentException- if the field is not found
-
getSetterAndValue
public static JavaRewriter.SetterAndValue getSetterAndValue(Class<?> componentType, String property, Object value) Gets the setter name and value for the given component type, property and value.- Parameters:
componentType- the component typeproperty- the propertyvalue- the value- Returns:
- the setter name and value
-
getSetterName
Gets the setter name for the given property.- Parameters:
property- the propertytype- the component typeincludeReactConversions- whether to include React property name conversions in the setter name- Returns:
- the setter name
-
getFieldOrVariableName
Gets the field or local variable name for the given component.- Parameters:
componentInfo- the component info- Returns:
- the field or local variable name
-
addImport
public static void addImport(com.github.javaparser.ast.CompilationUnit compilationUnit, String qualifiedName) Adds an import to the given compilation unit if it is not already imported.- Parameters:
compilationUnit- the compilation unitqualifiedName- the qualified name of the import
-
getJavaIdentifier
Converts the given string into a valid Java identifier.- Parameters:
str- the string- Returns:
- the Java identifier
-
hasMethod
Checks if the given component type has the given method.- Parameters:
type- the component typemethodName- the method to check for- Returns:
trueif the component has the method,falseotherwise
-
getClass
Finds the class for the given source type.- Parameters:
name- the class name- Returns:
- the class for the given name
- Throws:
IllegalArgumentException- if the class is not found
-
removeArgumentCalls
-
removeArgumentCalls
public static boolean removeArgumentCalls(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) -
findLocationBefore
Finds the insert location before of given expression.- Parameters:
expr- Reference expression- Returns:
- Returns code block and index
-
findLocationAfter
Finds the insert location after of given expression.- Parameters:
expr- Reference expression- Returns:
- Returns code block and index
-
findLocationAtEnd
-
addFieldAfter
public static void addFieldAfter(com.github.javaparser.ast.body.FieldDeclaration newField, com.github.javaparser.ast.body.FieldDeclaration reference) -
clone
public static <T extends com.github.javaparser.ast.Node> T clone(T node) -
getAttachArgument
public static Optional<com.github.javaparser.ast.expr.Expression> getAttachArgument(ComponentInfo component) -
getAttachArgumentOrThrow
public static com.github.javaparser.ast.expr.Expression getAttachArgumentOrThrow(ComponentInfo component) Find the attach argument reference of given component or throwsIllegalArgumentExceptionif not found.- Parameters:
component-- Returns:
- attach expression
-
setNameExprScope
public static boolean setNameExprScope(com.github.javaparser.ast.expr.MethodCallExpr newCall, com.github.javaparser.ast.expr.NameExpr nameExpr) Sets the name expression scope for the given method call. For a simple call like `foo.setBar()` this changes `foo` and for nested calls like `foo.baz().fo().a().b()` it also changes `foo`.- Parameters:
newCall- the method call to changenameExpr- the new scope- Returns:
trueif the scope was changed,falseotherwise
-
typesEqual
public static boolean typesEqual(com.github.javaparser.resolution.types.ResolvedType javaParserType, Class<?> javaReflectionType) Checks if the given type is equal to the given reflection type.- Parameters:
javaParserType- the Java parser typejavaReflectionType- the Java reflection type- Returns:
trueif the types are equal,falseotherwise
-
isArrayArgument
Uses reflection API for finding class name, method and the argument to find whether argument is array.
Includes the methods from extended Composite class if it is extended.- Parameters:
className- full class name of a class.methodName- method nameargumentIndex- argument to look up. It can exceed the method parameters, in that case the last argument is taken into account- Returns:
trueif argument is an array,falseotherwise
-
appendExpressionAsNextSiblingInBlockAncestor
public static boolean appendExpressionAsNextSiblingInBlockAncestor(com.github.javaparser.ast.Node original, com.github.javaparser.ast.stmt.Statement newExpression) Appends new expression to original one as sibling in a block statement.- Parameters:
original- Original expressionnewExpression- New expression- Returns:
- true if added, otherwise false.
-
getScopeIgnoreComposite
public static Optional<com.github.javaparser.ast.expr.Expression> getScopeIgnoreComposite(ComponentInfo componentInfo, com.github.javaparser.ast.expr.MethodCallExpr expr) Returns the scope by ignoring thegetContent()if component is in a composite container. Otherwise, returns the scope of method.
getContent().add(...)returnsemptythis.getContent().add(...)returnsthissetSomething()returnsemptyabc.setSomething()returnsabcthis.setSomething()returnsthis
- Parameters:
componentInfo- Component Infoexpr- Method to get scope- Returns:
- Scope of the method, empty otherwise.
-
isNodeInCompositeClass
public static boolean isNodeInCompositeClass(com.github.javaparser.ast.Node node) Checks if the given node is a child of a Composite- Parameters:
node- Any node in the tree- Returns:
trueif child of a composite ,falseotherwise
-
findConstructor
public static Optional<Constructor<?>> findConstructor(Class<? extends Component> componentType, com.github.javaparser.ast.expr.ObjectCreationExpr objectCreationExpr) Finds constructor for the given component- Parameters:
componentType- Component classobjectCreationExpr- Creation expression of the component.- Returns:
- Constructor if found, empty otherwise.
-