Skip to content

Commit b7ac32d

Browse files
author
Vicente Romero
committed
8257598: Clarify what component values are used in Record::equals
Reviewed-by: darcy, chegar
1 parent a280182 commit b7ac32d

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

src/java.base/share/classes/java/lang/Record.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,14 @@ protected Record() {}
111111
* <li> If the component is of a reference type, the component is
112112
* considered equal if and only if {@link
113113
* java.util.Objects#equals(Object,Object)
114-
* Objects.equals(this.c(), r.c()} would return {@code true}.
114+
* Objects.equals(this.c, r.c} would return {@code true}.
115115
*
116116
* <li> If the component is of a primitive type, using the
117117
* corresponding primitive wrapper class {@code PW} (the
118118
* corresponding wrapper class for {@code int} is {@code
119119
* java.lang.Integer}, and so on), the component is considered
120120
* equal if and only if {@code
121-
* PW.valueOf(this.c()).equals(PW.valueOf(r.c()))} would return
122-
* {@code true}.
121+
* PW.compare(this.c, r.c)} would return {@code 0}.
123122
*
124123
* </ul>
125124
*
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8257598
27+
* @summary check that Record::equals uses the fields and not the accessors for the comparison
28+
* @run testng CheckEqualityIsBasedOnFields
29+
*/
30+
31+
import java.lang.reflect.Constructor;
32+
import java.lang.reflect.Field;
33+
import java.lang.reflect.Method;
34+
35+
import org.testng.annotations.DataProvider;
36+
import org.testng.annotations.Test;
37+
38+
import static org.testng.Assert.*;
39+
40+
public class CheckEqualityIsBasedOnFields {
41+
public record R01(boolean x) {
42+
public boolean x() {
43+
return x ? x : !x;
44+
}
45+
}
46+
47+
public record R02(byte x) {
48+
public byte x() {
49+
return (x >= 50) ? (byte)(x - 50) : x;
50+
}
51+
}
52+
53+
public record R03(short x) {
54+
public short x() {
55+
return (x >= 50) ? (short)(x - 50) : x;
56+
}
57+
}
58+
59+
public record R04(char x) {
60+
public char x() {
61+
return (x >= 50) ? (char)(x - 50) : x;
62+
}
63+
}
64+
65+
public record R05(int x) {
66+
public int x() {
67+
return (x >= 50) ? (x - 50) : x;
68+
}
69+
}
70+
71+
public record R06(long x) {
72+
public long x() {
73+
return (x >= 50) ? (long)(x - 50) : x;
74+
}
75+
}
76+
77+
public record R07(float x) {
78+
public float x() {
79+
return (x >= 50) ? (float)(x - 50) : x;
80+
}
81+
}
82+
public record R08(double x) {
83+
public double x() {
84+
return (x >= 50) ? (double)(x - 50) : x;
85+
}
86+
}
87+
88+
public record R09(String x) {
89+
public String x() {
90+
return (x.length() > 1) ? x.substring(0, 1) : x;
91+
}
92+
}
93+
94+
@DataProvider(name = "recordData")
95+
public Object[][] recordTypeAndExpectedValue() {
96+
return new Object[][] {
97+
new Object[] { R01.class, boolean.class, new Object[]{true, false} },
98+
new Object[] { R02.class, byte.class, new Object[]{(byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5,
99+
(byte)50, (byte)51, (byte)52, (byte)53, (byte)54, (byte)55} },
100+
new Object[] { R03.class, short.class, new Object[]{(short)0, (short)1, (short)2, (short)3, (short)4, (short)5,
101+
(short)50, (short)51, (short)52, (short)53, (short)54, (short)55} },
102+
new Object[] { R04.class, char.class, new Object[]{(char)0, (char)1, (char)2, (char)3, (char)4, (char)5,
103+
(char)50, (char)51, (char)52, (char)53, (char)54, (char)55} },
104+
new Object[] { R05.class, int.class, new Object[]{0, 1, 2, 3, 4, 5, 50, 51, 52, 53, 54, 55} },
105+
new Object[] { R06.class, long.class, new Object[]{0L, 1L, 2L, 3L, 4L, 5L, 50L, 51L, 52L, 53L, 54L, 55L} },
106+
new Object[] { R07.class, float.class, new Object[]{(float)0, (float)1, (float)2, (float)3, (float)4, (float)5,
107+
(float)50, (float)51, (float)52, (float)53, (float)54, (float)55} },
108+
new Object[] { R08.class, double.class, new Object[]{(double)0, (double)1, (double)2, (double)3, (double)4, (double)5,
109+
(double)50, (double)51, (double)52, (double)53, (double)54, (double)55} },
110+
new Object[] { R09.class, String.class, new Object[]{"1", "2", "3", "4", "5",
111+
"1_", "2_", "3_", "4_", "5_"} },
112+
};
113+
}
114+
115+
@Test(dataProvider = "recordData")
116+
public void testEqualsDoesntUseAccessors(Class<?> clazz, Class<?> componentClass, Object[] expectedXValues) throws Exception {
117+
Constructor<?> ctor;
118+
Method getter, equalsMethod;
119+
ctor = clazz.getConstructor(componentClass);
120+
equalsMethod = clazz.getMethod("equals", Object.class);
121+
getter = clazz.getMethod("x");
122+
for (int i = 0; i < expectedXValues.length / 2; i++) {
123+
Object rec1 = ctor.newInstance(expectedXValues[i]);
124+
Object rec2 = ctor.newInstance(expectedXValues[i + expectedXValues.length / 2]);
125+
System.out.println(rec1.toString());
126+
System.out.println(rec2.toString());
127+
assertFalse((boolean) equalsMethod.invoke(rec1, rec2));
128+
assertNotEquals(expectedXValues[i], expectedXValues[i + expectedXValues.length / 2]);
129+
assertEquals(getter.invoke(rec1), getter.invoke(rec2));
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)