Class ArrayType

  • All Implemented Interfaces:
    Descriptor

    public final class ArrayType
    extends Type
    Represents a Java array type. Note that this representation of array types is different from the Java language representation.

    In the Java language, array types have a component type and an element type. An element type is never an array type; it is the ultimate type obtained after all array dimensions are removed. For example, an element type of String[][] is String. A component type is the element type in case of one-dimensional arrays, and the same array type with one dimension less in case of multidimensional arrays. For example, the component type of String[][] is String[], whose component type is String. The number of dimensions can only be found by obtaining the component type repeatedly, until the element type is reached.

    On the other hand, the Jandex representation is compressed. It consists of a constituent type and a number of dimensions. In case the array type does not contain type annotations, the constituent type is the element type. For example, the array type String[][] has 2 dimensions and a constituent type of String. However, to faithfully represent type annotations, array types may be nested; that is, the constituent type may be another array type. For example, the array type of String[] @Ann [] has 1 dimension and a constituent type of String @Ann []. In turn, this array type has 1 dimension and a constituent type of String.

    The constituent() and dimensions() methods provide access to the Jandex native representation. The elementType() and componentType() methods, as well as deepDimensions(), provide access to the Java language representation.

    The component() method is present for backwards compatibility and should not be used. It is equivalent to the constituent() method.

    Since:
    2.0
    • Field Detail

      • constituent

        private final Type constituent
      • dimensions

        private final int dimensions
      • hash

        private int hash
    • Constructor Detail

      • ArrayType

        ArrayType​(Type constituent,
                  int dimensions)
    • Method Detail

      • create

        public static ArrayType create​(Type constituent,
                                       int dimensions)
        Create a new array type instance with the specified number of dimensions and the specified constituent type.
        Parameters:
        constituent - the constituent type
        dimensions - the number of dimensions of this array
        Returns:
        the new array type instance
        Since:
        2.1
        See Also:
        constituent(), dimensions()
      • builder

        public static ArrayType.Builder builder​(Type constituent,
                                                int dimensions)
        Create a builder of an array type.
        Parameters:
        constituent - the constituent type
        dimensions - the number of dimensions of the array
        Returns:
        the builder
        Since:
        3.1.0
        See Also:
        constituent(), dimensions()
      • constituent

        public Type constituent()
        Returns the constituent type of the array. Note that it may be another array type in case type annotations are present on the array components. For example, String[][] has 2 dimensions and a constituent type of String, while String[] @Ann [] has 1 dimension and a constituent type of String @Ann []. The String @Ann [] array type, in turn, has 1 dimension and a constituent type of String.

        This method provides access to the Jandex compressed representation of array types. It is likely you want to use elementType() or componentType() instead.

        Returns:
        the constituent type
        Since:
        3.1.0
      • elementType

        public Type elementType()
        Returns the element type of the array. For example, both String[][] and String @Ann [] have an element type of String.

        This method never returns an ArrayType.

        Returns:
        the element type
        Since:
        3.1.0
      • componentType

        public Type componentType()
        Returns the component type of the array. For example, String[][] has a component type of String[], while String [] @Ann [] has a component type of String @Ann [].
        Returns:
        the component type
        Since:
        3.1.0
      • dimensions

        public int dimensions()
        The number of dimensions this array type has. Note that the constituent type may be an array type, so the number of dimensions does not necessarily correspond to the number of times an array dimension would have to be removed from this array type to reach its element type. For example, this method would return 2 for an array type of String[][], but it would return 1 for an array type of String[] @Ann [], because the constituent type of that array type is yet another array type, also with 1 dimension.

        This method is different to deepDimensions() in case this ArrayType has another ArrayType as its constituent type.

        This method provides access to the Jandex compressed representation of array types. It is likely you want to use deepDimensions() instead.

        Returns:
        the number of dimensions of this array type
      • deepDimensions

        public int deepDimensions()
        The total number of dimensions this array type has, when traversed deep to the element type. For example, both String[][] and String[] @Ann [] have a "deep" number of dimensions equal to 2.

        This method is different to dimensions() in case this ArrayType has another ArrayType as its constituent type.

        Returns:
        the "deep" number of dimensions of this array type
        Since:
        3.1.0
      • name

        public DotName name()
        Description copied from class: Type
        Returns the name of this type (or its erasure in case of generic types) as a DotName, using the Class.getName() format. Specifically:
        • for primitive types and the void pseudo-type, the corresponding Java keyword is returned (void, boolean, byte, short, int, long, float, double, char);
        • for class types, the binary name of the class is returned;
        • for array types, a string is returned that consists of one or more [ characters corresponding to the number of dimensions of the array type, followed by the element type as a single-character code for primitive types or Lbinary.name.of.TheClass; for class types (for example, [I for int[] or [[Ljava.lang.String; for String[][]);
        • for parameterized types, the binary name of the generic class is returned (for example, java.util.List for List<String>);
        • for type variables, the name of the first bound of the type variable is returned, or java.lang.Object for type variables that have no bound;
        • for wildcard types, the name of the upper bound is returned, or java.lang.Object if the wildcard type does not have an upper bound (for example, java.lang.Number for ? extends Number).
        Overrides:
        name in class Type
        Returns:
        the name of this type (or its erasure in case of generic types)
      • kind

        public Type.Kind kind()
        Description copied from class: Type
        Returns the kind of Type this is.
        Specified by:
        kind in class Type
        Returns:
        the kind
      • copyType

        Type copyType​(Type component,
                      int dimensions)
      • toString

        java.lang.String toString​(boolean simple)
        Overrides:
        toString in class Type
      • appendRootComponent

        private void appendRootComponent​(java.lang.StringBuilder builder,
                                         boolean simple)
      • appendArraySyntax

        private void appendArraySyntax​(java.lang.StringBuilder builder)
      • equals

        public boolean equals​(java.lang.Object o)
        Description copied from class: Type
        Compares this Type with another type. A type is equal to another type if it is of the same kind, and all of their fields are equal. This includes annotations, which must be equal as well.
        Overrides:
        equals in class Type
        Parameters:
        o - the type to compare to
        Returns:
        true if equal
        See Also:
        Object.equals(Object)
      • hashCode

        public int hashCode()
        Description copied from class: Type
        Computes a hash code representing this type.
        Overrides:
        hashCode in class Type
        Returns:
        the hash code
      • internEquals

        boolean internEquals​(java.lang.Object o)
        Overrides:
        internEquals in class Type