Skip to content

Commit f7c601e

Browse files
committed
Ensure all Location subclasses implement hashCode() and equals()
* And simplify toString(). * Add hashCode() for Location subclasses defining equals().
1 parent ee7ba1d commit f7c601e

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ExtLocations.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public final void set(DynamicObject store, Object value, boolean guard, boolean
199199

200200
@Override
201201
public String toString() {
202-
return "=" + String.valueOf(value);
202+
return "=" + value;
203203
}
204204

205205
@Override
@@ -369,12 +369,7 @@ public boolean equals(Object obj) {
369369

370370
@Override
371371
public String toString() {
372-
return super.toString() + ("[final=" + isAssumedFinal() + "]");
373-
}
374-
375-
@Override
376-
public String getWhereString() {
377-
return this instanceof ArrayLocation ? ("[" + index + "]") : ("@" + index);
372+
return super.toString() + (this instanceof ArrayLocation ? ("[" + index + "]") : ("@" + index)) + ("[final=" + isAssumedFinal() + "]");
378373
}
379374

380375
@Override
@@ -934,6 +929,11 @@ public void accept(LocationVisitor locationVisitor) {
934929
locationVisitor.visitPrimitiveField(getIndex(), 1);
935930
}
936931

932+
@Override
933+
public int hashCode() {
934+
return Objects.hash(super.hashCode(), allowInt);
935+
}
936+
937937
@Override
938938
public boolean equals(Object obj) {
939939
return super.equals(obj) && this.allowInt == ((DoubleFieldLocation) obj).allowInt;
@@ -1104,6 +1104,11 @@ public int primitiveArrayCount() {
11041104
return DOUBLE_ARRAY_SLOT_SIZE;
11051105
}
11061106

1107+
@Override
1108+
public int hashCode() {
1109+
return Objects.hash(super.hashCode(), allowInt);
1110+
}
1111+
11071112
@Override
11081113
public boolean equals(Object obj) {
11091114
return super.equals(obj) && this.allowInt == ((DoubleArrayLocation) obj).allowInt;
@@ -1198,6 +1203,11 @@ public void accept(LocationVisitor locationVisitor) {
11981203
locationVisitor.visitPrimitiveField(getIndex(), 1);
11991204
}
12001205

1206+
@Override
1207+
public int hashCode() {
1208+
return Objects.hash(super.hashCode(), allowInt);
1209+
}
1210+
12011211
@Override
12021212
public boolean equals(Object obj) {
12031213
return super.equals(obj) && this.allowInt == ((LongFieldLocation) obj).allowInt;
@@ -1283,6 +1293,11 @@ public void accept(LocationVisitor locationVisitor) {
12831293
locationVisitor.visitPrimitiveArray(index, LONG_ARRAY_SLOT_SIZE);
12841294
}
12851295

1296+
@Override
1297+
public int hashCode() {
1298+
return Objects.hash(super.hashCode(), allowInt);
1299+
}
1300+
12861301
@Override
12871302
public boolean equals(Object obj) {
12881303
return super.equals(obj) && this.allowInt == ((LongArrayLocation) obj).allowInt;

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public boolean isConstant() {
308308
}
309309

310310
/**
311-
* Abstract to force overriding.
311+
* Must be overridden in subclasses.
312312
*
313313
* @since 0.8 or earlier
314314
*/
@@ -318,7 +318,7 @@ public int hashCode() {
318318
}
319319

320320
/**
321-
* Abstract to force overriding.
321+
* Must be overridden in subclasses.
322322
*
323323
* @since 0.8 or earlier
324324
*/
@@ -330,20 +330,15 @@ public boolean equals(Object obj) {
330330
if (obj == null) {
331331
return false;
332332
}
333-
if (getClass() != obj.getClass()) {
334-
return false;
335-
}
336-
return true;
333+
return getClass() == obj.getClass();
337334
}
338335

336+
/**
337+
* @since 0.8 or earlier
338+
*/
339339
@Override
340340
public String toString() {
341-
String typeString = Objects.requireNonNullElse(getType(), Object.class).getSimpleName();
342-
return typeString + getWhereString();
343-
}
344-
345-
protected String getWhereString() {
346-
return "";
341+
return Objects.requireNonNullElse(getType(), Object.class).getSimpleName();
347342
}
348343

349344
/**

0 commit comments

Comments
 (0)