1 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 56 public class RequireThisCheck extends DeclarationCollector 57 { 58 59 private boolean mCheckFields = true; 60 61 private boolean mCheckMethods = true; 62 63 67 public void setCheckFields(boolean aCheckFields) 68 { 69 mCheckFields = aCheckFields; 70 } 71 74 public boolean getCheckFields() 75 { 76 return mCheckFields; 77 } 78 79 83 public void setCheckMethods(boolean aCheckMethods) 84 { 85 mCheckMethods = aCheckMethods; 86 } 87 90 public boolean getCheckMethods() 91 { 92 return mCheckMethods; 93 } 94 95 96 public RequireThisCheck() 97 { 98 } 99 100 101 public int[] getDefaultTokens() 102 { 103 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 117 public int[] getRequiredTokens() 118 { 119 return getDefaultTokens(); 120 } 121 122 123 public void visitToken(DetailAST aAST) 124 { 125 super.visitToken(aAST); 126 if (aAST.getType() == TokenTypes.IDENT) { 127 processIDENT(aAST); 128 } 129 } 131 137 private void processIDENT(DetailAST aAST) 138 { 139 final int parentType = aAST.getParent().getType(); 140 141 if (parentType == TokenTypes.METHOD_CALL) { 143 if (mCheckMethods) { 144 log(aAST, "require.this.method", aAST.getText()); 145 } 146 return; 147 } 148 149 if (!mCheckFields) { 151 return; 153 } 154 155 if (ScopeUtils.getSurroundingScope(aAST) == null) { 156 return; 160 } 161 162 if ((parentType == TokenTypes.DOT) 163 && (aAST.getPreviousSibling() != null)) 164 { 165 return; 167 } 168 if ((parentType == TokenTypes.TYPE) 169 || (parentType == TokenTypes.LITERAL_NEW)) 170 { 171 return; 173 } 174 if ((parentType == TokenTypes.VARIABLE_DEF) 175 || (parentType == TokenTypes.CTOR_DEF) 176 || (parentType == TokenTypes.METHOD_DEF) 177 || (parentType == TokenTypes.CLASS_DEF) 178 || (parentType == TokenTypes.ENUM_DEF) 179 || (parentType == TokenTypes.INTERFACE_DEF) 180 || (parentType == TokenTypes.PARAMETER_DEF)) 181 { 182 return; 184 } 185 186 final String name = aAST.getText(); 187 if (isClassField(name)) { 188 log(aAST, "require.this.variable", name); 189 } 190 } 191 } | Popular Tags |