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