Class DimensionHelpers


  • class DimensionHelpers
    extends java.lang.Object
    Utilities for working with array dimensions.

    javac's parser does not preserve concrete syntax for mixed-notation arrays, so we have to re-lex the input to extra it.

    For example, int [] a; cannot be distinguished from int [] a []; in the AST.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  DimensionHelpers.SortedDims  
      (package private) static class  DimensionHelpers.TypeWithDims
      The array dimension specifiers (including any type annotations) associated with a type.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static DimensionHelpers.TypeWithDims extractDims​(com.sun.source.tree.Tree node, DimensionHelpers.SortedDims sorted)
      Returns a (possibly re-ordered) DimensionHelpers.TypeWithDims for the given type.
      private static com.sun.source.tree.Tree extractDims​(java.util.Deque<java.util.List<com.sun.source.tree.AnnotationTree>> dims, com.sun.source.tree.Tree node)
      Accumulates a flattened list of array dimensions specifiers with type annotations, and returns the base type.
      private static java.lang.Iterable<java.util.List<com.sun.source.tree.AnnotationTree>> reorderBySourcePosition​(java.util.Deque<java.util.List<com.sun.source.tree.AnnotationTree>> dims)
      Rotate the list of dimension specifiers until all dimensions with type annotations appear in source order.
      • Methods inherited from class java.lang.Object

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

      • DimensionHelpers

        DimensionHelpers()
    • Method Detail

      • reorderBySourcePosition

        private static java.lang.Iterable<java.util.List<com.sun.source.tree.AnnotationTree>> reorderBySourcePosition​(java.util.Deque<java.util.List<com.sun.source.tree.AnnotationTree>> dims)
        Rotate the list of dimension specifiers until all dimensions with type annotations appear in source order.

        javac reorders dimension specifiers in method declarations with mixed-array notation, which means that any type annotations don't appear in source order.

        For example, the type of int @A [] f() @B [] {} is parsed as @B [] @A [].

        This doesn't handle cases with un-annotated dimension specifiers, so the formatting logic checks the token stream to figure out which side of the method name they appear on.

      • extractDims

        private static com.sun.source.tree.Tree extractDims​(java.util.Deque<java.util.List<com.sun.source.tree.AnnotationTree>> dims,
                                                            com.sun.source.tree.Tree node)
        Accumulates a flattened list of array dimensions specifiers with type annotations, and returns the base type.

        Given int @A @B [][] @C [], adds [[@A, @B], [@C]] to dims and returns int.