[error prone] fix ReferenceEquality warning in static equals methods

Implement a helper method suppressing the ReferenceEquality error prone
warning and use it to fix this warning in static equals methods where
this comparison is used to implement fast path of static equals
implementation.

See https://errorprone.info/bugpattern/ReferenceEquality

Change-Id: I33538a3406007d24efec3a504e031ca1069572ed
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2019-08-09 01:01:27 +02:00
parent 8f7e851346
commit bac0e8fd8d
6 changed files with 80 additions and 5 deletions

View File

@ -50,6 +50,7 @@
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.util.NB;
import org.eclipse.jgit.util.References;
/**
* A (possibly mutable) SHA-256 abstraction.
@ -76,8 +77,9 @@ public abstract class AnyLongObjectId implements Comparable<AnyLongObjectId> {
*/
public static boolean equals(final AnyLongObjectId firstObjectId,
final AnyLongObjectId secondObjectId) {
if (firstObjectId == secondObjectId)
if (References.isSameObject(firstObjectId, secondObjectId)) {
return true;
}
// We test word 2 first as odds are someone already used our
// word 1 as a hash code, and applying that came up with these

View File

@ -49,6 +49,7 @@
import java.nio.ByteBuffer;
import org.eclipse.jgit.util.NB;
import org.eclipse.jgit.util.References;
/**
* A (possibly mutable) SHA-1 abstraction.
@ -88,7 +89,7 @@ public static boolean equals(final AnyObjectId firstObjectId,
*/
public static boolean isEqual(final AnyObjectId firstObjectId,
final AnyObjectId secondObjectId) {
if (firstObjectId == secondObjectId) {
if (References.isSameObject(firstObjectId, secondObjectId)) {
return true;
}
// We test word 3 first since the git file-based ODB

View File

@ -49,6 +49,7 @@
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.util.References;
/**
* Describes how refs in one repository copy into another repository.
@ -585,8 +586,9 @@ public boolean equals(Object obj) {
}
private static boolean eq(String a, String b) {
if (a == b)
if (References.isSameObject(a, b)) {
return true;
}
if (a == null || b == null)
return false;
return a.equals(b);

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.References;
import org.eclipse.jgit.util.StringUtils;
/**
@ -624,8 +625,9 @@ public boolean equals(Object obj) {
}
private static boolean eq(String a, String b) {
if (a == b)
if (References.isSameObject(a, b)) {
return true;
}
if (StringUtils.isEmptyOrNull(a) && StringUtils.isEmptyOrNull(b))
return true;
if (a == null || b == null)

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.util;
/**
* Utility methods for object references
*
* @since 5.4
*/
public interface References {
/**
* Compare two references
*
* @param <T>
* type of the references
* @param ref1
* first reference
* @param ref2
* second reference
* @return {@code true} if both references refer to the same object
*/
@SuppressWarnings("ReferenceEquality")
public static <T> boolean isSameObject(T ref1, T ref2) {
return ref1 == ref2;
}
}

View File

@ -139,8 +139,9 @@ public static String capitalize(String str) {
* @return true if a equals b
*/
public static boolean equalsIgnoreCase(String a, String b) {
if (a == b)
if (References.isSameObject(a, b)) {
return true;
}
if (a.length() != b.length())
return false;
for (int i = 0; i < a.length(); i++) {