Skip to content

Commit ca12e13

Browse files
committed
ObjectUtils.nullSafeEquals allows for JVM method inlining (through reducing its bytecode size)
Issue: SPR-14349
1 parent 57ca8f5 commit ca12e13

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -277,14 +277,14 @@ public static Object[] toObjectArray(Object source) {
277277
//---------------------------------------------------------------------
278278

279279
/**
280-
* Determine if the given objects are equal, returning {@code true}
281-
* if both are {@code null} or {@code false} if only one is
282-
* {@code null}.
280+
* Determine if the given objects are equal, returning {@code true} if
281+
* both are {@code null} or {@code false} if only one is {@code null}.
283282
* <p>Compares arrays with {@code Arrays.equals}, performing an equality
284283
* check based on the array elements rather than the array reference.
285284
* @param o1 first Object to compare
286285
* @param o2 second Object to compare
287286
* @return whether the given objects are equal
287+
* @see Object#equals(Object)
288288
* @see java.util.Arrays#equals
289289
*/
290290
public static boolean nullSafeEquals(Object o1, Object o2) {
@@ -298,33 +298,47 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
298298
return true;
299299
}
300300
if (o1.getClass().isArray() && o2.getClass().isArray()) {
301-
if (o1 instanceof Object[] && o2 instanceof Object[]) {
302-
return Arrays.equals((Object[]) o1, (Object[]) o2);
303-
}
304-
if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
305-
return Arrays.equals((boolean[]) o1, (boolean[]) o2);
306-
}
307-
if (o1 instanceof byte[] && o2 instanceof byte[]) {
308-
return Arrays.equals((byte[]) o1, (byte[]) o2);
309-
}
310-
if (o1 instanceof char[] && o2 instanceof char[]) {
311-
return Arrays.equals((char[]) o1, (char[]) o2);
312-
}
313-
if (o1 instanceof double[] && o2 instanceof double[]) {
314-
return Arrays.equals((double[]) o1, (double[]) o2);
315-
}
316-
if (o1 instanceof float[] && o2 instanceof float[]) {
317-
return Arrays.equals((float[]) o1, (float[]) o2);
318-
}
319-
if (o1 instanceof int[] && o2 instanceof int[]) {
320-
return Arrays.equals((int[]) o1, (int[]) o2);
321-
}
322-
if (o1 instanceof long[] && o2 instanceof long[]) {
323-
return Arrays.equals((long[]) o1, (long[]) o2);
324-
}
325-
if (o1 instanceof short[] && o2 instanceof short[]) {
326-
return Arrays.equals((short[]) o1, (short[]) o2);
327-
}
301+
return arrayEquals(o1, o2);
302+
}
303+
return false;
304+
}
305+
306+
/**
307+
* Compare the given arrays with {@code Arrays.equals}, performing an equality
308+
* check based on the array elements rather than the array reference.
309+
* @param o1 first array to compare
310+
* @param o2 second array to compare
311+
* @return whether the given objects are equal
312+
* @see #nullSafeEquals(Object, Object)
313+
* @see java.util.Arrays#equals
314+
*/
315+
private static boolean arrayEquals(Object o1, Object o2) {
316+
if (o1 instanceof Object[] && o2 instanceof Object[]) {
317+
return Arrays.equals((Object[]) o1, (Object[]) o2);
318+
}
319+
if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
320+
return Arrays.equals((boolean[]) o1, (boolean[]) o2);
321+
}
322+
if (o1 instanceof byte[] && o2 instanceof byte[]) {
323+
return Arrays.equals((byte[]) o1, (byte[]) o2);
324+
}
325+
if (o1 instanceof char[] && o2 instanceof char[]) {
326+
return Arrays.equals((char[]) o1, (char[]) o2);
327+
}
328+
if (o1 instanceof double[] && o2 instanceof double[]) {
329+
return Arrays.equals((double[]) o1, (double[]) o2);
330+
}
331+
if (o1 instanceof float[] && o2 instanceof float[]) {
332+
return Arrays.equals((float[]) o1, (float[]) o2);
333+
}
334+
if (o1 instanceof int[] && o2 instanceof int[]) {
335+
return Arrays.equals((int[]) o1, (int[]) o2);
336+
}
337+
if (o1 instanceof long[] && o2 instanceof long[]) {
338+
return Arrays.equals((long[]) o1, (long[]) o2);
339+
}
340+
if (o1 instanceof short[] && o2 instanceof short[]) {
341+
return Arrays.equals((short[]) o1, (short[]) o2);
328342
}
329343
return false;
330344
}
@@ -335,6 +349,7 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
335349
* this method will delegate to any of the {@code nullSafeHashCode}
336350
* methods for arrays in this class. If the object is {@code null},
337351
* this method returns 0.
352+
* @see Object#hashCode()
338353
* @see #nullSafeHashCode(Object[])
339354
* @see #nullSafeHashCode(boolean[])
340355
* @see #nullSafeHashCode(byte[])

0 commit comments

Comments
 (0)