Interface CtRecordPattern

All Superinterfaces:
Cloneable, CtCodeElement, CtElement, CtExpression<Void>, CtPattern, CtQueryable, CtTypedElement<Void>, CtVisitable, FactoryAccessor, Serializable, SourcePositionHolder, TemplateParameter<Void>
All Known Implementing Classes:
CtRecordPatternImpl

public interface CtRecordPattern extends CtPattern, CtExpression<Void>
This code element defines a record pattern, introduced in Java 21 by JEP 440.

Example:

     Object obj = null;
     boolean longerThanTwo = false;
     record MyRecord(String value) {}
     // MyRecord(var string) is the record pattern
     if (obj instanceof MyRecord(var string)) {
         longerThanTwo = string.length() > 2;
     }
 
  • Method Details

    • getRecordType

      CtTypeReference<?> getRecordType()
      Returns the type of the deconstructed record.
      Returns:
      the type of the deconstructed record
    • setRecordType

      CtRecordPattern setRecordType(CtTypeReference<?> recordType)
      Sets the type of the deconstructed record.
      Parameters:
      recordType - the record type.
      Returns:
      this pattern
    • getPatternList

      List<CtPattern> getPatternList()
      Returns the inner patterns of this record pattern.
      Returns:
      the inner patterns of this record pattern
    • setPatternList

      CtRecordPattern setPatternList(List<CtPattern> patternList)
      Sets the inner patterns of this record pattern.
      Parameters:
      patternList - the list of inner patterns.
      Returns:
      this pattern
    • addPattern

      CtRecordPattern addPattern(CtPattern pattern)
      Adds an inner patterns to the list of inner patterns of this record.
      Parameters:
      pattern - the inner pattern.
      Returns:
      this pattern
    • clone

      Description copied from interface: CtElement
      Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtCodeElement
      Specified by:
      clone in interface CtElement
      Specified by:
      clone in interface CtExpression<Void>
      Returns:
      a clone of this element. All children are cloned, but the parent of the returned clone is set to null.
    • getTypeCasts

      List<CtTypeReference<?>> getTypeCasts()
      Description copied from interface: CtExpression
      Returns the type casts if any.
      Specified by:
      getTypeCasts in interface CtExpression<Void>
      Returns:
      this object
    • setTypeCasts

      <C extends CtExpression<Void>> C setTypeCasts(List<CtTypeReference<?>> types)
      Description copied from interface: CtExpression
      Sets the type casts.
      Specified by:
      setTypeCasts in interface CtExpression<Void>
      Returns:
      this object
    • addTypeCast

      <C extends CtExpression<Void>> C addTypeCast(CtTypeReference<?> type)
      Description copied from interface: CtExpression
      Adds a type cast.
      Specified by:
      addTypeCast in interface CtExpression<Void>
      Returns:
      this object