com.vaadin.copilot.javarewriter.
Class JavaRewriterUtil
Util methods for rewriting Java
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Adds an import to the given compilation unit if it is not already imported.
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.
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.
static String
findFreeVariableName
(JavaRewriter.ComponentInfo componentInfo, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the component type.
static String
findFreeVariableName
(String base, com.github.javaparser.ast.stmt.BlockStmt block) Finds a free variable name based on the given base name.
static List<com.github.javaparser.ast.expr.MethodCallExpr>
findMethodCallNonStatements
(JavaRewriter.ComponentInfo componentDefinition) static List<com.github.javaparser.ast.expr.MethodCallExpr>
findMethodCallStatements
(JavaRewriter.ComponentInfo componentDefinition) static List<com.github.javaparser.ast.expr.Expression>
findParameterUsage
(JavaRewriter.ComponentInfo componentDefinition) static Optional<com.github.javaparser.ast.expr.Expression>
findReference
(com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> nodes, JavaRewriter.ComponentInfo componentDefinition) Finds a node among the nodes which refer to the given component.
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.
static Class<?>
Finds the class for the given source type.
static String
getFieldOrVariableName
(JavaRewriter.ComponentInfo componentInfo) Gets the field or local variable name for the given component.
static String
getJavaIdentifier
(String str) Converts the given string into a valid Java identifier.
static JavaRewriter.SetterAndValue
getSetterAndValue
(Class<?> componentType, String property, Object value) Gets the setter name and value for the given component type, property and value.
static String
getSetterName
(String property, Class<?> type, boolean includeReactConversions) Gets the setter name for the given property.
static boolean
Checks if the given component type has the given method.
static boolean
hasSingleParameterMethod
(Class<? extends Component> type, String func) Checks if the given component type has a method with the given name, taking one parameter.
static boolean
onSameLine
(com.github.javaparser.ast.Node node1, com.github.javaparser.ast.Node node2) Checks if the two nodes are on the same line.
static boolean
removeArgumentCalls
(com.github.javaparser.ast.expr.MethodCallExpr methodCallExpr, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) static void
removeArgumentCalls
(List<com.github.javaparser.ast.expr.MethodCallExpr> methods, List<? extends com.github.javaparser.ast.expr.Expression> argumentsToRemove, boolean removeMethodIfNoArgs) static boolean
removeFromStringConcatenation
(com.github.javaparser.ast.Node node) Removes the given node from a string concatenation expression.
static void
removeStatement
(com.github.javaparser.ast.Node node) Removes the statement that wraps the given node.
-
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 unknownReturns:
the object represented by the expression
-
findMethodCallStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallStatements(JavaRewriter.ComponentInfo componentDefinition) -
findMethodCallNonStatements
public static List<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCallNonStatements(JavaRewriter.ComponentInfo componentDefinition) -
findParameterUsage
public static List<com.github.javaparser.ast.expr.Expression> findParameterUsage(JavaRewriter.ComponentInfo componentDefinition) -
hasSingleParameterMethod
Checks if the given component type has a method with the given name, taking one parameter.
Parameters:
type
- The component typefunc
- The method nameReturns:
true
if the method exists,false
otherwise -
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 forReturns:
The index of the statement in the block statement
-
findFreeVariableName
public static String findFreeVariableName(JavaRewriter.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 inReturns:
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 inReturns:
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 removeReturns:
true
if the node was removed,false
otherwise -
findReference
public static Optional<com.github.javaparser.ast.expr.Expression> findReference(com.github.javaparser.ast.NodeList<com.github.javaparser.ast.expr.Expression> nodes, JavaRewriter.ComponentInfo componentDefinition) Finds a node among the nodes which refer to the given component.
Parameters:
nodes
- the nodes to searchcomponentDefinition
- the component definitionReturns:
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 nameReturns:
the field declaration
Throws:
IllegalArgumentException
- if the field is not found -
onSameLine
public static boolean onSameLine(com.github.javaparser.ast.Node node1, com.github.javaparser.ast.Node node2) Checks if the two nodes are on the same line.
Parameters:
node1
- the first nodenode2
- the second nodeReturns:
true
if the nodes are on the same line,false
otherwise -
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 valueReturns:
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 nameReturns:
the setter name
-
getFieldOrVariableName
Gets the field or local variable name for the given component.
Parameters:
componentInfo
- the component infoReturns:
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 stringReturns:
the Java identifier
-
hasMethod
Checks if the given component type has the given method.
Parameters:
type
- the component typemethodName
- the method to check forReturns:
true
if the component has the method,false
otherwise -
getClass
Finds the class for the given source type.
Parameters:
name
- the class nameReturns:
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)
-