Skip to content
Permalink
Browse files
GROOVY-10434: ClassNode isRecord() refactoring: additional clarificat…
…ion when information will be available
  • Loading branch information
paulk-asert committed Jan 25, 2022
1 parent 3eac9fb commit f1da776d95156c9467b0a2a697bcb84392839436
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
@@ -55,6 +55,7 @@
import static org.codehaus.groovy.ast.ClassHelper.isObjectType;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveVoid;
import static org.codehaus.groovy.transform.RecordTypeASTTransformation.recordNative;
import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
import static org.objectweb.asm.Opcodes.ACC_ANNOTATION;
import static org.objectweb.asm.Opcodes.ACC_ENUM;
@@ -1369,23 +1370,24 @@ public boolean isInterface() {

/**
* Checks if the {@link ClassNode} instance represents a native {@code record}.
* Check instead for the {@code RecordType} annotation if looking for records and record-like classes.
* Check instead for the {@code RecordBase} annotation if looking for records and
* record-like classes currently being compiled.
*
* @return {@code true} if the instance represents a native {@code record}
*
* @since 4.0.0
*/
@Incubating
public boolean isRecord() {
return getUnresolvedSuperClass() != null && "java.lang.Record".equals(getUnresolvedSuperClass().getName());
return recordNative(this);
}

/**
* Gets the record components of record type.
*
* @return {@code RecordComponentNode} instances
*
* @since 4.0.0
*/
@Incubating
public List<RecordComponentNode> getRecordComponents() {
if (redirect != null)
return redirect.getRecordComponents();
@@ -1398,6 +1400,7 @@ public List<RecordComponentNode> getRecordComponents() {
*
* @since 4.0.0
*/
@Incubating
public void setRecordComponents(List<RecordComponentNode> recordComponents) {
if (redirect != null) {
redirect.setRecordComponents(recordComponents);
@@ -1411,7 +1414,8 @@ public boolean isAbstract() {
}

/**
* @return true for native and emulated (annotation based) sealed classes
* @return {@code true} for native and emulated (annotation based) sealed classes
* @since 4.0.0
*/
@Incubating
public boolean isSealed() {
@@ -26,6 +26,7 @@
import groovy.transform.RecordTypeMode;
import groovy.transform.options.PropertyHandler;
import org.apache.groovy.ast.tools.MethodNodeUtils;
import org.apache.groovy.lang.annotation.Incubating;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
@@ -254,6 +255,17 @@ private void doProcessRecordType(ClassNode cNode, PropertyHandler handler) {
}
}

/**
* Indicates that the given classnode is a native JVM record class.
* For classes being compiled, this will only be valid after the
* {@code RecordTypeASTTransformation} transform has been invoked.
*/
@Incubating
public static boolean recordNative(ClassNode node) {
return node.getUnresolvedSuperClass() != null &&
"java.lang.Record".equals(node.getUnresolvedSuperClass().getName());
}

private void createComponents(ClassNode cNode, List<PropertyNode> pList) {
if (pList.size() > 16) { // Groovy currently only goes to Tuple16
addError("Record has too many components for a components() method", cNode);
@@ -115,9 +115,9 @@ public static boolean sealedNative(AnnotatedNode node) {
}

/**
* Reports true if the {@code Sealed} annotation should be included in the bytecode for
* a sealed or emulated-sealed class.
* Will only ever return true after the SealedASTTransformation visit method has completed.
* Reports true if the {@code Sealed} annotation should not be
* included in the bytecode for a sealed or emulated-sealed class.
* Will only ever return true after the {@code SealedASTTransformation} transform has been invoked.
*
* @return true if a {@code Sealed} annotation is not required for this node
*/

0 comments on commit f1da776

Please sign in to comment.