| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractIllegalMethodCheck |
|
| 1.6666666666666667;1.667 |
| 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 | package com.puppycrawl.tools.checkstyle.checks.coding; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 24 | ||
| 25 | /** | |
| 26 | * Provide support for checking for a method with a specified name and no | |
| 27 | * arguments. | |
| 28 | * @author Oliver Burn | |
| 29 | */ | |
| 30 | public abstract class AbstractIllegalMethodCheck extends Check | |
| 31 | { | |
| 32 | /** Name of method to disallow. */ | |
| 33 | private final String mMethodName; | |
| 34 | /** The error key to report with. */ | |
| 35 | private final String mErrorKey; | |
| 36 | ||
| 37 | /** | |
| 38 | * Creates an instance. | |
| 39 | * @param aMethodName name of the method to disallow. | |
| 40 | * @param aErrorKey the error key to report with. | |
| 41 | */ | |
| 42 | public AbstractIllegalMethodCheck(String aMethodName, String aErrorKey) | |
| 43 | 3 | { |
| 44 | 3 | mMethodName = aMethodName; |
| 45 | 3 | mErrorKey = aErrorKey; |
| 46 | 3 | } |
| 47 | ||
| 48 | @Override | |
| 49 | public int[] getDefaultTokens() | |
| 50 | { | |
| 51 | 3 | return new int[] {TokenTypes.METHOD_DEF}; |
| 52 | } | |
| 53 | ||
| 54 | @Override | |
| 55 | public void visitToken(DetailAST aAST) | |
| 56 | { | |
| 57 | 23 | final DetailAST mid = aAST.findFirstToken(TokenTypes.IDENT); |
| 58 | 23 | final String methodName = mid.getText(); |
| 59 | ||
| 60 | 23 | if (mMethodName.equals(methodName)) { |
| 61 | ||
| 62 | 10 | final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); |
| 63 | 10 | final boolean hasEmptyParamList = |
| 64 | !params.branchContains(TokenTypes.PARAMETER_DEF); | |
| 65 | ||
| 66 | 10 | if (hasEmptyParamList) { |
| 67 | 7 | log(aAST.getLineNo(), mErrorKey); |
| 68 | } | |
| 69 | } | |
| 70 | 23 | } |
| 71 | } |