Top Description Methods
com.github.javaparser.ast.nodeTypes

public Interface NodeWithMembers<N extends Node>

extends NodeWithSimpleName<N extends Node>
Known Direct Implementers
com.github.javaparser.ast.body.TypeDeclaration
Imports
com.github.javaparser.ast.Modifier, .Node, .NodeList, com.github.javaparser.ast.body.*, com.github.javaparser.ast.expr.Expression, com.github.javaparser.ast.stmt.BlockStmt, com.github.javaparser.ast.type.Type, .VoidType, java.util.List, .Optional

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 TypeMethod and Description
public default ConstructorDeclaration

Returns:

the created constructor
addConstructor
(Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Adds a constructor to this node with members.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addField
(Class<?>
the type of the field
typeClass
,
String
the name of the field
name
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this and automatically add the import of the type if needed

public default FieldDeclaration

Returns:

the FieldDeclaration created
addField
(String
the type of the field
type
,
String
the name of the field
name
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addField
(Type
the type of the field
type
,
String
the name of the field
name
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addFieldWithInitializer
(Class<?>
the type of the field
typeClass
,
String
the name of the field
name
,
Expression
the initializer of the field
initializer
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this and automatically add the import of the type if needed

public default FieldDeclaration

Returns:

the FieldDeclaration created
addFieldWithInitializer
(String
the type of the field
type
,
String
the name of the field
name
,
Expression
the initializer of the field
initializer
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addFieldWithInitializer
(Type
the type of the field
type
,
String
the name of the field
name
,
Expression
the initializer of the field
initializer
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Add a field to this.

public default BlockStmt
addInitializer()

Add an initializer block (InitializerDeclaration) to this.

public default N
public default MethodDeclaration

Returns:

the MethodDeclaration created
addMethod
(String
the method name
methodName
,
Modifier.Keyword...
the modifiers like Modifier.Keyword#PUBLIC
modifiers
)

Adds a methods with void return by default to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPrivateField
(Class<?>
the type of the field
typeClass
,
String
the name of the field
name
)

Add a private field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPrivateField
(String
the type of the field
type
,
String
the name of the field
name
)

Add a private field to this and automatically add the import of the type if needed.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPrivateField
(Type
the type of the field
type
,
String
the name of the field
name
)

Add a private field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addProtectedField
(Class<?>
the type of the field
typeClass
,
String
the name of the field
name
)

Add a protected field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addProtectedField
(String
the type of the field
type
,
String
the name of the field
name
)

Add a protected field to this and automatically add the import of the type if needed.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addProtectedField
(Type
the type of the field
type
,
String
the name of the field
name
)

Add a protected field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPublicField
(Class<?>
the type of the field
typeClass
,
String
the name of the field
name
)

Add a public field to this.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPublicField
(String
the type of the field
type
,
String
the name of the field
name
)

Add a public field to this and automatically add the import of the type if needed.

public default FieldDeclaration

Returns:

the FieldDeclaration created
addPublicField
(Type
the type of the field
type
,
String
the name of the field
name
)

Add a public field to this.

public default BlockStmt
addStaticInitializer()

Add a static initializer block (InitializerDeclaration) to this.

public default Optional<ConstructorDeclaration>

Returns:

the constructor found, if any.
getConstructorByParameterTypes
(String...
the types of parameters like "Map<Integer, String>", "int" to match Foo(Map<Integer,String> myMap, int number)
paramTypes
)

Try to find a ConstructorDeclaration by its parameter types.

public default Optional<ConstructorDeclaration>

Returns:

the constructor found, if any.
getConstructorByParameterTypes
(Class<?>...
the types of parameters like Map.class, int.class to match Foo(Map<Integer,String> myMap, int number)
paramTypes
)

Try to find a ConstructorDeclaration by its parameter types.

public default List<ConstructorDeclaration>

Returns:

the constructors found. This list is immutable.
getConstructors
()

Find all constructors in the members of this node.

public default Optional<ConstructorDeclaration>

Returns:

the constructor found, if any.
getDefaultConstructor
()

Try to find a ConstructorDeclaration with no parameters.

public default Optional<FieldDeclaration>

Returns:

null if not found, the FieldDeclaration otherwise
getFieldByName
(String
the name of the field
name
)

Try to find a FieldDeclaration by its name

public default List<FieldDeclaration>

Returns:

the fields found. This list is immutable.
getFields
()

Find all fields in the members of this node.

public default BodyDeclaration<?>
getMember(int i)

public NodeList<BodyDeclaration<?>>

Returns:

all members inside the braces of this node, like fields, methods, nested types, etc.
getMembers
()

public default List<MethodDeclaration>

Returns:

the methods found. This list is immutable.
getMethods
()

Find all methods in the members of this node.

public default List<MethodDeclaration>

Returns:

the methods found (multiple in case of overloading)
getMethodsByName
(String
the name of the method
name
)

Try to find a MethodDeclaration by its name

public default List<MethodDeclaration>

Returns:

the methods found
getMethodsByParameterTypes
(String...
the types of parameters like "Map<Integer, String>", "int" to match void foo(Map<Integer,String> myMap, int number)
paramTypes
)

Try to find a MethodDeclaration by its parameter types.

public default List<MethodDeclaration>

Returns:

the methods found
getMethodsByParameterTypes
(Class<?>...
the types of parameters like Map.class, int.class to match void foo(Map<Integer,String> myMap, int number)
paramTypes
)

Try to find a MethodDeclaration by its parameter types.

public default List<MethodDeclaration>

Returns:

the methods found
getMethodsBySignature
(String name, String...
the types of parameters like "Map<Integer, String>", "int" to match void foo(Map<Integer,String> myMap, int number)
paramTypes
)

Try to find MethodDeclarations by their name and parameter types.

public default boolean

Returns:

true if there are no members contained in this node.
isEmpty
()

public default N
setMember(int i, BodyDeclaration<?> member)

public N
public void
Inherited from com.github.javaparser.ast.nodeTypes.NodeWithSimpleName:
getNamegetNameAsExpressiongetNameAsStringsetNamesetName

Method Detail

addConstructorback to summary
public default ConstructorDeclaration addConstructor(Modifier.Keyword... modifiers)

Adds a constructor to this node with members.

Parameters
modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:ConstructorDeclaration

the created constructor

addFieldback to summary
public default FieldDeclaration addField(Class<?> typeClass, String name, Modifier.Keyword... modifiers)

Add a field to this and automatically add the import of the type if needed

Parameters
typeClass:Class<?>

the type of the field

name:String

the name of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addFieldback to summary
public default FieldDeclaration addField(String type, String name, Modifier.Keyword... modifiers)

Add a field to this.

Parameters
type:String

the type of the field

name:String

the name of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addFieldback to summary
public default FieldDeclaration addField(Type type, String name, Modifier.Keyword... modifiers)

Add a field to this.

Parameters
type:Type

the type of the field

name:String

the name of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addFieldWithInitializerback to summary
public 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:Class<?>

the type of the field

name:String

the name of the field

initializer:Expression

the initializer of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addFieldWithInitializerback to summary
public default FieldDeclaration addFieldWithInitializer(String type, String name, Expression initializer, Modifier.Keyword... modifiers)

Add a field to this.

Parameters
type:String

the type of the field

name:String

the name of the field

initializer:Expression

the initializer of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addFieldWithInitializerback to summary
public default FieldDeclaration addFieldWithInitializer(Type type, String name, Expression initializer, Modifier.Keyword... modifiers)

Add a field to this.

Parameters
type:Type

the type of the field

name:String

the name of the field

initializer:Expression

the initializer of the field

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:FieldDeclaration

the FieldDeclaration created

addInitializerback to summary
public default BlockStmt addInitializer()

Add an initializer block (InitializerDeclaration) to this.

addMemberback to summary
public default N addMember(BodyDeclaration<?> member)
Annotations
@SuppressWarnings:unchecked
addMethodback to summary
public default MethodDeclaration addMethod(String methodName, Modifier.Keyword... modifiers)

Adds a methods with void return by default to this.

Parameters
methodName:String

the method name

modifiers:Modifier.Keyword[]

the modifiers like Modifier.Keyword#PUBLIC

Returns:MethodDeclaration

the MethodDeclaration created

addPrivateFieldback to summary
public default FieldDeclaration addPrivateField(Class<?> typeClass, String name)

Add a private field to this.

Parameters
typeClass:Class<?>

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addPrivateFieldback to summary
public default FieldDeclaration addPrivateField(String type, String name)

Add a private field to this and automatically add the import of the type if needed.

Parameters
type:String

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addPrivateFieldback to summary
public default FieldDeclaration addPrivateField(Type type, String name)

Add a private field to this.

Parameters
type:Type

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addProtectedFieldback to summary
public default FieldDeclaration addProtectedField(Class<?> typeClass, String name)

Add a protected field to this.

Parameters
typeClass:Class<?>

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addProtectedFieldback to summary
public default FieldDeclaration addProtectedField(String type, String name)

Add a protected field to this and automatically add the import of the type if needed.

Parameters
type:String

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addProtectedFieldback to summary
public default FieldDeclaration addProtectedField(Type type, String name)

Add a protected field to this.

Parameters
type:Type

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addPublicFieldback to summary
public default FieldDeclaration addPublicField(Class<?> typeClass, String name)

Add a public field to this.

Parameters
typeClass:Class<?>

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addPublicFieldback to summary
public default FieldDeclaration addPublicField(String type, String name)

Add a public field to this and automatically add the import of the type if needed.

Parameters
type:String

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addPublicFieldback to summary
public default FieldDeclaration addPublicField(Type type, String name)

Add a public field to this.

Parameters
type:Type

the type of the field

name:String

the name of the field

Returns:FieldDeclaration

the FieldDeclaration created

addStaticInitializerback to summary
public default BlockStmt addStaticInitializer()

Add a static initializer block (InitializerDeclaration) to this.

getConstructorByParameterTypesback to summary
public default Optional<ConstructorDeclaration> getConstructorByParameterTypes(String... paramTypes)

Try to find a ConstructorDeclaration 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 as List, but not for a constructor with exactly one parameter whose type is declared as java.util.List or java.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 as java.util.List, but not for a constructor whose type is declared as List. Similarly, note that generics are matched as well: If there is a constructor that has a parameter declared as List<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:String[]

the types of parameters like "Map<Integer, String>", "int" to match Foo(Map<Integer,String> myMap, int number)

Returns:Optional<ConstructorDeclaration>

the constructor found, if any.

getConstructorByParameterTypesback to summary
public default Optional<ConstructorDeclaration> getConstructorByParameterTypes(Class<?>... paramTypes)

Try to find a ConstructorDeclaration by its parameter types. Note that this is a match in SimpleName, so java.awt.List and java.util.List are identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passing List.class to this method will search for a constructor that has exactly one parameter whose type is named List, regardless of whether the parameter type is declared without generics as List, or with generics as List<String>, or List<Integer>, etc.

Parameters
paramTypes:Class<?>[]

the types of parameters like Map.class, int.class to match Foo(Map<Integer,String> myMap, int number)

Returns:Optional<ConstructorDeclaration>

the constructor found, if any.

getConstructorsback to summary
public default List<ConstructorDeclaration> getConstructors()

Find all constructors in the members of this node.

Returns:List<ConstructorDeclaration>

the constructors found. This list is immutable.

getDefaultConstructorback to summary
public default Optional<ConstructorDeclaration> getDefaultConstructor()

Try to find a ConstructorDeclaration with no parameters.

Returns:Optional<ConstructorDeclaration>

the constructor found, if any.

getFieldByNameback to summary
public default Optional<FieldDeclaration> getFieldByName(String name)

Try to find a FieldDeclaration by its name

Parameters
name:String

the name of the field

Returns:Optional<FieldDeclaration>

null if not found, the FieldDeclaration otherwise

getFieldsback to summary
public default List<FieldDeclaration> getFields()

Find all fields in the members of this node.

Returns:List<FieldDeclaration>

the fields found. This list is immutable.

getMemberback to summary
public default BodyDeclaration<?> getMember(int i)
getMembersback to summary
public NodeList<BodyDeclaration<?>> getMembers()
Returns:NodeList<BodyDeclaration<?>>

all members inside the braces of this node, like fields, methods, nested types, etc.

getMethodsback to summary
public default List<MethodDeclaration> getMethods()

Find all methods in the members of this node.

Returns:List<MethodDeclaration>

the methods found. This list is immutable.

getMethodsByNameback to summary
public default List<MethodDeclaration> getMethodsByName(String name)

Try to find a MethodDeclaration by its name

Parameters
name:String

the name of the method

Returns:List<MethodDeclaration>

the methods found (multiple in case of overloading)

getMethodsByParameterTypesback to summary
public default List<MethodDeclaration> getMethodsByParameterTypes(String... paramTypes)

Try to find a MethodDeclaration 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 as List, but not methods with exactly one parameter whose type is declared as java.util.List or java.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 as java.util.List, but not if the parameter type is declared as List. Similarly, note that generics are matched as well: If there is a method that has a parameter declared as List<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:String[]

the types of parameters like "Map<Integer, String>", "int" to match void foo(Map<Integer,String> myMap, int number)

Returns:List<MethodDeclaration>

the methods found

getMethodsByParameterTypesback to summary
public default List<MethodDeclaration> getMethodsByParameterTypes(Class<?>... paramTypes)

Try to find a MethodDeclaration by its parameter types. Note that this is a match in SimpleName, so java.awt.List and java.util.List are identical to this algorithm. In addition, note that it is the erasure of each type which is considered, so passing List.class to this method will return all methods that have exactly one parameter whose type is named List, regardless of whether the parameter type is declared without generics as List, or with generics as List<String>, or List<Integer>, etc.

Parameters
paramTypes:Class<?>[]

the types of parameters like Map.class, int.class to match void foo(Map<Integer,String> myMap, int number)

Returns:List<MethodDeclaration>

the methods found

getMethodsBySignatureback to summary
public default List<MethodDeclaration> getMethodsBySignature(String name, String... paramTypes)

Try to find MethodDeclarations by their name and parameter types. Parameter types are matched exactly as in the case of getMethodsByParameterTypes(String...).

Parameters
paramTypes:String[]

the types of parameters like "Map<Integer, String>", "int" to match void foo(Map<Integer,String> myMap, int number)

Returns:List<MethodDeclaration>

the methods found

isEmptyback to summary
public default boolean isEmpty()
Returns:boolean

true if there are no members contained in this node.

setMemberback to summary
public default N setMember(int i, BodyDeclaration<?> member)
Annotations
@SuppressWarnings:unchecked
setMembersback to summary
public N setMembers(NodeList<BodyDeclaration<?>> members)
tryAddImportToParentCompilationUnitback to summary
public void tryAddImportToParentCompilationUnit(Class<?> clazz)