Class RecordUtils


  • public final class RecordUtils
    extends java.lang.Object
    Large parts of this code is borrowed from ...

    Utility methods for record serialization, using MethodHandles.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object componentValue​(java.lang.Object recordObject, RecComponent recordComponent)
      Retrieves the value of the record component for the given record object.
      static <T> T invokeCanonicalConstructor​(java.lang.Class<T> recordType, RecComponent[] recordComponents, java.lang.Object[] args)
      Invokes the canonical constructor of a record class with the given argument values.
      static boolean isRecord​(java.lang.Class<?> type)
      Returns true if, and only if, the given class is a record class.
      static <T> RecComponent[] recordComponents​(java.lang.Class<T> type, java.util.Comparator<RecComponent> comparator)
      Returns an ordered array of the record components for the given record class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isRecord

        public static boolean isRecord​(java.lang.Class<?> type)
        Returns true if, and only if, the given class is a record class.
        Parameters:
        type - class type
        Returns:
        if this class is a record
      • recordComponents

        public static <T> RecComponent[] recordComponents​(java.lang.Class<T> type,
                                                          java.util.Comparator<RecComponent> comparator)
        Returns an ordered array of the record components for the given record class. The order is imposed by the given comparator. If the given comparator is null, the order is that of the record components in the record attribute of the class file.
        Type Parameters:
        T - This is the type of the record
        Parameters:
        type - class type
        comparator - how to sort the records
        Returns:
        the record components
      • componentValue

        public static java.lang.Object componentValue​(java.lang.Object recordObject,
                                                      RecComponent recordComponent)
        Retrieves the value of the record component for the given record object.
        Parameters:
        recordObject - record object to get its values
        recordComponent - components or records
        Returns:
        record components
      • invokeCanonicalConstructor

        public static <T> T invokeCanonicalConstructor​(java.lang.Class<T> recordType,
                                                       RecComponent[] recordComponents,
                                                       java.lang.Object[] args)
        Invokes the canonical constructor of a record class with the given argument values.
        Type Parameters:
        T - This is the type of the record
        Parameters:
        recordType - type of record
        recordComponents - record components
        args - objects used to create the record
        Returns:
        the record created