View Javadoc
1   package mockit.asm.types;
2   
3   import edu.umd.cs.findbugs.annotations.NonNull;
4   
5   import org.checkerframework.checker.index.qual.NonNegative;
6   
7   public final class ArrayType extends ReferenceType {
8       @NonNull
9       public static ArrayType create(@NonNull String typeDesc) {
10          return new ArrayType(typeDesc.toCharArray());
11      }
12  
13      /**
14       * Initializes an array type.
15       *
16       * @param typeDesc
17       *            a buffer containing the descriptor of the array type
18       * @param off
19       *            the offset of the descriptor in the buffer
20       */
21      @NonNull
22      static ArrayType create(@NonNull char[] typeDesc, @NonNegative int off) {
23          int len = findNumberOfDimensions(typeDesc, off);
24  
25          if (typeDesc[off + len] == 'L') {
26              len = findTypeNameLength(typeDesc, off, len);
27          }
28  
29          return new ArrayType(typeDesc, off, len + 1);
30      }
31  
32      @NonNegative
33      private static int findNumberOfDimensions(@NonNull char[] typeDesc, @NonNegative int off) {
34          int dimensions = 1;
35  
36          while (typeDesc[off + dimensions] == '[') {
37              dimensions++;
38          }
39  
40          return dimensions;
41      }
42  
43      private ArrayType(@NonNull char[] typeDesc, @NonNegative int off, @NonNegative int len) {
44          super(typeDesc, off, len);
45      }
46  
47      ArrayType(@NonNull char[] typeDesc) {
48          super(typeDesc);
49      }
50  
51      /**
52       * Returns the number of dimensions of this array type.
53       */
54      @NonNegative
55      public int getDimensions() {
56          return findNumberOfDimensions(typeDescChars, off);
57      }
58  
59      /**
60       * Returns the type of the elements of this array type.
61       */
62      @NonNull
63      public JavaType getElementType() {
64          int dimensions = getDimensions();
65          return getType(typeDescChars, off + dimensions);
66      }
67  
68      @NonNull
69      @Override
70      public String getClassName() {
71          String className = getElementType().getClassName();
72          StringBuilder sb = new StringBuilder(className);
73          int dimensions = getDimensions();
74  
75          for (int i = dimensions; i > 0; i--) {
76              sb.append("[]");
77          }
78  
79          return sb.toString();
80      }
81  }