Class RecordDeclaration
java.lang.Object
com.github.javaparser.ast.Node
com.github.javaparser.ast.body.BodyDeclaration<RecordDeclaration>
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
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Node
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate NodeList<ClassOrInterfaceType> private ReceiverParameterprivate NodeList<TypeParameter> Fields inherited from class Node
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY -
Constructor Summary
ConstructorsConstructorDescriptionRecordDeclaration(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> voidaccept(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.voidifRecordDeclaration(Consumer<RecordDeclaration> action) booleanisFinal()Records are implicitly final, even without the explicit modifier.booleanbooleanbooleanisStatic()Record components are implicitly static when nested (i.e.booleanbooleanresolve()setImplementedTypes(NodeList<ClassOrInterfaceType> implementedTypes) setParameters(NodeList<Parameter> parameters) setReceiverParameter(ReceiverParameter receiverParameter) setTypeParameters(NodeList<TypeParameter> typeParameters) Methods inherited from class TypeDeclaration
addMember, asTypeDeclaration, getCallablesWithSignature, getMembers, getModifiers, getName, ifTypeDeclaration, isNestedType, isTopLevelType, isTypeDeclaration, setMembers, setModifiers, setName, toTypeDeclarationMethods inherited from class 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, toMethodDeclarationMethods inherited from class 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, walkMethods inherited from interface HasParentNode
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOfMethods inherited from interface NodeWithAnnotations
addAndGetAnnotation, addAndGetAnnotation, addAnnotation, addAnnotation, addAnnotation, addMarkerAnnotation, addMarkerAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, addSingleMemberAnnotation, getAnnotation, getAnnotationByClass, getAnnotationByName, isAnnotationPresent, isAnnotationPresent, setAnnotation, tryAddImportToParentCompilationUnitMethods inherited from interface NodeWithFinalModifier
setFinalMethods inherited from interface NodeWithImplements
addImplementedType, addImplementedType, addImplementedType, addImplements, addImplements, getImplementedTypes, setImplementedType, tryAddImportToParentCompilationUnitMethods inherited from interface NodeWithJavadoc
getComment, getJavadoc, getJavadocComment, hasJavaDocComment, removeJavaDocComment, setComment, setJavadocComment, setJavadocComment, setJavadocComment, setJavadocCommentMethods inherited from interface 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, tryAddImportToParentCompilationUnitMethods inherited from interface NodeWithModifiers
addModifier, getAccessSpecifier, getModifiers, hasModifier, removeModifier, setModifier, setModifiers, setModifiersMethods inherited from interface NodeWithParameters
addAndGetParameter, addAndGetParameter, addAndGetParameter, addAndGetParameter, addParameter, addParameter, addParameter, addParameter, getParameter, getParameterByName, getParameterByType, getParameterByType, hasParametersOfType, hasParametersOfType, setParameter, tryAddImportToParentCompilationUnitMethods inherited from interface NodeWithPrivateModifier
isPrivate, setPrivateMethods inherited from interface NodeWithProtectedModifier
isProtected, setProtectedMethods inherited from interface NodeWithPublicModifier
isPublic, setPublicMethods inherited from interface NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRangeMethods inherited from interface NodeWithSimpleName
getNameAsExpression, getNameAsString, setNameMethods inherited from interface NodeWithStaticModifier
setStaticMethods inherited from interface NodeWithStrictfpModifier
isStrictfp, setStrictfpMethods inherited from interface NodeWithTypeParameters
addTypeParameter, addTypeParameter, getTypeParameter, isGeneric, setTypeParameter
-
Field Details
-
typeParameters
-
implementedTypes
-
receiverParameter
-
parameters
-
-
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: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
Description copied from interface:VisitableAccept method for visitor support. -
getImplementedTypes
- Specified by:
getImplementedTypesin interfaceNodeWithImplements<RecordDeclaration>
-
getTypeParameters
- Specified by:
getTypeParametersin interfaceNodeWithTypeParameters<RecordDeclaration>
-
setImplementedTypes
- Specified by:
setImplementedTypesin interfaceNodeWithImplements<RecordDeclaration>
-
setTypeParameters
- Specified by:
setTypeParametersin interfaceNodeWithTypeParameters<RecordDeclaration>
-
isLocalRecordDeclaration
public boolean isLocalRecordDeclaration()- Returns:
- is this class's parent a LocalRecordDeclarationStmt ?
-
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:
-
resolve
- Specified by:
resolvein interfaceResolvable<ResolvedReferenceTypeDeclaration>- Specified by:
resolvein classTypeDeclaration<RecordDeclaration>
-
isRecordDeclaration
public boolean isRecordDeclaration()- Overrides:
isRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
asRecordDeclaration
- Overrides:
asRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
toRecordDeclaration
- Overrides:
toRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
ifRecordDeclaration
- Overrides:
ifRecordDeclarationin classBodyDeclaration<RecordDeclaration>
-
remove
- Overrides:
removein classTypeDeclaration<RecordDeclaration>
-
replace
- Overrides:
replacein classTypeDeclaration<RecordDeclaration>
-
clone
- Overrides:
clonein classTypeDeclaration<RecordDeclaration>
-
getMetaModel
- Overrides:
getMetaModelin 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:
getParametersin interfaceNodeWithParameters<RecordDeclaration>- See Also:
-
setParameters
- Specified by:
setParametersin 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 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
- Returns:
- Only the "compact" constructors within this record,
not "normal" constructors (which are obtainable via
NodeWithMembers.getConstructors()).
-