Package com.github.javaparser.ast.body
Class RecordDeclaration
java.lang.Object
com.github.javaparser.ast.Node
com.github.javaparser.ast.body.BodyDeclaration<T>
com.github.javaparser.ast.body.TypeDeclaration<RecordDeclaration>
com.github.javaparser.ast.body.RecordDeclaration
- All Implemented Interfaces:
NodeWithAccessModifiers<RecordDeclaration>
,NodeWithFinalModifier<RecordDeclaration>
,NodeWithPrivateModifier<RecordDeclaration>
,NodeWithProtectedModifier<RecordDeclaration>
,NodeWithPublicModifier<RecordDeclaration>
,NodeWithStaticModifier<RecordDeclaration>
,NodeWithStrictfpModifier<RecordDeclaration>
,NodeWithAnnotations<RecordDeclaration>
,NodeWithImplements<RecordDeclaration>
,NodeWithJavadoc<RecordDeclaration>
,NodeWithMembers<RecordDeclaration>
,NodeWithModifiers<RecordDeclaration>
,NodeWithParameters<RecordDeclaration>
,NodeWithRange<Node>
,NodeWithSimpleName<RecordDeclaration>
,NodeWithTokenRange<Node>
,NodeWithTypeParameters<RecordDeclaration>
,Observable
,Visitable
,HasParentNode<Node>
,Resolvable<ResolvedReferenceTypeDeclaration>
,Cloneable
public class RecordDeclaration
extends TypeDeclaration<RecordDeclaration>
implements NodeWithParameters<RecordDeclaration>, NodeWithImplements<RecordDeclaration>, NodeWithTypeParameters<RecordDeclaration>, NodeWithFinalModifier<RecordDeclaration>, Resolvable<ResolvedReferenceTypeDeclaration>
The record declaration
WARNING: This implementation is subject to change.Java 1.0 to 13
Not available.Java 14 (preview), Java 15 (2nd preview), Java 16
A definition of a record.
record X(...) { ... }
Note that the syntax of records is substantively different to standard classes/interfaces/enums. Specifically, note that record header contains the component declarations - where a "component" is defined as a non-static field.
This is in contrast to "normal" classes, where fields declarations are within the class body (optionally then initialised within a constructor.
Also note that the constructor for records does not accept any parameters.
Consider this example from https://openjdk.java.net/jeps/359
record Range(int lo, int hi) {
public Range {
if (lo > hi)
throw new IllegalArgumentException(String.format("(%d,%d)",lo,hi));
}
}
To access these non-static field declarations, use getParameters()
- Since:
- 3.22.0
- Author:
- Roger Howell
- See Also:
- https://openjdk.java.net/jeps/395, JLS 8.10 - Record Classes
-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.javaparser.ast.Node
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal
-
Field Summary
Fields inherited from class com.github.javaparser.ast.Node
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY
-
Constructor Summary
ConstructorDescriptionRecordDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Parameter> parameters, NodeList<TypeParameter> typeParameters, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<BodyDeclaration<?>> members, ReceiverParameter receiverParameter)
RecordDeclaration(NodeList<Modifier> modifiers, String name)
RecordDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Parameter> parameters, NodeList<TypeParameter> typeParameters, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<BodyDeclaration<?>> members, ReceiverParameter receiverParameter)
This constructor is used by the parser and is considered private. -
Method Summary
Modifier and TypeMethodDescription<R, A> R
accept(GenericVisitor<R,A> v, A arg)
Accept method for visitor support.<A> void
accept(VoidVisitor<A> v, A arg)
Accept method for visitor support.clone()
Returns the fully qualified name of this type, derived only from information available in this compilation unit.Type declarations do not normally have parameters - e.g.void
ifRecordDeclaration(Consumer<RecordDeclaration> action)
boolean
isFinal()
Records are implicitly final, even without the explicit modifier.boolean
boolean
boolean
isStatic()
Record components are implicitly static when nested (i.e.boolean
boolean
resolve()
setImplementedTypes(NodeList<ClassOrInterfaceType> implementedTypes)
setParameters(NodeList<Parameter> parameters)
setReceiverParameter(ReceiverParameter receiverParameter)
setTypeParameters(NodeList<TypeParameter> typeParameters)
Methods inherited from class com.github.javaparser.ast.body.TypeDeclaration
addMember, asTypeDeclaration, getCallablesWithSignature, getMembers, getModifiers, getName, ifTypeDeclaration, isNestedType, isTopLevelType, isTypeDeclaration, setMembers, setModifiers, setName, toTypeDeclaration
Methods inherited from class com.github.javaparser.ast.body.BodyDeclaration
asAnnotationDeclaration, asAnnotationMemberDeclaration, asCallableDeclaration, asClassOrInterfaceDeclaration, asCompactConstructorDeclaration, asConstructorDeclaration, asEnumConstantDeclaration, asEnumDeclaration, asFieldDeclaration, asInitializerDeclaration, asMethodDeclaration, getAnnotations, ifAnnotationDeclaration, ifAnnotationMemberDeclaration, ifCallableDeclaration, ifClassOrInterfaceDeclaration, ifCompactConstructorDeclaration, ifConstructorDeclaration, ifEnumConstantDeclaration, ifEnumDeclaration, ifFieldDeclaration, ifInitializerDeclaration, ifMethodDeclaration, isAnnotationDeclaration, isAnnotationMemberDeclaration, isCallableDeclaration, isClassOrInterfaceDeclaration, isCompactConstructorDeclaration, isConstructorDeclaration, isEnumConstantDeclaration, isEnumDeclaration, isFieldDeclaration, isInitializerDeclaration, isMethodDeclaration, setAnnotations, toAnnotationDeclaration, toAnnotationMemberDeclaration, toCallableDeclaration, toClassOrInterfaceDeclaration, toCompactConstructorDeclaration, toConstructorDeclaration, toEnumConstantDeclaration, toEnumDeclaration, toFieldDeclaration, toInitializerDeclaration, toMethodDeclaration
Methods inherited from class com.github.javaparser.ast.Node
addOrphanComment, containsData, createDefaultPrinter, createDefaultPrinter, customInitialization, equals, findAll, findAll, findAll, findCompilationUnit, findFirst, findFirst, findFirst, findRootNode, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getDataKeys, getDefaultPrinterConfiguration, getLineEndingStyle, getLineEndingStyleOrDefault, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getPrinter, getPrinter, getRange, getSymbolResolver, getTokenRange, hashCode, hasScope, isAncestorOf, isPhantom, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeData, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, stream, stream, toString, toString, tryAddImportToParentCompilationUnit, unregister, walk, walk, walk
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.github.javaparser.HasParentNode
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOf
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithAnnotations
addAndGetAnnotation, addAndGetAnnotation, addAnnotation, addAnnotation, addAnnotation, addMarkerAnnotation, addMarkerAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, getAnnotation, getAnnotationByClass, getAnnotationByName, isAnnotationPresent, isAnnotationPresent, setAnnotation, tryAddImportToParentCompilationUnit
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier
setFinal
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithImplements
addImplementedType, addImplementedType, addImplementedType, addImplements, addImplements, getImplementedTypes, setImplementedType, tryAddImportToParentCompilationUnit
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithJavadoc
getComment, getJavadoc, getJavadocComment, hasJavaDocComment, removeJavaDocComment, setComment, setJavadocComment, setJavadocComment, setJavadocComment, setJavadocComment
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithMembers
addConstructor, addField, addField, addField, addFieldWithInitializer, addFieldWithInitializer, addFieldWithInitializer, addInitializer, addMethod, addPrivateField, addPrivateField, addPrivateField, addProtectedField, addProtectedField, addProtectedField, addPublicField, addPublicField, addPublicField, addStaticInitializer, getConstructorByParameterTypes, getConstructorByParameterTypes, getConstructors, getDefaultConstructor, getFieldByName, getFields, getMember, getMethods, getMethodsByName, getMethodsByParameterTypes, getMethodsByParameterTypes, getMethodsBySignature, isEmpty, setMember, tryAddImportToParentCompilationUnit
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithModifiers
addModifier, getAccessSpecifier, getModifiers, hasModifier, removeModifier, setModifier, setModifiers, setModifiers
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithParameters
addAndGetParameter, addAndGetParameter, addAndGetParameter, addAndGetParameter, addParameter, addParameter, addParameter, addParameter, getParameter, getParameterByName, getParameterByType, getParameterByType, hasParametersOfType, hasParametersOfType, setParameter, tryAddImportToParentCompilationUnit
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithPrivateModifier
isPrivate, setPrivate
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithProtectedModifier
isProtected, setProtected
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithPublicModifier
isPublic, setPublic
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithSimpleName
getNameAsExpression, getNameAsString, setName
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithStaticModifier
setStatic
Methods inherited from interface com.github.javaparser.ast.nodeTypes.modifiers.NodeWithStrictfpModifier
isStrictfp, setStrictfp
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters
addTypeParameter, addTypeParameter, getTypeParameter, isGeneric, setTypeParameter
-
Constructor Details
-
RecordDeclaration
public RecordDeclaration() -
RecordDeclaration
-
RecordDeclaration
public RecordDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Parameter> parameters, NodeList<TypeParameter> typeParameters, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<BodyDeclaration<?>> members, ReceiverParameter receiverParameter) -
RecordDeclaration
public RecordDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Parameter> parameters, NodeList<TypeParameter> typeParameters, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<BodyDeclaration<?>> members, ReceiverParameter receiverParameter)This constructor is used by the parser and is considered private.
-
-
Method Details
-
accept
Description copied from interface:Visitable
Accept method for visitor support.- Specified by:
accept
in interfaceVisitable
- Type Parameters:
R
- the type of the return value of the visitorA
- the type the user argument passed to the visitor- Parameters:
v
- the visitor implementationarg
- the argument passed to the visitor (of type A)- Returns:
- the result of the visit (of type R)
-
accept
Description copied from interface:Visitable
Accept method for visitor support. -
getImplementedTypes
- Specified by:
getImplementedTypes
in interfaceNodeWithImplements<RecordDeclaration>
-
getTypeParameters
- Specified by:
getTypeParameters
in interfaceNodeWithTypeParameters<RecordDeclaration>
-
setImplementedTypes
- Specified by:
setImplementedTypes
in interfaceNodeWithImplements<RecordDeclaration>
-
setTypeParameters
- Specified by:
setTypeParameters
in interfaceNodeWithTypeParameters<RecordDeclaration>
-
isLocalRecordDeclaration
public boolean isLocalRecordDeclaration()- Returns:
- is this class's parent a LocalRecordDeclarationStmt ?
-
getFullyQualifiedName
Description copied from class:TypeDeclaration
Returns the fully qualified name of this type, derived only from information available in this compilation unit. (So no symbol solving happens.) If the declared type is a local class declaration, it will return Optional.empty(). If the declared type is a local record declaration, it will return Optional.empty(). If the declared type is not contained in a compilation unit, it will return Optional.empty().- Overrides:
getFullyQualifiedName
in classTypeDeclaration<RecordDeclaration>
- See Also:
LocalClassDeclarationStmt
,LocalRecordDeclarationStmt
-
resolve
- Specified by:
resolve
in interfaceResolvable<ResolvedReferenceTypeDeclaration>
- Specified by:
resolve
in classTypeDeclaration<RecordDeclaration>
-
isRecordDeclaration
public boolean isRecordDeclaration()- Overrides:
isRecordDeclaration
in classBodyDeclaration<RecordDeclaration>
-
asRecordDeclaration
- Overrides:
asRecordDeclaration
in classBodyDeclaration<RecordDeclaration>
-
toRecordDeclaration
- Overrides:
toRecordDeclaration
in classBodyDeclaration<RecordDeclaration>
-
ifRecordDeclaration
- Overrides:
ifRecordDeclaration
in classBodyDeclaration<RecordDeclaration>
-
remove
- Overrides:
remove
in classTypeDeclaration<RecordDeclaration>
-
replace
- Overrides:
replace
in classTypeDeclaration<RecordDeclaration>
-
clone
- Overrides:
clone
in classTypeDeclaration<RecordDeclaration>
-
getMetaModel
- Overrides:
getMetaModel
in classTypeDeclaration<RecordDeclaration>
- Returns:
- get JavaParser specific node introspection information.
-
getParameters
Type declarations do not normally have parameters - e.g.class X {}
andenum X {}
. Records are different, where the record declaration can include parameters e.g.record X(int a) {}
. Additionally, note that the constructor for a record does not allow the declaration of parameters. See the JEP for details.- Specified by:
getParameters
in interfaceNodeWithParameters<RecordDeclaration>
- See Also:
- https://openjdk.java.net/jeps/359
-
setParameters
- Specified by:
setParameters
in interfaceNodeWithParameters<RecordDeclaration>
-
getReceiverParameter
-
setReceiverParameter
-
removeReceiverParameter
-
isFinal
public boolean isFinal()Records are implicitly final, even without the explicit modifier. https://openjdk.java.net/jeps/359#Restrictions-on-records If wanting to find out if the keywordfinal
is explicitly added to this parameter, you should usenode.hasModifier(Modifier.Keyword.FINAL)
- Specified by:
isFinal
in interfaceNodeWithFinalModifier<RecordDeclaration>
- Returns:
- always true -- Records are always implicitly final, therefore can never not be final.
-
isStatic
public boolean isStatic()Record components are implicitly static when nested (i.e. when the parent isn't a compilation unit). https://openjdk.java.net/jeps/359#Restrictions-on-records- Specified by:
isStatic
in interfaceNodeWithStaticModifier<RecordDeclaration>
- Returns:
- True if the record declaration is nested, otherwise use the default method implementation.
-
getCompactConstructors
- Returns:
- Only the "compact" constructors within this record,
not "normal" constructors (which are obtainable via
NodeWithMembers.getConstructors()
).
-