@@ -61,6 +61,7 @@ public static void exec(JProgram program) {
6161 private final JMethod getClassMethod ;
6262 private final JMethod getSimpleNameMethod ;
6363 private final JClassType javaLangString ;
64+ private final JMethod objectsEquals ;
6465
6566 private ImplementRecordComponents (JProgram program ) {
6667 this .program = program ;
@@ -69,6 +70,8 @@ private ImplementRecordComponents(JProgram program) {
6970 getClassMethod = program .getIndexedMethod (RuntimeConstants .OBJECT_GET_CLASS );
7071 getSimpleNameMethod = program .getIndexedMethod (RuntimeConstants .CLASS_GET_SIMPLE_NAME );
7172 javaLangString = program .getTypeJavaLangString ();
73+ objectsEquals = program .getIndexedType ("Objects" )
74+ .findMethod ("equals(Ljava/lang/Object;Ljava/lang/Object;)Z" , false );
7275 }
7376
7477 private void execImpl () {
@@ -190,28 +193,18 @@ private void implementEquals(JRecordType type, JMethod method, SourceInfo info)
190193 body .getBlock ().addStmt (uncheckedAssign .makeStatement ());
191194
192195 JExpression componentCheck = JBooleanLiteral .TRUE ;
193- JMethod objectEquals = program .getIndexedMethod (RuntimeConstants .OBJECT_EQUALS );
194196 for (JField field : type .getFields ()) {
195197 if (!field .isStatic ()) {
196198 JFieldRef myField = new JFieldRef (info , new JThisRef (info , type ), field , type );
197199 JFieldRef otherField = new JFieldRef (info , typedOther .createRef (info ), field , type );
198- final JBinaryOperation equals ;
200+ final JExpression equals ;
199201 if (field .getType ().isPrimitiveType ()) {
200202 equals = new JBinaryOperation (info , JPrimitiveType .BOOLEAN ,
201203 JBinaryOperator .EQ ,
202204 myField ,
203205 otherField );
204206 } else {
205- // We would like to use Objects.equals here to be more concise, but we would need
206- // to look up the right impl based on the field - just as simple to insert a null check
207- // and get it a little closer to all being inlined away
208-
209- // Make another field ref to call equals() on
210- JFieldRef myField2 = new JFieldRef (info , new JThisRef (info , type ), field , type );
211- equals = new JBinaryOperation (info , JPrimitiveType .BOOLEAN , JBinaryOperator .AND ,
212- new JBinaryOperation (info , JPrimitiveType .BOOLEAN , JBinaryOperator .NEQ ,
213- myField , JNullLiteral .INSTANCE ),
214- new JMethodCall (info , myField2 , objectEquals , otherField ));
207+ equals = new JMethodCall (info , null , objectsEquals , myField , otherField );
215208 }
216209 if (componentCheck != JBooleanLiteral .TRUE ) {
217210 componentCheck = new JBinaryOperation (info , JPrimitiveType .BOOLEAN ,
0 commit comments