Skip to content

Commit

Permalink
add support for records to the language model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Oct 5, 2021
1 parent a626d76 commit 5f55bfe
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import java.util.List;

/**
* A class. Four kinds of classes are distinguished:
* A class. Five kinds of classes are distinguished:
* <ul>
* <li>plain classes</li>
* <li>interfaces</li>
* <li>enums (restricted kind of classes)</li>
* <li>annotations (specialized kind of interfaces)</li>
* <li>records (restricted kind of classes)</li>
* </ul>
*
* Classes are represented as isolated units. That is, if this class is nested, it is not possible
Expand All @@ -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 <cite>The Java&trade; Language Specification</cite>;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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}.
*
Expand Down Expand Up @@ -176,6 +183,14 @@ public interface ClassInfo extends DeclarationInfo {
*/
Collection<FieldInfo> 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<RecordComponentInfo> recordComponents();

// ---

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum Kind {
METHOD,
PARAMETER,
FIELD,
RECORD_COMPONENT,
}

/**
Expand Down Expand Up @@ -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}.
*
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 5f55bfe

Please sign in to comment.