From bac0e8fd8d7b8a5b70009346d3ccbd5f2a3a2a5b Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 9 Aug 2019 01:01:27 +0200 Subject: [PATCH] [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 --- .../eclipse/jgit/lfs/lib/AnyLongObjectId.java | 4 +- .../src/org/eclipse/jgit/lib/AnyObjectId.java | 3 +- .../org/eclipse/jgit/transport/RefSpec.java | 4 +- .../org/eclipse/jgit/transport/URIish.java | 4 +- .../src/org/eclipse/jgit/util/References.java | 67 +++++++++++++++++++ .../org/eclipse/jgit/util/StringUtils.java | 3 +- 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/util/References.java diff --git a/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/lib/AnyLongObjectId.java b/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/lib/AnyLongObjectId.java index e6fe7408a..0a509acb7 100644 --- a/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/lib/AnyLongObjectId.java +++ b/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/lib/AnyLongObjectId.java @@ -50,6 +50,7 @@ import java.nio.ByteBuffer; 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 { */ 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 diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java index 4252e4138..4f90e6900 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java @@ -49,6 +49,7 @@ import java.io.Writer; 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 abstract class AnyObjectId implements Comparable { */ 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 diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/RefSpec.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/RefSpec.java index afd3adaac..16c6faf27 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/RefSpec.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/RefSpec.java @@ -49,6 +49,7 @@ import java.text.MessageFormat; 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 class RefSpec implements Serializable { } 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); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java index 34730d395..7ca9cc134 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java @@ -62,6 +62,7 @@ import java.util.regex.Pattern; 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 class URIish implements Serializable { } 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) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/References.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/References.java new file mode 100644 index 000000000..341fbfa94 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/References.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2019, Matthias Sohn + * 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 + * 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 boolean isSameObject(T ref1, T ref2) { + return ref1 == ref2; + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java index 3868e56f5..f4b6f9d0d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java @@ -139,8 +139,9 @@ public final class StringUtils { * @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++) {