1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 import org.apache.bcel.Const;
20
21
22
23
24 public final class ArrayType extends ReferenceType {
25
26 private final int dimensions;
27 private final Type basicType;
28
29
30
31
32
33
34
35 public ArrayType(final byte type, final int dimensions) {
36 this(BasicType.getType(type), dimensions);
37 }
38
39
40
41
42
43
44
45 public ArrayType(final String className, final int dimensions) {
46 this(ObjectType.getInstance(className), dimensions);
47 }
48
49
50
51
52
53
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:
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
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
95
96 public Type getBasicType() {
97 return basicType;
98 }
99
100
101
102
103
104
105
106 @Override
107 public String getClassName() {
108 return signature;
109 }
110
111
112
113
114 public int getDimensions() {
115 return dimensions;
116 }
117
118
119
120
121 public Type getElementType() {
122 if (dimensions == 1) {
123 return basicType;
124 }
125 return new ArrayType(basicType, dimensions - 1);
126 }
127
128
129
130
131 @Override
132 public int hashCode() {
133 return basicType.hashCode() ^ dimensions;
134 }
135 }