Class DimensionHelpers

java.lang.Object
com.google.googlejavaformat.java.DimensionHelpers

class DimensionHelpers extends 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.

  • Constructor Details

    • DimensionHelpers

      DimensionHelpers()
  • Method Details

    • extractDims

      static DimensionHelpers.TypeWithDims extractDims(com.sun.source.tree.Tree node, DimensionHelpers.SortedDims sorted)
      Returns a (possibly re-ordered) DimensionHelpers.TypeWithDims for the given type.
    • reorderBySourcePosition

      private static Iterable<List<com.sun.source.tree.AnnotationTree>> reorderBySourcePosition(Deque<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(Deque<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.