From 25f451277c756b747594623727d9d56fb686a6c2 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Thu, 9 Jun 2005 22:00:06 +0000 Subject: [PATCH] Harden equality of AbstractRule objects git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3553 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + pmd/src/net/sourceforge/pmd/AbstractRule.java | 51 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index d1bb13edc0..fc6e6bc163 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -13,6 +13,7 @@ Fixed bug which caused MissingSerialVersionUID to trigger on all interfaces that Modified NullAssignmentRule to catch null assignments in ternary expressions. Added two new node types - ASTCatchStatement and ASTFinallyStatement. Modified rule XML definition; it no longer includes a symboltable attribute. +Harden equality of AbstractRule objects (needed for the Eclipse plugin features) May 10, 2005 - 3.1: New rules: SimplifyStartsWith, UnnecessaryParentheses, CollapsibleIfStatements, UseAssertEqualsInsteadOfAssertTrue, UseAssertSameInsteadOfAssertTrue, UseStringBufferForStringAppends, SimplifyConditional, SingularField diff --git a/pmd/src/net/sourceforge/pmd/AbstractRule.java b/pmd/src/net/sourceforge/pmd/AbstractRule.java index 83ec58e177..16bdb93c15 100644 --- a/pmd/src/net/sourceforge/pmd/AbstractRule.java +++ b/pmd/src/net/sourceforge/pmd/AbstractRule.java @@ -3,19 +3,18 @@ */ package net.sourceforge.pmd; -import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; -import net.sourceforge.pmd.ast.ASTCompilationUnit; -import net.sourceforge.pmd.ast.ASTMethodDeclarator; -import net.sourceforge.pmd.ast.JavaParserVisitorAdapter; -import net.sourceforge.pmd.ast.SimpleNode; -import net.sourceforge.pmd.ast.ASTMethodDeclaration; -import net.sourceforge.pmd.symboltable.Scope; -import net.sourceforge.pmd.symboltable.MethodScope; - import java.util.Iterator; import java.util.List; import java.util.Properties; +import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; +import net.sourceforge.pmd.ast.ASTCompilationUnit; +import net.sourceforge.pmd.ast.ASTMethodDeclaration; +import net.sourceforge.pmd.ast.JavaParserVisitorAdapter; +import net.sourceforge.pmd.ast.SimpleNode; +import net.sourceforge.pmd.symboltable.MethodScope; +import net.sourceforge.pmd.symboltable.Scope; + public abstract class AbstractRule extends JavaParserVisitorAdapter implements Rule { protected String name = getClass().getName(); @@ -96,15 +95,41 @@ public abstract class AbstractRule extends JavaParserVisitorAdapter implements R this.message = message; } + /** + * Test if rules are equals. Rules are equals if + * 1. they have the same implementation class + * 2. they have the same name + * 3. they have the same priority + * 4. they share the same properties/values + */ public boolean equals(Object o) { - if (!(o instanceof Rule)) { - return false; + if (o == null) { + return false; // trivial } - return ((Rule) o).getName().equals(getName()); + + if (this == o) { + return true; // trivial + } + + Rule rule = null; + boolean equality = this.getClass().getName().equals(o.getClass().getName()); + + if (equality) { + rule = (Rule) o; + equality = this.getName().equals(rule.getName()) + && this.getPriority() == rule.getPriority() + && this.getProperties().equals(rule.getProperties()); + } + + return equality; } + /** + * Return a hash code to conform to equality. Try with a string. + */ public int hashCode() { - return getName().hashCode(); + String s = this.getClass().getName() + this.getName() + String.valueOf(this.getPriority()) + this.getProperties().toString(); + return s.hashCode(); } public void apply(List acus, RuleContext ctx) {