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) Adds a field declaration after the given reference field.static com.github.javaparser.ast.expr.MethodCallExpraddFunctionCall(ComponentInfo componentInfo, String function, List<com.github.javaparser.ast.expr.Expression> parameterExpressions) Adds a function call to the given component.static voidAdds an import to the given compilation unit if it is not already imported.static voidaddImport(com.github.javaparser.ast.CompilationUnit compilationUnit, String qualifiedName, boolean isStatic, boolean isAsterisk) Adds 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 booleancheckExpressionInLoop(com.github.javaparser.ast.expr.Expression expression) Checks if given expression is descendant of loop statements.static booleancheckMethodHasExpressionCalledInLoop(com.github.javaparser.ast.expr.Expression expression) Determines whether all invocations of the method containing the given expression are made from within a loop construct (e.g.,for,while, ordo-while) viacheckExpressionInLoop(Expression)static <T extends com.github.javaparser.ast.Node>
Tclone(T node) Clones the node in a way that does not mess up the lexical printer.static booleanconstructorArgumentIsArray(com.github.javaparser.ast.expr.ObjectCreationExpr expr, int argIndex) Resolves object creation expression to access Class API to get argument.isArray() value of given argument index.static booleancontainsMethodInEmptyConstructorCall(ComponentInfo componentInfo, String methodName) Analyzes bytecode of the given component to search for method with methodName is called in an empty constructorstatic com.github.javaparser.ast.expr.ObjectCreationExprcreateComponentConstructor(JavaComponent javaComponent, com.github.javaparser.ast.type.ClassOrInterfaceType componentType, String dataEntityRecordName, Consumer<String> importAdder) Creates the convenient component constructor for a new JavaComponent to be added to the code.static ComponentInfocreateComponentInfoForTemplateComponent(JavaSource javaSource, com.github.javaparser.ast.expr.ObjectCreationExpr objectCreationExpr, AttachExpression attachExpression) static com.github.javaparser.ast.type.TypeCreates a Type that can be used in generics to end up with `invalid input: '<'>`.static com.github.javaparser.ast.expr.MethodCallExprcreateMethodCall(com.github.javaparser.ast.expr.Expression scope, String function, com.github.javaparser.ast.expr.Expression... parameterExpressions) static booleanexpressionsInDifferentBlocks(com.github.javaparser.ast.expr.Expression expr1, com.github.javaparser.ast.expr.Expression expr2) Checks given two expression are in different blocks.extractInlineVariableToLocalVariable(ComponentInfo componentInfo) Extracts an inline variable to local variable with a new variable name.static intfindArgumentIndex(com.github.javaparser.ast.nodeTypes.NodeWithArguments<?> nodeWithArgs, com.github.javaparser.ast.expr.Expression argExpression) Iterates arguments to check any of the argument equals to argExpression.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 List<com.github.javaparser.ast.expr.MethodCallExpr> findCalls(Class<?> classWithMethods, ComponentInfo componentInfo) Find all calls done to methods in the given class through a reference to the given component.static Optional<Constructor<?>> findConstructor(Class<? extends com.vaadin.flow.component.Component> componentType, com.github.javaparser.resolution.Resolvable<com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration> resolvable) Finds constructor for the given component from the constructor that is provided byResolvableimplementationstatic intfindDeclarationIndex(com.github.javaparser.ast.Node node) Finds the index of the given node in the block statement.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 StringfindFreeRecordName(String base, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration block) 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 com.github.javaparser.ast.body.FieldDeclarationfindIdAnnotatedField(String id, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration classType) static InsertionPointfindLocationAfter(com.github.javaparser.ast.Node node) Finds the insert location after of given expression.static InsertionPointfindLocationAtEnd(com.github.javaparser.ast.stmt.Statement statement) Finds the insert location at the end of the code block containing the given statement.static InsertionPointfindLocationBefore(com.github.javaparser.ast.Node node) Finds the insert location before of given expression.static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(ComponentInfo componentDefinition) Finds usage of the variable outside of method call statements, i.e. all usage not reported byfindMethodCallStatements(ComponentInfo).static Stream<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(ComponentInfo componentDefinition, String functionName) Finds usage of the variable outside of method call statements, i.e. all usage not reported byfindMethodCallStatements(ComponentInfo).static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCalls(ComponentInfo componentInfo) Finds all method calls that are related to the given component.static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCalls(ComponentInfo componentInfo, com.github.javaparser.ast.Node.TreeTraversal traversalOrder) Finds all method calls that are related to the given component.static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(ComponentInfo componentDefinition) Find all method calls that are statements, i.e. calls like foo.bar() or baz.qux("zug")).static Stream<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(ComponentInfo componentInfo, String functionName) Find all method calls statements for the given function, i.e. calls like foo.bar() or baz.qux("zug")).static Stream<com.github.javaparser.ast.expr.Expression> findNameReferences(String variableName, com.github.javaparser.ast.stmt.BlockStmt scope) Finds all name references to the given variable name in the given scope.static List<com.github.javaparser.ast.expr.Expression> findParameterUsage(ComponentInfo componentDefinition) Find all expressions where the given component is used as a call parameter.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 List<com.github.javaparser.ast.stmt.ReturnStmt> findReturnStatements(ComponentInfo componentInfo) Find return statements referring to the 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 booleanfunctionAffectsVariableName(String functionName) Checks if the value passed to the given function affects the variable name generated for the component.static StringgenerateVariableName(JavaComponent javaComponent, com.github.javaparser.ast.type.ClassOrInterfaceType type, InsertionPoint insertionPoint) Provides a free name to be used when adding a new component.static StringgetAnnotationValue(com.github.javaparser.ast.expr.AnnotationExpr annotation) Gets the value attribute of the given annotation or null if no value is found.static Optional<com.github.javaparser.ast.expr.Expression> getAttachArgument(ComponentInfo component) Finds the attach argument reference of the given component.static com.github.javaparser.ast.expr.ExpressiongetAttachArgumentOrThrow(ComponentInfo component) Find the attach argument reference of given component or throwsIllegalArgumentExceptionif not found.static Enum<?> getEnumValue(Class<?> componentType, String setterName, String value) static StringgetFieldOrVariableName(ComponentInfo componentInfo) Gets the field or local variable name for the given component.static StringgetJavaIdentifier(String str, int maxLength) Converts the given string into a valid Java identifier.static StringgetMappedProperty(Constructor<?> c, int propertyIndex) Returns a property used in a constructor at the given indexstatic Optional<com.github.javaparser.ast.body.MethodDeclaration> getMethodIfEmpty(com.github.javaparser.ast.stmt.BlockStmt createScope) static Optional<com.github.javaparser.ast.expr.Expression> getOutermostScope(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr) Gets the outermost scope for a chain of method calls.static List<UIServiceCreator.FieldInfo> getPropertiesInSourceOrder(String itemType) Parses the given class and finds bean properties in the order they are defined.static StringgetPropertyName(String setter) Gets the camel case property name for the given setter.getQualifiedClassName(com.github.javaparser.resolution.declarations.ResolvedDeclaration resolvedDeclaration) Gets the qualified class name of ResolvedDeclaration when the given declaration is an instance ofResolvedValueDeclarationFor generic classes such asBinder, it does not return the generic type instead it would returncom.vaadin.flow.data.binder.Binderstatic 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.getSingleStringParamConstructor(com.github.javaparser.ast.type.ClassOrInterfaceType type, Set<String> setters) Returns the single String parameter name from mapped properties of the component that matches an existing single String parameter constructor of the componentstatic StringgetSymbolNameFromJavaParserException(com.github.javaparser.resolution.UnsolvedSymbolException e) Gets the name of the symbol that could not be resolved.static StringgetVariableNameForAdd(ComponentInfo referenceComponent) static com.github.javaparser.ast.expr.ObjectCreationExprgetWrapperDivObjectCreationExpr(com.github.javaparser.ast.CompilationUnit compilationUnit, com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> args) Creates a Div creation expressionnew Div()with given arguments.static booleanChecks if the given component type has the given method.static booleanhasSetterForType(Class<?> componentType, String setterName, Class<?> valueType) static booleanhasSingleParameterMethod(Class<? extends com.vaadin.flow.component.Component> type, String func) Checks if the given component type has a method with the given name, taking one parameter.static booleanisComponentOrComponentArray(Class<?> type) static booleanisConstructorArgumentFlowComponentType(com.github.javaparser.ast.expr.ObjectCreationExpr expr, int argIndex) Resolves object creation expression to access Class API to check if given argument in the argIndex exactly equals tocom.vaadin.flow.component.Componentstatic booleanisLocationRefersSource(ComponentTypeAndSourceLocation typeAndSourceLocation, com.github.javaparser.ast.CompilationUnit compilationUnit) Checks if given attach and create location refers to a source file instead of instance creation.static booleanisNodeInCompositeClass(com.github.javaparser.ast.Node node) Checks if the given node is a child of a Compositestatic booleanisObjectCreationExprInConstructor(ComponentInfo componentInfo) Checks if the given component is created inside the constructor of the class (as opposed to being created in another method).static booleanisVariableNameAutoGenerated(ComponentInfo componentInfo, String currentText, String currentLabel) Checks if the given component has a variable name that is auto-generated based on the type/label/text of the component.static voidmoveAboveMethodsAndConstructors(com.github.javaparser.ast.body.BodyDeclaration<?> toMove, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration classOrInterface) static StringregenerateVariableName(ComponentInfo componentInfo, String newText, String newLabel) Regenerates the variable name for the given component.static booleanremoveArgumentCalls(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) Removes the given arguments from the given method call.static voidremoveArgumentCalls(List<com.github.javaparser.ast.expr.MethodCallExpr> methods, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) Removes the given arguments from the given method calls.static voidremoveFromChainedStyleCall(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr) Removes the given method call from a getStyle call chain.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 Predicate<com.github.javaparser.ast.expr.MethodCallExpr> testMethodInClass(Class<?> classWithMethods, ComponentInfo componentInfo) Looks for given method call is a member of given class for the componentstatic booleantypesEqual(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration methodDeclaration1, com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration methodDeclaration2) Compares twoResolvedMethodDeclarationinstances to determine if they are semantically equivalent in terms of their signature and declaring context.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
-
createMethodCall
public static com.github.javaparser.ast.expr.MethodCallExpr createMethodCall(com.github.javaparser.ast.expr.Expression scope, String function, com.github.javaparser.ast.expr.Expression... parameterExpressions) -
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
-
findNameReferences
public static Stream<com.github.javaparser.ast.expr.Expression> findNameReferences(String variableName, com.github.javaparser.ast.stmt.BlockStmt scope) Finds all name references to the given variable name in the given scope.- Parameters:
variableName- the variable namescope- the scope- Returns:
- a stream of name references
-
findMethodCallStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(ComponentInfo componentDefinition) Find all method calls that are statements, i.e. calls like foo.bar() or baz.qux("zug")). Does not find usage of the variable in other expressions, like otherFunction("something" + foo.bar())- Parameters:
componentDefinition- the component to look for- Returns:
- a list of method calls
-
findMethodCallStatements
public static Stream<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(ComponentInfo componentInfo, String functionName) Find all method calls statements for the given function, i.e. calls like foo.bar() or baz.qux("zug")). Does not find usage of the variable in other expressions, like otherFunction("something" + foo.bar())- Parameters:
componentInfo- the component to look forfunctionName- the function name to look for- Returns:
- a list of method calls
-
findMethodCallNonStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(ComponentInfo componentDefinition) Finds usage of the variable outside of method call statements, i.e. all usage not reported byfindMethodCallStatements(ComponentInfo).- Parameters:
componentDefinition- the component to look for- Returns:
- a list of method calls
-
findMethodCallNonStatements
public static Stream<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(ComponentInfo componentDefinition, String functionName) Finds usage of the variable outside of method call statements, i.e. all usage not reported byfindMethodCallStatements(ComponentInfo).- Parameters:
componentDefinition- the component to look forfunctionName- the function name to look for- Returns:
- a list of method calls
-
findMethodCalls
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCalls(ComponentInfo componentInfo) Finds all method calls that are related to the given component.- Parameters:
componentInfo- the component to look for- Returns:
- a list of method calls
-
findMethodCalls
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCalls(ComponentInfo componentInfo, com.github.javaparser.ast.Node.TreeTraversal traversalOrder) Finds all method calls that are related to the given component.- Parameters:
componentInfo- the component to look fortraversalOrder- the order in which the nodes are traversed- Returns:
- a list of method calls
-
findParameterUsage
public static List<com.github.javaparser.ast.expr.Expression> findParameterUsage(ComponentInfo componentDefinition) Find all expressions where the given component is used as a call parameter.- Parameters:
componentDefinition- the component to look for- Returns:
- a list of expressions
-
extractInlineVariableToLocalVariable
public static JavaRewriter.ExtractInlineVariableResult extractInlineVariableToLocalVariable(ComponentInfo componentInfo) Extracts an inline variable to local variable with a new variable name. The new variable is created in the same block as the inline variable just before the replacement.- Parameters:
componentInfo- Component info- Returns:
- Block, new variable name and index
- Throws:
CopilotException- if the inline variable cannot be extracted
-
isLocationRefersSource
public static boolean isLocationRefersSource(ComponentTypeAndSourceLocation typeAndSourceLocation, com.github.javaparser.ast.CompilationUnit compilationUnit) Checks if given attach and create location refers to a source file instead of instance creation.- Parameters:
typeAndSourceLocation- the type and source location to checkcompilationUnit- the compilation unit that contains the code- Returns:
trueif given source location and compilation unit refers to source file,falseotherwise
-
hasSingleParameterMethod
public static boolean hasSingleParameterMethod(Class<? extends com.vaadin.flow.component.Component> type, String func) 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) Finds the index of the given node in the block statement.- Parameters:
node- The node to find the index for- Returns:
- The index of the node in the block statement or -1 if not found
-
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
-
findFreeRecordName
-
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
-
getPropertyName
Gets the camel case property name for the given setter.- Parameters:
setter- the setter name- Returns:
- the property 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
-
addImport
public static void addImport(com.github.javaparser.ast.CompilationUnit compilationUnit, String qualifiedName, boolean isStatic, boolean isAsterisk) Adds an import to the given compilation unit if it is not already imported.- Parameters:
compilationUnit- the compilation unitqualifiedName- the qualified name of the importisStatic- whether the import is staticisAsterisk- whether the import end with ".*"
-
getJavaIdentifier
Converts the given string into a valid Java identifier.- Parameters:
str- the stringmaxLength- the maximum length of the identifier, or 0 for unlimited- 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
-
hasSetterForType
-
getEnumValue
-
removeArgumentCalls
public static void removeArgumentCalls(List<com.github.javaparser.ast.expr.MethodCallExpr> methods, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) Removes the given arguments from the given method calls.- Parameters:
methods- the method calls to processargumentsToRemove- the arguments to removeremoveMethodIfNoArgs- whether to remove the method call if no arguments are left
-
removeArgumentCalls
public static boolean removeArgumentCalls(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) Removes the given arguments from the given method call.- Parameters:
methodCallExpr- the method callargumentsToRemove- the arguments to removeremoveMethodIfNoArgs- whether to remove the method call if no arguments are left- Returns:
true
-
findLocationBefore
Finds the insert location before of given expression.- Parameters:
node- Reference node- Returns:
- Returns code block and index
-
findLocationAfter
Finds the insert location after of given expression.- Parameters:
node- Reference node- Returns:
- the insert location
-
findLocationAtEnd
Finds the insert location at the end of the code block containing the given statement.- Parameters:
statement- The reference statement- Returns:
- the insert location
-
addFieldAfter
public static void addFieldAfter(com.github.javaparser.ast.body.FieldDeclaration newField, com.github.javaparser.ast.body.FieldDeclaration reference) Adds a field declaration after the given reference field.- Parameters:
newField- the new field declarationreference- the reference field
-
clone
public static <T extends com.github.javaparser.ast.Node> T clone(T node) Clones the node in a way that does not mess up the lexical printer.- Type Parameters:
T- the type of the node- Parameters:
node- the node to clone- Returns:
- the cloned node
-
getAttachArgument
public static Optional<com.github.javaparser.ast.expr.Expression> getAttachArgument(ComponentInfo component) Finds the attach argument reference of the given component.- Parameters:
component- the component- Returns:
- the attach argument reference, if found
-
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
-
typesEqual
public static boolean typesEqual(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration methodDeclaration1, com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration methodDeclaration2) Compares twoResolvedMethodDeclarationinstances to determine if they are semantically equivalent in terms of their signature and declaring context.- Parameters:
methodDeclaration1- the first method to comparemethodDeclaration2- the second method to compare- Returns:
trueif both methods are considered equal based on the criteria,falseotherwise
-
getWrapperDivObjectCreationExpr
public static com.github.javaparser.ast.expr.ObjectCreationExpr getWrapperDivObjectCreationExpr(com.github.javaparser.ast.CompilationUnit compilationUnit, com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> args) Creates a Div creation expressionnew Div()with given arguments. Given arguments will be cloned and clones will be added to Div constructor.- Parameters:
compilationUnit- current compilation unitargs- Argument expressions- Returns:
- Div object creation expression
-
isConstructorArgumentFlowComponentType
public static boolean isConstructorArgumentFlowComponentType(com.github.javaparser.ast.expr.ObjectCreationExpr expr, int argIndex) Resolves object creation expression to access Class API to check if given argument in the argIndex exactly equals tocom.vaadin.flow.component.Component- Parameters:
expr- Object creation expression of a statement e.g.new Div(a,b,c)argIndex- Argument to look for. If value exceeds the argument in the constructor, it looks for the last argument- Returns:
- true if argument class is com.vaadin.flow.component.Component or resolving of the expression fails, false otherwise.
-
constructorArgumentIsArray
public static boolean constructorArgumentIsArray(com.github.javaparser.ast.expr.ObjectCreationExpr expr, int argIndex) Resolves object creation expression to access Class API to get argument.isArray() value of given argument index.- Parameters:
expr- Object creation expression of a statement e.g.new Div(a,b,c)argIndex- Argument to look for. If value exceeds the argument in the constructor, it looks for the last argument- Returns:
- true when array is an argument or resolving of expression fails, false otherwise.
-
expressionsInDifferentBlocks
public static boolean expressionsInDifferentBlocks(com.github.javaparser.ast.expr.Expression expr1, com.github.javaparser.ast.expr.Expression expr2) Checks given two expression are in different blocks. If any expression is not a descendant of a method, it returns false.- Parameters:
expr1- first expressionexpr2- second expression- Returns:
- true if expressions present in different methods , false otherwise
-
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 com.vaadin.flow.component.Component> componentType, com.github.javaparser.resolution.Resolvable<com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration> resolvable) Finds constructor for the given component from the constructor that is provided byResolvableimplementation- Parameters:
componentType- Component classresolvable- Resolvable constructor declaration node- Returns:
- Constructor if found, empty otherwise.
-
createComponentConstructor
public static com.github.javaparser.ast.expr.ObjectCreationExpr createComponentConstructor(JavaComponent javaComponent, com.github.javaparser.ast.type.ClassOrInterfaceType componentType, String dataEntityRecordName, Consumer<String> importAdder) Creates the convenient component constructor for a new JavaComponent to be added to the code.- Returns:
- ObjectCreationExpr for Component
-
getSingleStringParamConstructor
public static Optional<String> getSingleStringParamConstructor(com.github.javaparser.ast.type.ClassOrInterfaceType type, Set<String> setters) Returns the single String parameter name from mapped properties of the component that matches an existing single String parameter constructor of the component- Parameters:
type- The component typesetters- The setters of the component- Returns:
- The name of the property that is the setter of the constructor
-
getMappedProperty
Returns a property used in a constructor at the given index- Parameters:
c- The constructorpropertyIndex- The index of the property- Returns:
- The property name
-
containsMethodInEmptyConstructorCall
public static boolean containsMethodInEmptyConstructorCall(ComponentInfo componentInfo, String methodName) Analyzes bytecode of the given component to search for method with methodName is called in an empty constructor- Parameters:
componentInfo- Component info to get empty constructormethodName- The method name to search for.- Returns:
- Returns true when method call found in constructor, false when constructor is not found or method is not present
-
generateVariableName
public static String generateVariableName(JavaComponent javaComponent, com.github.javaparser.ast.type.ClassOrInterfaceType type, InsertionPoint insertionPoint) Provides a free name to be used when adding a new component.- Parameters:
javaComponent- The JavaComponent to be addedtype- The type of the componentinsertionPoint- The insertion point- Returns:
- A free variable name
-
regenerateVariableName
public static String regenerateVariableName(ComponentInfo componentInfo, String newText, String newLabel) Regenerates the variable name for the given component.- Parameters:
componentInfo- the component to regenerate the variable name fornewText- the text of the componentnewLabel- the label of the component- Returns:
- the new variable name
-
functionAffectsVariableName
Checks if the value passed to the given function affects the variable name generated for the component.- Parameters:
functionName- the function name- Returns:
trueif the function affects the variable name,falseotherwise
-
isVariableNameAutoGenerated
public static boolean isVariableNameAutoGenerated(ComponentInfo componentInfo, String currentText, String currentLabel) Checks if the given component has a variable name that is auto-generated based on the type/label/text of the component.- Parameters:
componentInfo- the component to checkcurrentText- the current text of the componentcurrentLabel- the current label of the component- Returns:
trueif the variable name is auto-generated,falseotherwise
-
findCalls
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findCalls(Class<?> classWithMethods, ComponentInfo componentInfo) Find all calls done to methods in the given class through a reference to the given component.For instance, if you pass in Style.class, then it will find all invocations like
button.getStyle().set("color","red"),button.getStyle().setColor("blue")and alsoStyle s = button.getStyle(); s.setColor("black")- Parameters:
classWithMethods- the class the check for callscomponentInfo- the component- Returns:
- a list of method calls
-
testMethodInClass
public static Predicate<com.github.javaparser.ast.expr.MethodCallExpr> testMethodInClass(Class<?> classWithMethods, ComponentInfo componentInfo) Looks for given method call is a member of given class for the component- Parameters:
classWithMethods- Class that where method instance is declaredcomponentInfo- Component to look for variable names- Returns:
- predicate that
-
addFunctionCall
@Nonnull public static com.github.javaparser.ast.expr.MethodCallExpr addFunctionCall(ComponentInfo componentInfo, String function, List<com.github.javaparser.ast.expr.Expression> parameterExpressions) Adds a function call to the given component.- Parameters:
componentInfo- the componentfunction- the function to callparameterExpressions- the parameters to pass to the function
-
getOutermostScope
public static Optional<com.github.javaparser.ast.expr.Expression> getOutermostScope(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr) Gets the outermost scope for a chain of method calls.E.g. for
foo.bar().baz().qux()it will returnfoo- Parameters:
methodCallExpr- The method call expression- Returns:
- The outermost scope
-
removeFromChainedStyleCall
public static void removeFromChainedStyleCall(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr) Removes the given method call from a getStyle call chain.If the method call is the only style call, removes the whole statement.
- Parameters:
methodCallExpr- the method call to remove
-
moveAboveMethodsAndConstructors
public static void moveAboveMethodsAndConstructors(com.github.javaparser.ast.body.BodyDeclaration<?> toMove, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration classOrInterface) -
createEmptyType
public static com.github.javaparser.ast.type.Type createEmptyType()Creates a Type that can be used in generics to end up with `invalid input: '<'>`.- Returns:
- an empty type
-
isObjectCreationExprInConstructor
Checks if the given component is created inside the constructor of the class (as opposed to being created in another method).- Parameters:
componentInfo- the component- Returns:
trueif the component is created in the constructor,falseotherwise
-
getPropertiesInSourceOrder
public static List<UIServiceCreator.FieldInfo> getPropertiesInSourceOrder(String itemType) throws IOException Parses the given class and finds bean properties in the order they are defined.- Parameters:
itemType- the item class name- Returns:
- a list of property names in the order they are defined in the source
- Throws:
IOException- if the class file cannot be read
-
getSymbolNameFromJavaParserException
public static String getSymbolNameFromJavaParserException(com.github.javaparser.resolution.UnsolvedSymbolException e) Gets the name of the symbol that could not be resolved.- Parameters:
e- the exception from java parser- Returns:
- the name of the symbol
-
isComponentOrComponentArray
-
checkExpressionInLoop
public static boolean checkExpressionInLoop(com.github.javaparser.ast.expr.Expression expression) Checks if given expression is descendant of loop statements.ForStmt,WhileStmt,DoStmt- Parameters:
expression- expression to check- Returns:
- true if loop statements found in ancestor, false otherwise.
-
checkMethodHasExpressionCalledInLoop
public static boolean checkMethodHasExpressionCalledInLoop(com.github.javaparser.ast.expr.Expression expression) Determines whether all invocations of the method containing the given expression are made from within a loop construct (e.g.,for,while, ordo-while) viacheckExpressionInLoop(Expression)- Parameters:
expression- the expression located within a method whose calls are to be analyzed- Returns:
trueif all method calls to the enclosing method are found inside loops;falseotherwise or if any step in the process fails
-
findArgumentIndex
public static int findArgumentIndex(com.github.javaparser.ast.nodeTypes.NodeWithArguments<?> nodeWithArgs, com.github.javaparser.ast.expr.Expression argExpression) Iterates arguments to check any of the argument equals to argExpression.- Parameters:
nodeWithArgs- node with arguments. e.g.MethodCallExpr,ExplicitConstructorInvocationStmtargExpression- expression to check- Returns:
- argument index or -1 if not found.
-
getVariableNameForAdd
-
createComponentInfoForTemplateComponent
public static ComponentInfo createComponentInfoForTemplateComponent(JavaSource javaSource, com.github.javaparser.ast.expr.ObjectCreationExpr objectCreationExpr, AttachExpression attachExpression) -
findReturnStatements
public static List<com.github.javaparser.ast.stmt.ReturnStmt> findReturnStatements(ComponentInfo componentInfo) Find return statements referring to the component.- Parameters:
componentInfo- the component to find return statements for- Returns:
- a list of return statements that refer to the component
-
getMethodIfEmpty
public static Optional<com.github.javaparser.ast.body.MethodDeclaration> getMethodIfEmpty(com.github.javaparser.ast.stmt.BlockStmt createScope) -
getAnnotationValue
Gets the value attribute of the given annotation or null if no value is found.- Parameters:
annotation- the annotation to check- Returns:
- the value of the value attribute
-
findIdAnnotatedField
public static com.github.javaparser.ast.body.FieldDeclaration findIdAnnotatedField(String id, com.github.javaparser.ast.body.ClassOrInterfaceDeclaration classType) -
getQualifiedClassName
public static Optional<String> getQualifiedClassName(com.github.javaparser.resolution.declarations.ResolvedDeclaration resolvedDeclaration) Gets the qualified class name of ResolvedDeclaration when the given declaration is an instance ofResolvedValueDeclarationFor generic classes such asBinder, it does not return the generic type instead it would returncom.vaadin.flow.data.binder.Binder- Parameters:
resolvedDeclaration- Resolved declaration fromResolvable.resolve()- Returns:
- the qualified class name of the given declaration.
-