Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EqualsHashCodeCheck
100%
30/30
58%
18/31
5
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.coding;
 21  
 
 22  
 import antlr.collections.AST;
 23  
 import com.google.common.collect.Maps;
 24  
 import com.google.common.collect.Sets;
 25  
 import com.puppycrawl.tools.checkstyle.api.Check;
 26  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 27  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 28  
 import java.util.Map;
 29  
 import java.util.Set;
 30  
 
 31  
 /**
 32  
  * <p>
 33  
  * Checks that classes that override equals() also override hashCode().
 34  
  * </p>
 35  
  * <p>
 36  
  * Rationale: The contract of equals() and hashCode() requires that
 37  
  * equal objects have the same hashCode. Hence, whenever you override
 38  
  * equals() you must override hashCode() to ensure that your class can
 39  
  * be used in collections that are hash based.
 40  
  * </p>
 41  
  * <p>
 42  
  * An example of how to configure the check is:
 43  
  * </p>
 44  
  * <pre>
 45  
  * &lt;module name="EqualsHashCode"/&gt;
 46  
  * </pre>
 47  
  * @author lkuehne
 48  
  */
 49  1
 public class EqualsHashCodeCheck
 50  
         extends Check
 51  
 {
 52  
     // implementation note: we have to use the following members to
 53  
     // keep track of definitions in different inner classes
 54  
 
 55  
     /** maps OBJ_BLOCK to the method definition of equals() */
 56  1
     private final Map<DetailAST, DetailAST> mObjBlockEquals = Maps.newHashMap();
 57  
 
 58  
     /** the set of OBJ_BLOCKs that contain a definition of hashCode() */
 59  1
     private final Set<DetailAST> mObjBlockWithHashCode = Sets.newHashSet();
 60  
 
 61  
     @Override
 62  
     public int[] getDefaultTokens()
 63  
     {
 64  1
         return new int[] {TokenTypes.METHOD_DEF};
 65  
     }
 66  
 
 67  
     @Override
 68  
     public void beginTree(DetailAST aRootAST)
 69  
     {
 70  1
         mObjBlockEquals.clear();
 71  1
         mObjBlockWithHashCode.clear();
 72  1
     }
 73  
 
 74  
     @Override
 75  
     public void visitToken(DetailAST aAST)
 76  
     {
 77  13
         final DetailAST modifiers = aAST.getFirstChild();
 78  13
         final AST type = aAST.findFirstToken(TokenTypes.TYPE);
 79  13
         final AST methodName = aAST.findFirstToken(TokenTypes.IDENT);
 80  13
         final DetailAST parameters = aAST.findFirstToken(TokenTypes.PARAMETERS);
 81  
 
 82  13
         if ((type.getFirstChild().getType() == TokenTypes.LITERAL_BOOLEAN)
 83  
                 && "equals".equals(methodName.getText())
 84  
                 && modifiers.branchContains(TokenTypes.LITERAL_PUBLIC)
 85  
                 && (parameters.getChildCount() == 1)
 86  
                 && isObjectParam(parameters.getFirstChild())
 87  
             )
 88  
         {
 89  5
             mObjBlockEquals.put(aAST.getParent(), aAST);
 90  
         }
 91  8
         else if ((type.getFirstChild().getType() == TokenTypes.LITERAL_INT)
 92  
                 && "hashCode".equals(methodName.getText())
 93  
                 && modifiers.branchContains(TokenTypes.LITERAL_PUBLIC)
 94  
                 && (parameters.getFirstChild() == null)) // no params
 95  
         {
 96  2
             mObjBlockWithHashCode.add(aAST.getParent());
 97  
         }
 98  13
     }
 99  
 
 100  
     /**
 101  
      * Determines if an AST is a formal param of type Object (or subclass).
 102  
      * @param aFirstChild the AST to check
 103  
      * @return true iff aFirstChild is a parameter of an Object type.
 104  
      */
 105  
     private boolean isObjectParam(AST aFirstChild)
 106  
     {
 107  7
         final AST modifiers = aFirstChild.getFirstChild();
 108  7
         final AST type = modifiers.getNextSibling();
 109  7
         switch (type.getFirstChild().getType()) {
 110  
         case TokenTypes.LITERAL_BOOLEAN:
 111  
         case TokenTypes.LITERAL_BYTE:
 112  
         case TokenTypes.LITERAL_CHAR:
 113  
         case TokenTypes.LITERAL_DOUBLE:
 114  
         case TokenTypes.LITERAL_FLOAT:
 115  
         case TokenTypes.LITERAL_INT:
 116  
         case TokenTypes.LITERAL_LONG:
 117  
         case TokenTypes.LITERAL_SHORT:
 118  2
             return false;
 119  
         default:
 120  5
             return true;
 121  
         }
 122  
     }
 123  
 
 124  
     @Override
 125  
     public void finishTree(DetailAST aRootAST)
 126  
     {
 127  1
         final Set<DetailAST> equalsDefs = mObjBlockEquals.keySet();
 128  1
         for (DetailAST objBlock : equalsDefs) {
 129  5
             if (!mObjBlockWithHashCode.contains(objBlock)) {
 130  3
                 final DetailAST equalsAST = mObjBlockEquals.get(objBlock);
 131  3
                 log(equalsAST.getLineNo(), equalsAST.getColumnNo(),
 132  
                         "equals.noHashCode");
 133  5
             }
 134  
         }
 135  
 
 136  1
         mObjBlockEquals.clear();
 137  1
         mObjBlockWithHashCode.clear();
 138  1
     }
 139  
 }