|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RequireThisCheck | Line # 56 | 88.9% |
0.8888889
|
26.93 | 27 | 26 | 26 | 0.96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (4) | |||
| Result | |||
|
0.7777778
|
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testFieldsOnly
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testFieldsOnly
|
1 PASS | |
|
0.7592593
|
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testIt
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testIt
|
1 PASS | |
|
0.5555556
|
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testGenerics
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testGenerics
|
1 PASS | |
|
0.42592594
|
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testMethodsOnly
com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckTest.testMethodsOnly
|
1 PASS | |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2005 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.DetailAST; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.ScopeUtils; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 24 | import com.puppycrawl.tools.checkstyle.checks.DeclarationCollector; | |
| 25 | ||
| 26 | /** | |
| 27 | * <p>Checks that code doesn't rely on the "this" default. | |
| 28 | * That is references to instance variables and methods of the present | |
| 29 | * object are explicitly of the form "this.varName" or | |
| 30 | * "this.methodName(args)". | |
| 31 | *</p> | |
| 32 | * <p>Examples of use: | |
| 33 | * <pre> | |
| 34 | * <module name="RequireThis"/> | |
| 35 | * </pre> | |
| 36 | * An example of how to configure to check <code>this</code> qualifier for | |
| 37 | * methods only: | |
| 38 | * <pre> | |
| 39 | * <module name="RequireThis"> | |
| 40 | * <property name="checkFields" value="false"/> | |
| 41 | * <property name="checkMethods" value="true"/> | |
| 42 | * </module> | |
| 43 | * </pre> | |
| 44 | * </p> | |
| 45 | * <p>Limitations: I'm not currently doing anything about static variables | |
| 46 | * or catch-blocks. Static methods invoked on a class name seem to be OK; | |
| 47 | * both the class name and the method name have a DOT parent. | |
| 48 | * Non-static methods invoked on either this or a variable name seem to be | |
| 49 | * OK, likewise.</p> | |
| 50 | * <p>Much of the code for this check was cribbed from Rick Giles's | |
| 51 | * <code>HiddenFieldCheck</code>.</p> | |
| 52 | * | |
| 53 | * @author Stephen Bloch | |
| 54 | * @author o_sukhodolsky | |
| 55 | */ | |
| 56 | public class RequireThisCheck extends DeclarationCollector | |
| 57 | { | |
| 58 | /** whether we should check fields usage. */ | |
| 59 | private boolean mCheckFields = true; | |
| 60 | /** whether we should check methods usage. */ | |
| 61 | private boolean mCheckMethods = true; | |
| 62 | ||
| 63 | /** | |
| 64 | * Setter for checkFields property. | |
| 65 | * @param aCheckFields should we check fields usage or not. | |
| 66 | */ | |
| 67 | 1 |
public void setCheckFields(boolean aCheckFields) |
| 68 | { | |
| 69 | 1 | mCheckFields = aCheckFields; |
| 70 | } | |
| 71 | /** | |
| 72 | * @return true if we should check fields usage false overwise. | |
| 73 | */ | |
| 74 | 0 |
public boolean getCheckFields() |
| 75 | { | |
| 76 | 0 | return mCheckFields; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Setter for checkMethods property. | |
| 81 | * @param aCheckMethods should we check methods usage or not. | |
| 82 | */ | |
| 83 | 1 |
public void setCheckMethods(boolean aCheckMethods) |
| 84 | { | |
| 85 | 1 | mCheckMethods = aCheckMethods; |
| 86 | } | |
| 87 | /** | |
| 88 | * @return true if we should check methods usage false overwise. | |
| 89 | */ | |
| 90 | 0 |
public boolean getCheckMethods() |
| 91 | { | |
| 92 | 0 | return mCheckMethods; |
| 93 | } | |
| 94 | ||
| 95 | /** Creates new instance of the check. */ | |
| 96 | 4 |
public RequireThisCheck() |
| 97 | { | |
| 98 | } | |
| 99 | ||
| 100 | /** {@inheritDoc} */ | |
| 101 | 4 |
public int[] getDefaultTokens() |
| 102 | { | |
| 103 | 4 | return new int[] { |
| 104 | TokenTypes.CLASS_DEF, | |
| 105 | TokenTypes.CTOR_DEF, | |
| 106 | TokenTypes.ENUM_DEF, | |
| 107 | TokenTypes.IDENT, | |
| 108 | TokenTypes.INTERFACE_DEF, | |
| 109 | TokenTypes.METHOD_DEF, | |
| 110 | TokenTypes.PARAMETER_DEF, | |
| 111 | TokenTypes.SLIST, | |
| 112 | TokenTypes.VARIABLE_DEF, | |
| 113 | }; | |
| 114 | } | |
| 115 | ||
| 116 | /** {@inheritDoc} */ | |
| 117 | 0 |
public int[] getRequiredTokens() |
| 118 | { | |
| 119 | 0 | return getDefaultTokens(); |
| 120 | } | |
| 121 | ||
| 122 | /** {@inheritDoc} */ | |
| 123 | 215 |
public void visitToken(DetailAST aAST) |
| 124 | { | |
| 125 | 215 | super.visitToken(aAST); |
| 126 | 215 | if (aAST.getType() == TokenTypes.IDENT) { |
| 127 | 141 | processIDENT(aAST); |
| 128 | } | |
| 129 | } // end visitToken | |
| 130 | ||
| 131 | /** | |
| 132 | * Checks if a given IDENT is method call or field name which | |
| 133 | * require explicit <code>this</code> qualifier. | |
| 134 | * | |
| 135 | * @param aAST IDENT to check. | |
| 136 | */ | |
| 137 | 141 |
private void processIDENT(DetailAST aAST) |
| 138 | { | |
| 139 | 141 | final int parentType = aAST.getParent().getType(); |
| 140 | ||
| 141 | // let's check method calls | |
| 142 | 141 | if (parentType == TokenTypes.METHOD_CALL) { |
| 143 | 3 | if (mCheckMethods) { |
| 144 | 2 | log(aAST, "require.this.method", aAST.getText()); |
| 145 | } | |
| 146 | 3 | return; |
| 147 | } | |
| 148 | ||
| 149 | // let's check fields | |
| 150 | 138 | if (!mCheckFields) { |
| 151 | // we shouldn't check fields | |
| 152 | 38 | return; |
| 153 | } | |
| 154 | ||
| 155 | 100 | if (ScopeUtils.getSurroundingScope(aAST) == null) { |
| 156 | // it is not a class or inteface it's | |
| 157 | // either import or package | |
| 158 | // we shouldn't checks this | |
| 159 | 6 | return; |
| 160 | } | |
| 161 | ||
| 162 | 94 | if ((parentType == TokenTypes.DOT) |
| 163 | && (aAST.getPreviousSibling() != null)) | |
| 164 | { | |
| 165 | // it's the method name in a method call; no problem | |
| 166 | 14 | return; |
| 167 | } | |
| 168 | ||