Skip to content

Commit d5fc1ae

Browse files
committed
added hashCode and equals to TypeAnnotationTargetInfo, LocationInfo, and Location
removed redundant LocationInfo.depth field
1 parent 2187a71 commit d5fc1ae

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

src/java.base/share/classes/sun/reflect/annotation/TypeAnnotation.java

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import java.lang.reflect.AnnotatedElement;
3030
import java.nio.ByteBuffer;
3131
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.Collections;
3234
import java.util.List;
35+
import java.util.Objects;
3336

3437
/**
3538
* A TypeAnnotation contains all the information needed to transform type
@@ -130,30 +133,56 @@ public int getSecondaryIndex() {
130133
return secondaryIndex;
131134
}
132135

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+
133152
@Override
134153
public String toString() {
135154
return "" + target + ": " + count + ", " + secondaryIndex;
136155
}
137156
}
138157

139158
public static final class LocationInfo {
140-
private final int depth;
141159
private final Location[] locations;
142160

143161
private LocationInfo() {
144-
this(0, new Location[0]);
162+
this(new Location[0]);
145163
}
146-
public LocationInfo(int depth, Location[] locations) {
147-
this.depth = depth;
164+
public LocationInfo(Location[] locations) {
148165
this.locations = locations;
149166
}
150167

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;
153181
}
154182

155-
public Location getLocationAt(int index) {
156-
return locations[index];
183+
@Override
184+
public int hashCode() {
185+
return Arrays.hashCode(locations);
157186
}
158187

159188
public static final LocationInfo BASE_LOCATION = new LocationInfo();
@@ -172,7 +201,7 @@ public static LocationInfo parseLocationInfo(ByteBuffer buf) {
172201
throw new AnnotationFormatError("Bad Location encoding in Type Annotation");
173202
locations[i] = new Location(tag, index);
174203
}
175-
return new LocationInfo(depth, locations);
204+
return new LocationInfo(locations);
176205
}
177206

178207
public LocationInfo pushArray() {
@@ -192,24 +221,26 @@ public LocationInfo pushTypeArg(short index) {
192221
}
193222

194223
public LocationInfo pushLocation(byte tag, short index) {
195-
int newDepth = this.depth + 1;
224+
int depth = this.locations.length;
225+
int newDepth = depth + 1;
196226
Location[] res = new Location[newDepth];
197227
System.arraycopy(this.locations, 0, res, 0, depth);
198228
res[newDepth - 1] = new Location(tag, (short)(index & 0xFF));
199-
return new LocationInfo(newDepth, res);
229+
return new LocationInfo(res);
200230
}
201231

202232
/**
203233
* Pops a location matching {@code tag}, or returns {@code null}
204234
* if no matching location was found.
205235
*/
206236
public LocationInfo popLocation(byte tag) {
237+
int depth = locations.length;
207238
if (depth == 0 || locations[depth - 1].tag != tag) {
208239
return null;
209240
}
210241
Location[] res = new Location[depth - 1];
211242
System.arraycopy(locations, 0, res, 0, depth - 1);
212-
return new LocationInfo(depth - 1, res);
243+
return new LocationInfo(res);
213244
}
214245

215246
public TypeAnnotation[] filter(TypeAnnotation[] ta) {
@@ -222,9 +253,9 @@ public TypeAnnotation[] filter(TypeAnnotation[] ta) {
222253
}
223254

224255
boolean isSameLocationInfo(LocationInfo other) {
225-
if (depth != other.depth)
256+
if (locations.length != other.locations.length)
226257
return false;
227-
for (int i = 0; i < depth; i++)
258+
for (int i = 0; i < locations.length; i++)
228259
if (!locations[i].isSameLocation(other.locations[i]))
229260
return false;
230261
return true;
@@ -238,6 +269,19 @@ boolean isSameLocation(Location other) {
238269
return tag == other.tag && index == other.index;
239270
}
240271

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+
241285
public Location(byte tag, short index) {
242286
this.tag = tag;
243287
this.index = index;

0 commit comments

Comments
 (0)