RFR: 8322292: Rearrange comparison of fields in Record.equals() [v5]

ExE Boss duke at openjdk.org
Thu Dec 21 19:46:48 UTC 2023


On Thu, 21 Dec 2023 18:30:40 GMT, Rob Spoor <duke at openjdk.org> wrote:

>> Sergey Tsypanov has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Update src/java.base/share/classes/java/lang/runtime/ObjectMethods.java
>>   
>>   Co-authored-by: ExE Boss <3889017+ExE-Boss at users.noreply.github.com>
>
> src/java.base/share/classes/java/lang/runtime/ObjectMethods.java line 224:
> 
>> 222:             var rt2 = mh2.type().returnType();
>> 223:             return Integer.compare(
>> 224:                 rt1.isPrimitive() || rt1.isEnum() || rt1.isArray() ? 1 : Iterable.class.isAssignableFrom(rt1) ? -1 : 0,
> 
> Doesn't this put primitives, enums and arrays at the end instead of at the start? I've tried this with a simple array:
> 
> Class<?>[] types = { int.class, String.class, List.class, long.class, TimeUnit.class, byte[].class, Integer.class };
> 
> The result of sorting:
> 
>  Class[7] { interface java.util.List, class java.lang.String, class java.lang.Integer, int, long, class java.util.concurrent.TimeUnit, class [B }
> 
> By switching the -1 and 1 I get the primitives etc.  at the start:
> 
>  Class[7] { int, long, class java.util.concurrent.TimeUnit, class [B, class java.lang.String, class java.lang.Integer, interface java.util.List }
> ``

The `equalator`s are joined together in reverse order, so this is actually correct for the current implementation:


record Example(A a, B b, C c) {
	public static void main(String... args) {
		final var left	= new Example(new A(), new B(), new C());
		final var right	= new Example(new A(), new B(), new C());

		left.equals(right);
		// prints:
		// > C::equals()
		// > B::equals()
		// > A::equals()
	}
}

record A() {
	@Override
	public boolean equals(Object other) {
		System.out.println("A::equals()");
		return other instanceof A;
	}
}

record B() {
	@Override
	public boolean equals(Object other) {
		System.out.println("B::equals()");
		return other instanceof B;
	}
}

record C() {
	@Override
	public boolean equals(Object other) {
		System.out.println("C::equals()");
		return other instanceof C;
	}
}

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/17143#discussion_r1434439484


More information about the core-libs-dev mailing list