29
29
import java .lang .reflect .AnnotatedElement ;
30
30
import java .nio .ByteBuffer ;
31
31
import java .util .ArrayList ;
32
+ import java .util .Arrays ;
33
+ import java .util .Collections ;
32
34
import java .util .List ;
35
+ import java .util .Objects ;
33
36
34
37
/**
35
38
* A TypeAnnotation contains all the information needed to transform type
@@ -130,30 +133,56 @@ public int getSecondaryIndex() {
130
133
return secondaryIndex ;
131
134
}
132
135
136
+ @ Override
137
+ public boolean equals (Object obj ) {
138
+ if (obj instanceof TypeAnnotationTargetInfo that ) {
139
+ return target == that .target &&
140
+ count == that .count &&
141
+ secondaryIndex == that .secondaryIndex ;
142
+
143
+ }
144
+ return false ;
145
+ }
146
+
147
+ @ Override
148
+ public int hashCode () {
149
+ return Objects .hash (target , count , secondaryIndex );
150
+ }
151
+
133
152
@ Override
134
153
public String toString () {
135
154
return "" + target + ": " + count + ", " + secondaryIndex ;
136
155
}
137
156
}
138
157
139
158
public static final class LocationInfo {
140
- private final int depth ;
141
159
private final Location [] locations ;
142
160
143
161
private LocationInfo () {
144
- this (0 , new Location [0 ]);
162
+ this (new Location [0 ]);
145
163
}
146
- public LocationInfo (int depth , Location [] locations ) {
147
- this .depth = depth ;
164
+ public LocationInfo (Location [] locations ) {
148
165
this .locations = locations ;
149
166
}
150
167
151
- public int getDepth () {
152
- return depth ;
168
+ /**
169
+ * Gets an immutable view on the locations.
170
+ */
171
+ public List <Location > getLocations () {
172
+ return Collections .unmodifiableList (Arrays .asList (locations ));
173
+ }
174
+
175
+ @ Override
176
+ public boolean equals (Object obj ) {
177
+ if (obj instanceof LocationInfo that ) {
178
+ return Arrays .equals (locations , that .locations );
179
+ }
180
+ return false ;
153
181
}
154
182
155
- public Location getLocationAt (int index ) {
156
- return locations [index ];
183
+ @ Override
184
+ public int hashCode () {
185
+ return Arrays .hashCode (locations );
157
186
}
158
187
159
188
public static final LocationInfo BASE_LOCATION = new LocationInfo ();
@@ -172,7 +201,7 @@ public static LocationInfo parseLocationInfo(ByteBuffer buf) {
172
201
throw new AnnotationFormatError ("Bad Location encoding in Type Annotation" );
173
202
locations [i ] = new Location (tag , index );
174
203
}
175
- return new LocationInfo (depth , locations );
204
+ return new LocationInfo (locations );
176
205
}
177
206
178
207
public LocationInfo pushArray () {
@@ -192,24 +221,26 @@ public LocationInfo pushTypeArg(short index) {
192
221
}
193
222
194
223
public LocationInfo pushLocation (byte tag , short index ) {
195
- int newDepth = this .depth + 1 ;
224
+ int depth = this .locations .length ;
225
+ int newDepth = depth + 1 ;
196
226
Location [] res = new Location [newDepth ];
197
227
System .arraycopy (this .locations , 0 , res , 0 , depth );
198
228
res [newDepth - 1 ] = new Location (tag , (short )(index & 0xFF ));
199
- return new LocationInfo (newDepth , res );
229
+ return new LocationInfo (res );
200
230
}
201
231
202
232
/**
203
233
* Pops a location matching {@code tag}, or returns {@code null}
204
234
* if no matching location was found.
205
235
*/
206
236
public LocationInfo popLocation (byte tag ) {
237
+ int depth = locations .length ;
207
238
if (depth == 0 || locations [depth - 1 ].tag != tag ) {
208
239
return null ;
209
240
}
210
241
Location [] res = new Location [depth - 1 ];
211
242
System .arraycopy (locations , 0 , res , 0 , depth - 1 );
212
- return new LocationInfo (depth - 1 , res );
243
+ return new LocationInfo (res );
213
244
}
214
245
215
246
public TypeAnnotation [] filter (TypeAnnotation [] ta ) {
@@ -222,9 +253,9 @@ public TypeAnnotation[] filter(TypeAnnotation[] ta) {
222
253
}
223
254
224
255
boolean isSameLocationInfo (LocationInfo other ) {
225
- if (depth != other .depth )
256
+ if (locations . length != other .locations . length )
226
257
return false ;
227
- for (int i = 0 ; i < depth ; i ++)
258
+ for (int i = 0 ; i < locations . length ; i ++)
228
259
if (!locations [i ].isSameLocation (other .locations [i ]))
229
260
return false ;
230
261
return true ;
@@ -238,6 +269,19 @@ boolean isSameLocation(Location other) {
238
269
return tag == other .tag && index == other .index ;
239
270
}
240
271
272
+ @ Override
273
+ public boolean equals (Object obj ) {
274
+ if (obj instanceof Location that ) {
275
+ return isSameLocation (that );
276
+ }
277
+ return false ;
278
+ }
279
+
280
+ @ Override
281
+ public int hashCode () {
282
+ return Objects .hash (tag , index );
283
+ }
284
+
241
285
public Location (byte tag , short index ) {
242
286
this .tag = tag ;
243
287
this .index = index ;
0 commit comments