View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.generic;
18  
19  import org.apache.bcel.Const;
20  
21  /**
22   * Denotes array type, such as int[][]
23   */
24  public final class ArrayType extends ReferenceType {
25  
26      private final int dimensions;
27      private final Type basicType;
28  
29      /**
30       * Convenience constructor for array type, e.g. int[]
31       *
32       * @param type array type, e.g. T_INT
33       * @param dimensions array dimensions
34       */
35      public ArrayType(final byte type, final int dimensions) {
36          this(BasicType.getType(type), dimensions);
37      }
38  
39      /**
40       * Convenience constructor for reference array type, e.g. Object[]
41       *
42       * @param className complete name of class ({@link String}, for example)
43       * @param dimensions array dimensions
44       */
45      public ArrayType(final String className, final int dimensions) {
46          this(ObjectType.getInstance(className), dimensions);
47      }
48  
49      /**
50       * Constructor for array of given type
51       *
52       * @param type type of array (may be an array itself)
53       * @param dimensions array dimensions
54       */
55      public ArrayType(final Type type, final int dimensions) {
56          super(Const.T_ARRAY, "<dummy>");
57          if (dimensions < 1 || dimensions > Const.MAX_BYTE) {
58              throw new ClassGenException("Invalid number of dimensions: " + dimensions);
59          }
60          switch (type.getType()) {
61          case Const.T_ARRAY:
62              final ArrayType array = (ArrayType) type;
63              this.dimensions = dimensions + array.dimensions;
64              basicType = array.basicType;
65              break;
66          case Const.T_VOID:
67              throw new ClassGenException("Invalid type: void[]");
68          default: // Basic type or reference
69              this.dimensions = dimensions;
70              basicType = type;
71              break;
72          }
73          final StringBuilder buf = new StringBuilder();
74          for (int i = 0; i < this.dimensions; i++) {
75              buf.append('[');
76          }
77          buf.append(basicType.getSignature());
78          this.signature = buf.toString();
79      }
80  
81      /**
82       * @return true if both type objects refer to the same array type.
83       */
84      @Override
85      public boolean equals(final Object type) {
86          if (type instanceof ArrayType) {
87              final ArrayType array = (ArrayType) type;
88              return array.dimensions == dimensions && array.basicType.equals(basicType);
89          }
90          return false;
91      }
92  
93      /**
94       * @return basic type of array, i.e., for int[][][] the basic type is int
95       */
96      public Type getBasicType() {
97          return basicType;
98      }
99  
100     /**
101      * Gets the name of referenced class.
102      *
103      * @return name of referenced class.
104      * @since 6.7.0
105      */
106     @Override
107     public String getClassName() {
108         return signature;
109     }
110 
111     /**
112      * @return number of dimensions of array
113      */
114     public int getDimensions() {
115         return dimensions;
116     }
117 
118     /**
119      * @return element type of array, i.e., for int[][][] the element type is int[][]
120      */
121     public Type getElementType() {
122         if (dimensions == 1) {
123             return basicType;
124         }
125         return new ArrayType(basicType, dimensions - 1);
126     }
127 
128     /**
129      * @return a hash code value for the object.
130      */
131     @Override
132     public int hashCode() {
133         return basicType.hashCode() ^ dimensions;
134     }
135 }