Interface NodeWithMembers<N extends Node>
- All Superinterfaces:
NodeWithSimpleName<N>
- All Known Implementing Classes:
AnnotationDeclaration,ClassOrInterfaceDeclaration,EnumDeclaration,RecordDeclaration,TypeDeclaration
A node having members.
The main reason for this interface is to permit users to manipulate homogeneously all nodes with a getMembers method.
-
Method Summary
Modifier and TypeMethodDescriptiondefault ConstructorDeclarationaddConstructor(Modifier.Keyword... modifiers)Adds a constructor to this node with members.default FieldDeclarationaddField(Type type, String name, Modifier.Keyword... modifiers)Add a field to this.default FieldDeclarationaddField(Class<?> typeClass, String name, Modifier.Keyword... modifiers)Add a field to this and automatically add the import of the type if neededdefault FieldDeclarationaddField(String type, String name, Modifier.Keyword... modifiers)Add a field to this.default FieldDeclarationaddFieldWithInitializer(Type type, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this.default FieldDeclarationaddFieldWithInitializer(Class<?> typeClass, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this and automatically add the import of the type if neededdefault FieldDeclarationaddFieldWithInitializer(String type, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this.default BlockStmtAdd an initializer block (InitializerDeclaration) to this.default NaddMember(BodyDeclaration<?> member)default MethodDeclarationaddMethod(String methodName, Modifier.Keyword... modifiers)Adds a methods with void return by default to this.default FieldDeclarationaddPrivateField(Type type, String name)Add a private field to this.default FieldDeclarationaddPrivateField(Class<?> typeClass, String name)Add a private field to this.default FieldDeclarationaddPrivateField(String type, String name)Add a private field to this and automatically add the import of the type if needed.default FieldDeclarationaddProtectedField(Type type, String name)Add a protected field to this.default FieldDeclarationaddProtectedField(Class<?> typeClass, String name)Add a protected field to this.default FieldDeclarationaddProtectedField(String type, String name)Add a protected field to this and automatically add the import of the type if needed.default FieldDeclarationaddPublicField(Type type, String name)Add a public field to this.default FieldDeclarationaddPublicField(Class<?> typeClass, String name)Add a public field to this.default FieldDeclarationaddPublicField(String type, String name)Add a public field to this and automatically add the import of the type if needed.default BlockStmtAdd a static initializer block (InitializerDeclaration) to this.default Optional<ConstructorDeclaration>getConstructorByParameterTypes(Class<?>... paramTypes)Try to find aConstructorDeclarationby its parameter types.default Optional<ConstructorDeclaration>getConstructorByParameterTypes(String... paramTypes)Try to find aConstructorDeclarationby its parameter types.default List<ConstructorDeclaration>Find all constructors in the members of this node.default Optional<ConstructorDeclaration>Try to find aConstructorDeclarationwith no parameters.default Optional<FieldDeclaration>getFieldByName(String name)Try to find aFieldDeclarationby its namedefault List<FieldDeclaration>Find all fields in the members of this node.default BodyDeclaration<?>getMember(int i)default List<MethodDeclaration>Find all methods in the members of this node.default List<MethodDeclaration>getMethodsByName(String name)Try to find aMethodDeclarationby its namedefault List<MethodDeclaration>getMethodsByParameterTypes(Class<?>... paramTypes)Try to find aMethodDeclarationby its parameter types.default List<MethodDeclaration>getMethodsByParameterTypes(String... paramTypes)Try to find aMethodDeclarationby its parameter types.default List<MethodDeclaration>getMethodsBySignature(String name, String... paramTypes)Try to findMethodDeclarations by their name and parameter types.default booleanisEmpty()default NsetMember(int i, BodyDeclaration<?> member)setMembers(NodeList<BodyDeclaration<?>> members)voidtryAddImportToParentCompilationUnit(Class<?> clazz)Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithSimpleName
getName, getNameAsExpression, getNameAsString, setName, setName
-
Method Details
-
getMembers
NodeList<BodyDeclaration<?>> getMembers()- Returns:
- all members inside the braces of this node, like fields, methods, nested types, etc.
-
tryAddImportToParentCompilationUnit
-
getMember
-
setMember
-
addMember
-
setMembers
-
addField
Add a field to this and automatically add the import of the type if needed- Parameters:
typeClass- the type of the fieldname- the name of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addField
Add a field to this.- Parameters:
type- the type of the fieldname- the name of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addField
Add a field to this.- Parameters:
type- the type of the fieldname- the name of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(Class<?> typeClass, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this and automatically add the import of the type if needed- Parameters:
typeClass- the type of the fieldname- the name of the fieldinitializer- the initializer of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(String type, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this.- Parameters:
type- the type of the fieldname- the name of the fieldinitializer- the initializer of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(Type type, String name, Expression initializer, Modifier.Keyword... modifiers)Add a field to this.- Parameters:
type- the type of the fieldname- the name of the fieldinitializer- the initializer of the fieldmodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
FieldDeclarationcreated
-
addPrivateField
Add a private field to this.- Parameters:
typeClass- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addPrivateField
Add a private field to this and automatically add the import of the type if needed.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addPrivateField
Add a private field to this.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addPublicField
Add a public field to this.- Parameters:
typeClass- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addPublicField
Add a public field to this and automatically add the import of the type if needed.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addPublicField
Add a public field to this.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addProtectedField
Add a protected field to this.- Parameters:
typeClass- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addProtectedField
Add a protected field to this and automatically add the import of the type if needed.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addProtectedField
Add a protected field to this.- Parameters:
type- the type of the fieldname- the name of the field- Returns:
- the
FieldDeclarationcreated
-
addMethod
Adds a methods with void return by default to this.- Parameters:
methodName- the method namemodifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the
MethodDeclarationcreated
-
addConstructor
Adds a constructor to this node with members.- Parameters:
modifiers- the modifiers likeModifier.Keyword.PUBLIC- Returns:
- the created constructor
-
addInitializer
Add an initializer block (InitializerDeclaration) to this. -
addStaticInitializer
Add a static initializer block (InitializerDeclaration) to this. -
getMethodsByName
Try to find aMethodDeclarationby its name- Parameters:
name- the name of the method- Returns:
- the methods found (multiple in case of overloading)
-
getMethods
Find all methods in the members of this node.- Returns:
- the methods found. This list is immutable.
-
getMethodsByParameterTypes
Try to find aMethodDeclarationby its parameter types. The given parameter types must literally match the declared types of this node's parameters, so passing the string"List"to this method will find all methods that have exactly one parameter whose type is declared asList, but not methods with exactly one parameter whose type is declared asjava.util.Listorjava.awt.List. Conversely, passing the string"java.util.List"to this method will find all methods that have exactly one parameter whose type is declared asjava.util.List, but not if the parameter type is declared asList. Similarly, note that generics are matched as well: If there is a method that has a parameter declared asList<String>, then it will be considered as a match only if the given string is"List<String>", but not if the given string is only"List".- Parameters:
paramTypes- the types of parameters like"Map<Integer, String>", "int"to matchvoid foo(Map<Integer,String> myMap, int number)- Returns:
- the methods found
-
getMethodsBySignature
Try to findMethodDeclarations by their name and parameter types. Parameter types are matched exactly as in the case ofgetMethodsByParameterTypes(String...).- Parameters:
paramTypes- the types of parameters like"Map<Integer, String>", "int"to matchvoid foo(Map<Integer,String> myMap, int number)- Returns:
- the methods found
-
getMethodsByParameterTypes
Try to find aMethodDeclarationby its parameter types. Note that this is a match in SimpleName, sojava.awt.Listandjava.util.Listare identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passingList.classto this method will return all methods that have exactly one parameter whose type is namedList, regardless of whether the parameter type is declared without generics asList, or with generics asList<String>, orList<Integer>, etc.- Parameters:
paramTypes- the types of parameters likeMap.class, int.classto matchvoid foo(Map<Integer,String> myMap, int number)- Returns:
- the methods found
-
getConstructors
Find all constructors in the members of this node. Note that only "normal" constructors, not the "compact" constructors", withinRecordDeclarationare included in the output of this method.- Returns:
- the constructors found. This list is immutable.
-
getDefaultConstructor
Try to find aConstructorDeclarationwith no parameters.- Returns:
- the constructor found, if any.
-
getConstructorByParameterTypes
Try to find aConstructorDeclarationby its parameter types. The given parameter types must literally match the declared types of the desired constructor, so passing the string"List"to this method will search for a constructor that has exactly one parameter whose type is declared asList, but not for a constructor with exactly one parameter whose type is declared asjava.util.Listorjava.awt.List. Conversely, passing the string"java.util.List"to this method will search for a constructor that has exactly one parameter whose type is declared asjava.util.List, but not for a constructor whose type is declared asList. Similarly, note that generics are matched as well: If there is a constructor that has a parameter declared asList<String>, then it will be considered as a match only if the given string is"List<String>", but not if the given string is only"List".- Parameters:
paramTypes- the types of parameters like"Map<Integer, String>", "int"to matchFoo(Map<Integer,String> myMap, int number)- Returns:
- the constructor found, if any.
-
getConstructorByParameterTypes
Try to find aConstructorDeclarationby its parameter types. Note that this is a match in SimpleName, sojava.awt.Listandjava.util.Listare identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passingList.classto this method will search for a constructor that has exactly one parameter whose type is namedList, regardless of whether the parameter type is declared without generics asList, or with generics asList<String>, orList<Integer>, etc.- Parameters:
paramTypes- the types of parameters likeMap.class, int.classto matchFoo(Map<Integer,String> myMap, int number)- Returns:
- the constructor found, if any.
-
getFieldByName
Try to find aFieldDeclarationby its name- Parameters:
name- the name of the field- Returns:
- null if not found, the FieldDeclaration otherwise
-
getFields
Find all fields in the members of this node.- Returns:
- the fields found. This list is immutable.
-
isEmpty
default boolean isEmpty()- Returns:
- true if there are no members contained in this node.
-