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
Constructors Constructor Description RecordDeclaration()RecordDeclaration(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
-
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, findByRange, findCompilationUnit, findData, 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 Detail
-
RecordDeclaration
public 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 Detail
-
accept
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Description copied from interface:VisitableAccept method for visitor support.- Specified by:
acceptin 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
public <A> void accept(VoidVisitor<A> v, A arg)
Description copied from interface:VisitableAccept method for visitor support.
-
getImplementedTypes
public NodeList<ClassOrInterfaceType> getImplementedTypes()
- Specified by:
getImplementedTypesin interfaceNodeWithImplements<RecordDeclaration>
-
getTypeParameters
public NodeList<TypeParameter> getTypeParameters()
- Specified by:
getTypeParametersin interfaceNodeWithTypeParameters<RecordDeclaration>
-
setImplementedTypes
public RecordDeclaration setImplementedTypes(NodeList<ClassOrInterfaceType> implementedTypes)
- Specified by:
setImplementedTypesin interfaceNodeWithImplements<RecordDeclaration>
-
setTypeParameters
public RecordDeclaration setTypeParameters(NodeList<TypeParameter> typeParameters)
- Specified by:
setTypeParametersin interfaceNodeWithTypeParameters<RecordDeclaration>
-
isLocalRecordDeclaration
public boolean isLocalRecordDeclaration()
- Returns:
- is this class's parent a LocalRecordDeclarationStmt ?
-
getFullyQualifiedName
public Optional<String> getFullyQualifiedName()
Description copied from class:TypeDeclarationReturns 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:
getFullyQualifiedNamein classTypeDeclaration<RecordDeclaration>- See Also:
LocalClassDeclarationStmt,LocalRecordDeclarationStmt
-
resolve
public ResolvedReferenceTypeDeclaration resolve()
- Specified by:
resolvein interfaceResolvable<ResolvedReferenceTypeDeclaration>- Specified by:
resolvein classTypeDeclaration<RecordDeclaration>
-
isRecordDeclaration
public boolean isRecordDeclaration()
- Overrides:
isRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
asRecordDeclaration
public RecordDeclaration asRecordDeclaration()
- Overrides:
asRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
toRecordDeclaration
public Optional<RecordDeclaration> toRecordDeclaration()
- Overrides:
toRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
ifRecordDeclaration
public void ifRecordDeclaration(Consumer<RecordDeclaration> action)
- Overrides:
ifRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
remove
public boolean remove(Node node)
- Overrides:
removein classTypeDeclaration<RecordDeclaration>
-
replace
public boolean replace(Node node, Node replacementNode)
- Overrides:
replacein classTypeDeclaration<RecordDeclaration>
-
clone
public RecordDeclaration clone()
- Overrides:
clonein classTypeDeclaration<RecordDeclaration>
-
getMetaModel
public RecordDeclarationMetaModel getMetaModel()
- Overrides:
getMetaModelin classTypeDeclaration<RecordDeclaration>- Returns:
- get JavaParser specific node introspection information.
-
getParameters
public NodeList<Parameter> 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:
getParametersin interfaceNodeWithParameters<RecordDeclaration>- See Also:
- https://openjdk.java.net/jeps/359
-
setParameters
public RecordDeclaration setParameters(NodeList<Parameter> parameters)
- Specified by:
setParametersin interfaceNodeWithParameters<RecordDeclaration>
-
getReceiverParameter
public Optional<ReceiverParameter> getReceiverParameter()
-
setReceiverParameter
public RecordDeclaration setReceiverParameter(ReceiverParameter receiverParameter)
-
removeReceiverParameter
public RecordDeclaration 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 keywordfinalis explicitly added to this parameter, you should usenode.hasModifier(Modifier.Keyword.FINAL)- Specified by:
isFinalin 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:
isStaticin interfaceNodeWithStaticModifier<RecordDeclaration>- Returns:
- True if the record declaration is nested, otherwise use the default method implementation.
-
getCompactConstructors
public List<CompactConstructorDeclaration> getCompactConstructors()
- Returns:
- Only the "compact" constructors within this record,
not "normal" constructors (which are obtainable via
NodeWithMembers.getConstructors()).
-
-