Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FinalParametersCheck |
|
| 2.5714285714285716;2.571 |
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; | |
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 | * Check that method/constructor/catch/foreach parameters are final. | |
27 | * The user can set the token set to METHOD_DEF, CONSTRUCTOR_DEF, | |
28 | * LITERAL_CATCH, FOR_EACH_CLAUSE or any combination of these token | |
29 | * types, to control the scope of this check. | |
30 | * Default scope is both METHOD_DEF and CONSTRUCTOR_DEF. | |
31 | * | |
32 | * @author lkuehne | |
33 | * @author o_sukhodolsky | |
34 | * @author Michael Studman | |
35 | */ | |
36 | 5 | public class FinalParametersCheck extends Check |
37 | { | |
38 | @Override | |
39 | public int[] getDefaultTokens() | |
40 | { | |
41 | 1 | return new int[] { |
42 | TokenTypes.METHOD_DEF, | |
43 | TokenTypes.CTOR_DEF, | |
44 | }; | |
45 | } | |
46 | ||
47 | @Override | |
48 | public int[] getAcceptableTokens() | |
49 | { | |
50 | 4 | return new int[] { |
51 | TokenTypes.METHOD_DEF, | |
52 | TokenTypes.CTOR_DEF, | |
53 | TokenTypes.LITERAL_CATCH, | |
54 | TokenTypes.FOR_EACH_CLAUSE, | |
55 | }; | |
56 | } | |
57 | ||
58 | @Override | |
59 | public void visitToken(DetailAST aAST) | |
60 | { | |
61 | // don't flag interfaces | |
62 | 58 | final DetailAST container = aAST.getParent().getParent(); |
63 | 58 | if (container.getType() == TokenTypes.INTERFACE_DEF) { |
64 | 2 | return; |
65 | } | |
66 | ||
67 | 56 | if (aAST.getType() == TokenTypes.LITERAL_CATCH) { |
68 | 4 | visitCatch(aAST); |
69 | } | |
70 | 52 | else if (aAST.getType() == TokenTypes.FOR_EACH_CLAUSE) { |
71 | 4 | visitForEachClause(aAST); |
72 | } | |
73 | else { | |
74 | 48 | visitMethod(aAST); |
75 | } | |
76 | 56 | } |
77 | ||
78 | /** | |
79 | * Checks parameters of the method or ctor. | |
80 | * @param aMethod method or ctor to check. | |
81 | */ | |
82 | private void visitMethod(final DetailAST aMethod) | |
83 | { | |
84 | // exit on fast lane if there is nothing to check here | |
85 | 48 | if (!aMethod.branchContains(TokenTypes.PARAMETER_DEF)) { |
86 | 8 | return; |
87 | } | |
88 | ||
89 | // ignore abstract method | |
90 | 40 | final DetailAST modifiers = |
91 | aMethod.findFirstToken(TokenTypes.MODIFIERS); | |
92 | 40 | if (modifiers.branchContains(TokenTypes.ABSTRACT)) { |
93 | 2 | return; |
94 | } | |
95 | ||
96 | // we can now be sure that there is at least one parameter | |
97 | 38 | final DetailAST parameters = |
98 | aMethod.findFirstToken(TokenTypes.PARAMETERS); | |
99 | 38 | DetailAST child = parameters.getFirstChild(); |
100 | 80 | while (child != null) { |
101 | // childs are PARAMETER_DEF and COMMA | |
102 | 42 | if (child.getType() == TokenTypes.PARAMETER_DEF) { |
103 | 38 | checkParam(child); |
104 | } | |
105 | 42 | child = child.getNextSibling(); |
106 | } | |
107 | 38 | } |
108 | ||
109 | /** | |
110 | * Checks parameter of the catch block. | |
111 | * @param aCatch catch block to check. | |
112 | */ | |
113 | private void visitCatch(final DetailAST aCatch) | |
114 | { | |
115 | 4 | checkParam(aCatch.findFirstToken(TokenTypes.PARAMETER_DEF)); |
116 | 4 | } |
117 | ||
118 | /** | |
119 | * Checks parameter of the for each clause. | |
120 | * @param aForEachClause for each clause to check. | |
121 | */ | |
122 | private void visitForEachClause(final DetailAST aForEachClause) | |
123 | { | |
124 | 4 | checkParam(aForEachClause.findFirstToken(TokenTypes.VARIABLE_DEF)); |
125 | 4 | } |
126 | ||
127 | /** | |
128 | * Checks if the given parameter is final. | |
129 | * @param aParam parameter to check. | |
130 | */ | |
131 | private void checkParam(final DetailAST aParam) | |
132 | { | |
133 | 46 | if (!aParam.branchContains(TokenTypes.FINAL)) { |
134 | 27 | final DetailAST paramName = aParam.findFirstToken(TokenTypes.IDENT); |
135 | 27 | final DetailAST firstNode = CheckUtils.getFirstNode(aParam); |
136 | 27 | log(firstNode.getLineNo(), firstNode.getColumnNo(), |
137 | "final.parameter", paramName.getText()); | |
138 | } | |
139 | 46 | } |
140 | } |