From 5f55bfe80cad6b5e27bb773e9e009e144d9cdb00 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Tue, 5 Oct 2021 14:30:58 +0200 Subject: [PATCH] add support for records to the language model --- .../lang/model/declarations/ClassInfo.java | 21 ++++++- .../model/declarations/DeclarationInfo.java | 20 +++++++ .../declarations/RecordComponentInfo.java | 57 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/jakarta/enterprise/lang/model/declarations/RecordComponentInfo.java diff --git a/api/src/main/java/jakarta/enterprise/lang/model/declarations/ClassInfo.java b/api/src/main/java/jakarta/enterprise/lang/model/declarations/ClassInfo.java index 28bf834c..1ec5f339 100644 --- a/api/src/main/java/jakarta/enterprise/lang/model/declarations/ClassInfo.java +++ b/api/src/main/java/jakarta/enterprise/lang/model/declarations/ClassInfo.java @@ -7,12 +7,13 @@ import java.util.List; /** - * A class. Four kinds of classes are distinguished: + * A class. Five kinds of classes are distinguished: * * * Classes are represented as isolated units. That is, if this class is nested, it is not possible @@ -25,7 +26,6 @@ * * @since 4.0 */ -// TODO maybe support records already? public interface ClassInfo extends DeclarationInfo { /** * Returns the binary name of this class, as defined by The Java™ Language Specification; @@ -93,7 +93,7 @@ public interface ClassInfo extends DeclarationInfo { /** * Returns whether this class is a plain class. That is, not an interface, - * not an enum, and not an annotation. + * not an enum, not an annotation, and not a record. * * @return whether this class is a plain class */ @@ -120,6 +120,13 @@ public interface ClassInfo extends DeclarationInfo { */ boolean isAnnotation(); + /** + * Returns whether this class is a record. + * + * @return whether this class is a record + */ + boolean isRecord(); + /** * Returns whether this class is {@code abstract}. * @@ -176,6 +183,14 @@ public interface ClassInfo extends DeclarationInfo { */ Collection fields(); + /** + * Returns a collection of {@linkplain RecordComponentInfo record components} declared in this class. + * If this class is not a record, returns an empty collection. + * + * @return immutable collection of record components, never {@code nul} + */ + Collection recordComponents(); + // --- @Override diff --git a/api/src/main/java/jakarta/enterprise/lang/model/declarations/DeclarationInfo.java b/api/src/main/java/jakarta/enterprise/lang/model/declarations/DeclarationInfo.java index 7536ac06..9990f341 100644 --- a/api/src/main/java/jakarta/enterprise/lang/model/declarations/DeclarationInfo.java +++ b/api/src/main/java/jakarta/enterprise/lang/model/declarations/DeclarationInfo.java @@ -44,6 +44,7 @@ enum Kind { METHOD, PARAMETER, FIELD, + RECORD_COMPONENT, } /** @@ -98,6 +99,15 @@ default boolean isField() { return kind() == Kind.FIELD; } + /** + * Returns whether this declaration is a {@linkplain RecordComponentInfo record component}. + * + * @return {@code true} if this is a record component, {@code false} otherwise + */ + default boolean isRecordComponent() { + return kind() == Kind.RECORD_COMPONENT; + } + /** * Returns this declaration as a {@linkplain PackageInfo package}. * @@ -147,4 +157,14 @@ default ParameterInfo asParameter() { default FieldInfo asField() { throw new IllegalStateException("Not a field"); } + + /** + * Returns this declaration as a {@linkplain RecordComponentInfo record component}. + * + * @return this record component, never {@code null} + * @throws IllegalStateException if {@link #isRecordComponent()} returns {@code false} + */ + default RecordComponentInfo asRecordComponent() { + throw new IllegalStateException("Not a record component"); + } } diff --git a/api/src/main/java/jakarta/enterprise/lang/model/declarations/RecordComponentInfo.java b/api/src/main/java/jakarta/enterprise/lang/model/declarations/RecordComponentInfo.java new file mode 100644 index 00000000..59195eb2 --- /dev/null +++ b/api/src/main/java/jakarta/enterprise/lang/model/declarations/RecordComponentInfo.java @@ -0,0 +1,57 @@ +package jakarta.enterprise.lang.model.declarations; + +import jakarta.enterprise.lang.model.types.Type; + +/** + * A record component, {@linkplain #declaringRecord() declared} in some record. + * + * @since 4.0 + */ +public interface RecordComponentInfo extends DeclarationInfo { + /** + * Returns the name of this record component. + * + * @return the name of this record component, never {@code null} + */ + String name(); + + /** + * Returns the {@linkplain Type type} of this record component. + * + * @return the {@linkplain Type type} of this record component, never {@code null} + */ + Type type(); + + /** + * Returns the field corresponding to this record component. + * + * @return the field, never {@code null} + */ + FieldInfo field(); + + /** + * Returns the accessor method corresponding to this record component. + * + * @return the accessor method, never {@code null} + */ + MethodInfo accessor(); + + /** + * Returns the {@linkplain ClassInfo record} that declares this component. + * + * @return the {@linkplain ClassInfo record} that declares this component, never {@code null} + */ + ClassInfo declaringRecord(); + + // --- + + @Override + default Kind kind() { + return Kind.RECORD_COMPONENT; + } + + @Override + default RecordComponentInfo asRecordComponent() { + return this; + } +}