Interface NodeWithMembers<N extends Node>
-
- All Superinterfaces:
NodeWithSimpleName<N>
- All Known Implementing Classes:
AnnotationDeclaration
,ClassOrInterfaceDeclaration
,EnumDeclaration
,TypeDeclaration
public interface NodeWithMembers<N extends Node> extends NodeWithSimpleName<N>
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
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default ConstructorDeclaration
addConstructor(Modifier.Keyword... modifiers)
Adds a constructor to this node with members.default FieldDeclaration
addField(Type type, java.lang.String name, Modifier.Keyword... modifiers)
Add a field to this.default FieldDeclaration
addField(java.lang.Class<?> typeClass, java.lang.String name, Modifier.Keyword... modifiers)
Add a field to this and automatically add the import of the type if neededdefault FieldDeclaration
addField(java.lang.String type, java.lang.String name, Modifier.Keyword... modifiers)
Add a field to this.default FieldDeclaration
addFieldWithInitializer(Type type, java.lang.String name, Expression initializer, Modifier.Keyword... modifiers)
Add a field to this.default FieldDeclaration
addFieldWithInitializer(java.lang.Class<?> typeClass, java.lang.String name, Expression initializer, Modifier.Keyword... modifiers)
Add a field to this and automatically add the import of the type if neededdefault FieldDeclaration
addFieldWithInitializer(java.lang.String type, java.lang.String name, Expression initializer, Modifier.Keyword... modifiers)
Add a field to this.default BlockStmt
addInitializer()
Add an initializer block (InitializerDeclaration
) to this.default N
addMember(BodyDeclaration<?> member)
default MethodDeclaration
addMethod(java.lang.String methodName, Modifier.Keyword... modifiers)
Adds a methods with void return by default to this.default FieldDeclaration
addPrivateField(Type type, java.lang.String name)
Add a private field to this.default FieldDeclaration
addPrivateField(java.lang.Class<?> typeClass, java.lang.String name)
Add a private field to this.default FieldDeclaration
addPrivateField(java.lang.String type, java.lang.String name)
Add a private field to this and automatically add the import of the type if needed.default FieldDeclaration
addProtectedField(Type type, java.lang.String name)
Add a protected field to this.default FieldDeclaration
addProtectedField(java.lang.Class<?> typeClass, java.lang.String name)
Add a protected field to this.default FieldDeclaration
addProtectedField(java.lang.String type, java.lang.String name)
Add a protected field to this and automatically add the import of the type if needed.default FieldDeclaration
addPublicField(Type type, java.lang.String name)
Add a public field to this.default FieldDeclaration
addPublicField(java.lang.Class<?> typeClass, java.lang.String name)
Add a public field to this.default FieldDeclaration
addPublicField(java.lang.String type, java.lang.String name)
Add a public field to this and automatically add the import of the type if needed.default BlockStmt
addStaticInitializer()
Add a static initializer block (InitializerDeclaration
) to this.default java.util.Optional<ConstructorDeclaration>
getConstructorByParameterTypes(java.lang.Class<?>... paramTypes)
Try to find aConstructorDeclaration
by its parameter types.default java.util.Optional<ConstructorDeclaration>
getConstructorByParameterTypes(java.lang.String... paramTypes)
Try to find aConstructorDeclaration
by its parameter types.default java.util.List<ConstructorDeclaration>
getConstructors()
Find all constructors in the members of this node.default java.util.Optional<ConstructorDeclaration>
getDefaultConstructor()
Try to find aConstructorDeclaration
with no parameters.default java.util.Optional<FieldDeclaration>
getFieldByName(java.lang.String name)
Try to find aFieldDeclaration
by its namedefault java.util.List<FieldDeclaration>
getFields()
Find all fields in the members of this node.default BodyDeclaration<?>
getMember(int i)
NodeList<BodyDeclaration<?>>
getMembers()
default java.util.List<MethodDeclaration>
getMethods()
Find all methods in the members of this node.default java.util.List<MethodDeclaration>
getMethodsByName(java.lang.String name)
Try to find aMethodDeclaration
by its namedefault java.util.List<MethodDeclaration>
getMethodsByParameterTypes(java.lang.Class<?>... paramTypes)
Try to find aMethodDeclaration
by its parameter types.default java.util.List<MethodDeclaration>
getMethodsByParameterTypes(java.lang.String... paramTypes)
Try to find aMethodDeclaration
by its parameter types.default java.util.List<MethodDeclaration>
getMethodsBySignature(java.lang.String name, java.lang.String... paramTypes)
Try to findMethodDeclaration
s by their name and parameter types.default boolean
isEmpty()
default N
setMember(int i, BodyDeclaration<?> member)
N
setMembers(NodeList<BodyDeclaration<?>> members)
void
tryAddImportToParentCompilationUnit(java.lang.Class<?> clazz)
-
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithSimpleName
getName, getNameAsExpression, getNameAsString, setName, setName
-
-
-
-
Method Detail
-
getMembers
NodeList<BodyDeclaration<?>> getMembers()
- Returns:
- all members inside the braces of this node, like fields, methods, nested types, etc.
-
tryAddImportToParentCompilationUnit
void tryAddImportToParentCompilationUnit(java.lang.Class<?> clazz)
-
getMember
default BodyDeclaration<?> getMember(int i)
-
setMember
default N setMember(int i, BodyDeclaration<?> member)
-
addMember
default N addMember(BodyDeclaration<?> member)
-
setMembers
N setMembers(NodeList<BodyDeclaration<?>> members)
-
addField
default FieldDeclaration addField(java.lang.Class<?> typeClass, java.lang.String name, 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 fieldmodifiers
- the modifiers likeModifier.Keyword.PUBLIC
- Returns:
- the
FieldDeclaration
created
-
addField
default FieldDeclaration addField(java.lang.String type, java.lang.String name, Modifier.Keyword... modifiers)
Add a field to this.- Parameters:
type
- the type of the fieldname
- the name of the fieldmodifiers
- the modifiers likeModifier.Keyword.PUBLIC
- Returns:
- the
FieldDeclaration
created
-
addField
default FieldDeclaration addField(Type type, java.lang.String name, Modifier.Keyword... modifiers)
Add a field to this.- Parameters:
type
- the type of the fieldname
- the name of the fieldmodifiers
- the modifiers likeModifier.Keyword.PUBLIC
- Returns:
- the
FieldDeclaration
created
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(java.lang.Class<?> typeClass, java.lang.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
FieldDeclaration
created
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(java.lang.String type, java.lang.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
FieldDeclaration
created
-
addFieldWithInitializer
default FieldDeclaration addFieldWithInitializer(Type type, java.lang.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
FieldDeclaration
created
-
addPrivateField
default FieldDeclaration addPrivateField(java.lang.Class<?> typeClass, java.lang.String name)
Add a private field to this.- Parameters:
typeClass
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addPrivateField
default FieldDeclaration addPrivateField(java.lang.String type, java.lang.String name)
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
FieldDeclaration
created
-
addPrivateField
default FieldDeclaration addPrivateField(Type type, java.lang.String name)
Add a private field to this.- Parameters:
type
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addPublicField
default FieldDeclaration addPublicField(java.lang.Class<?> typeClass, java.lang.String name)
Add a public field to this.- Parameters:
typeClass
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addPublicField
default FieldDeclaration addPublicField(java.lang.String type, java.lang.String name)
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
FieldDeclaration
created
-
addPublicField
default FieldDeclaration addPublicField(Type type, java.lang.String name)
Add a public field to this.- Parameters:
type
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addProtectedField
default FieldDeclaration addProtectedField(java.lang.Class<?> typeClass, java.lang.String name)
Add a protected field to this.- Parameters:
typeClass
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addProtectedField
default FieldDeclaration addProtectedField(java.lang.String type, java.lang.String name)
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
FieldDeclaration
created
-
addProtectedField
default FieldDeclaration addProtectedField(Type type, java.lang.String name)
Add a protected field to this.- Parameters:
type
- the type of the fieldname
- the name of the field- Returns:
- the
FieldDeclaration
created
-
addMethod
default MethodDeclaration addMethod(java.lang.String methodName, Modifier.Keyword... modifiers)
Adds a methods with void return by default to this.- Parameters:
methodName
- the method namemodifiers
- the modifiers likeModifier.Keyword.PUBLIC
- Returns:
- the
MethodDeclaration
created
-
addConstructor
default ConstructorDeclaration addConstructor(Modifier.Keyword... modifiers)
Adds a constructor to this node with members.- Parameters:
modifiers
- the modifiers likeModifier.Keyword.PUBLIC
- Returns:
- the created constructor
-
addInitializer
default BlockStmt addInitializer()
Add an initializer block (InitializerDeclaration
) to this.
-
addStaticInitializer
default BlockStmt addStaticInitializer()
Add a static initializer block (InitializerDeclaration
) to this.
-
getMethodsByName
default java.util.List<MethodDeclaration> getMethodsByName(java.lang.String name)
Try to find aMethodDeclaration
by its name- Parameters:
name
- the name of the method- Returns:
- the methods found (multiple in case of overloading)
-
getMethods
default java.util.List<MethodDeclaration> getMethods()
Find all methods in the members of this node.- Returns:
- the methods found. This list is immutable.
-
getMethodsByParameterTypes
default java.util.List<MethodDeclaration> getMethodsByParameterTypes(java.lang.String... paramTypes)
Try to find aMethodDeclaration
by 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.List
orjava.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
default java.util.List<MethodDeclaration> getMethodsBySignature(java.lang.String name, java.lang.String... paramTypes)
Try to findMethodDeclaration
s 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
default java.util.List<MethodDeclaration> getMethodsByParameterTypes(java.lang.Class<?>... paramTypes)
Try to find aMethodDeclaration
by its parameter types. Note that this is a match in SimpleName, sojava.awt.List
andjava.util.List
are identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passingList.class
to 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.class
to matchvoid foo(Map<Integer,String> myMap, int number)
- Returns:
- the methods found
-
getConstructors
default java.util.List<ConstructorDeclaration> getConstructors()
Find all constructors in the members of this node.- Returns:
- the constructors found. This list is immutable.
-
getDefaultConstructor
default java.util.Optional<ConstructorDeclaration> getDefaultConstructor()
Try to find aConstructorDeclaration
with no parameters.- Returns:
- the constructor found, if any.
-
getConstructorByParameterTypes
default java.util.Optional<ConstructorDeclaration> getConstructorByParameterTypes(java.lang.String... paramTypes)
Try to find aConstructorDeclaration
by 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.List
orjava.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
default java.util.Optional<ConstructorDeclaration> getConstructorByParameterTypes(java.lang.Class<?>... paramTypes)
Try to find aConstructorDeclaration
by its parameter types. Note that this is a match in SimpleName, sojava.awt.List
andjava.util.List
are identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passingList.class
to 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.class
to matchFoo(Map<Integer,String> myMap, int number)
- Returns:
- the constructor found, if any.
-
getFieldByName
default java.util.Optional<FieldDeclaration> getFieldByName(java.lang.String name)
Try to find aFieldDeclaration
by its name- Parameters:
name
- the name of the field- Returns:
- null if not found, the FieldDeclaration otherwise
-
getFields
default java.util.List<FieldDeclaration> 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.
-
-