提交 ca12e13e 编写于 作者: J Juergen Hoeller

ObjectUtils.nullSafeEquals allows for JVM method inlining (through reducing its bytecode size)

Issue: SPR-14349
上级 57ca8f53
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -277,14 +277,14 @@ public abstract class ObjectUtils { ...@@ -277,14 +277,14 @@ public abstract class ObjectUtils {
//--------------------------------------------------------------------- //---------------------------------------------------------------------
/** /**
* Determine if the given objects are equal, returning {@code true} * Determine if the given objects are equal, returning {@code true} if
* if both are {@code null} or {@code false} if only one is * both are {@code null} or {@code false} if only one is {@code null}.
* {@code null}.
* <p>Compares arrays with {@code Arrays.equals}, performing an equality * <p>Compares arrays with {@code Arrays.equals}, performing an equality
* check based on the array elements rather than the array reference. * check based on the array elements rather than the array reference.
* @param o1 first Object to compare * @param o1 first Object to compare
* @param o2 second Object to compare * @param o2 second Object to compare
* @return whether the given objects are equal * @return whether the given objects are equal
* @see Object#equals(Object)
* @see java.util.Arrays#equals * @see java.util.Arrays#equals
*/ */
public static boolean nullSafeEquals(Object o1, Object o2) { public static boolean nullSafeEquals(Object o1, Object o2) {
...@@ -298,6 +298,21 @@ public abstract class ObjectUtils { ...@@ -298,6 +298,21 @@ public abstract class ObjectUtils {
return true; return true;
} }
if (o1.getClass().isArray() && o2.getClass().isArray()) { if (o1.getClass().isArray() && o2.getClass().isArray()) {
return arrayEquals(o1, o2);
}
return false;
}
/**
* Compare the given arrays with {@code Arrays.equals}, performing an equality
* check based on the array elements rather than the array reference.
* @param o1 first array to compare
* @param o2 second array to compare
* @return whether the given objects are equal
* @see #nullSafeEquals(Object, Object)
* @see java.util.Arrays#equals
*/
private static boolean arrayEquals(Object o1, Object o2) {
if (o1 instanceof Object[] && o2 instanceof Object[]) { if (o1 instanceof Object[] && o2 instanceof Object[]) {
return Arrays.equals((Object[]) o1, (Object[]) o2); return Arrays.equals((Object[]) o1, (Object[]) o2);
} }
...@@ -325,7 +340,6 @@ public abstract class ObjectUtils { ...@@ -325,7 +340,6 @@ public abstract class ObjectUtils {
if (o1 instanceof short[] && o2 instanceof short[]) { if (o1 instanceof short[] && o2 instanceof short[]) {
return Arrays.equals((short[]) o1, (short[]) o2); return Arrays.equals((short[]) o1, (short[]) o2);
} }
}
return false; return false;
} }
...@@ -335,6 +349,7 @@ public abstract class ObjectUtils { ...@@ -335,6 +349,7 @@ public abstract class ObjectUtils {
* this method will delegate to any of the {@code nullSafeHashCode} * this method will delegate to any of the {@code nullSafeHashCode}
* methods for arrays in this class. If the object is {@code null}, * methods for arrays in this class. If the object is {@code null},
* this method returns 0. * this method returns 0.
* @see Object#hashCode()
* @see #nullSafeHashCode(Object[]) * @see #nullSafeHashCode(Object[])
* @see #nullSafeHashCode(boolean[]) * @see #nullSafeHashCode(boolean[])
* @see #nullSafeHashCode(byte[]) * @see #nullSafeHashCode(byte[])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册