From be08323f8a58f992bc61171bf53e0e51dbd21f29 Mon Sep 17 00:00:00 2001 From: Michal Kordas Date: Sat, 23 May 2015 21:44:32 +0200 Subject: [PATCH] Fix equals and hashCode in SuppressionFilter and remove toString. #1088 --- pom.xml | 2 +- .../checkstyle/filters/SuppressionFilter.java | 24 ++++++------- .../filters/SuppressionFilterTest.java | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilterTest.java diff --git a/pom.xml b/pom.xml index 5bcb8efdc..acb6a632d 100644 --- a/pom.xml +++ b/pom.xml @@ -886,7 +886,7 @@ .*.filters.SuppressElement8288 .*.filters.SuppressionCommentFilter8387 .*.filters.SuppressionCommentFilter\$.*4169 - .*.filters.SuppressionFilter00 + .*.filters.SuppressionFilter10058 .*.filters.SuppressionsLoader6877 .*.filters.SuppressWithNearbyCommentFilter7689 .*.filters.SuppressWithNearbyCommentFilter\$Tag8878 diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilter.java index 7635f71d0..44040f70a 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilter.java @@ -25,6 +25,8 @@ import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Filter; import com.puppycrawl.tools.checkstyle.api.FilterSet; +import java.util.Objects; + /** *

* This filter accepts AuditEvents according to file, check, line, and @@ -55,21 +57,19 @@ public class SuppressionFilter } @Override - public String toString() { - return filters.toString(); + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final SuppressionFilter that = (SuppressionFilter) obj; + return Objects.equals(filters, that.filters); } @Override public int hashCode() { - return filters.hashCode(); - } - - @Override - public boolean equals(Object object) { - if (object instanceof SuppressionFilter) { - final SuppressionFilter other = (SuppressionFilter) object; - return this.filters.equals(other.filters); - } - return false; + return Objects.hash(filters); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilterTest.java new file mode 100644 index 000000000..ecae369c0 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionFilterTest.java @@ -0,0 +1,35 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2015 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.filters; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; +import org.junit.Test; + +public class SuppressionFilterTest { + @Test + public void testEqualsAndHashCode() { + EqualsVerifier + .forClass(SuppressionFilter.class) + .usingGetClass() + .suppress(Warning.NONFINAL_FIELDS) + .verify(); + } +}