KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > modifier > RedundantModifierCheck


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.modifier;
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 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
25
26 /**
27  * Checks for redundant modifiers in interface and annotation definitions.
28  * Also checks for redundant final modifiers on methods of final classes.
29  *
30  * @author lkuehne
31  */

32 public class RedundantModifierCheck
33     extends Check
34 {
35     /** {@inheritDoc} */
36     public int[] getDefaultTokens()
37     {
38         return new int[] {
39             TokenTypes.METHOD_DEF,
40             TokenTypes.VARIABLE_DEF,
41             TokenTypes.ANNOTATION_FIELD_DEF,
42         };
43     }
44
45     /** {@inheritDoc} */
46     public int[] getRequiredTokens()
47     {
48         return new int[] {};
49     }
50
51     /** {@inheritDoc} */
52     public void visitToken(DetailAST aAST)
53     {
54         if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) {
55             final DetailAST modifiers =
56                 aAST.findFirstToken(TokenTypes.MODIFIERS);
57
58             DetailAST modifier = (DetailAST) modifiers.getFirstChild();
59             while (modifier != null) {
60
61                 // javac does not allow final or static in interface methods
62
// order annotation fields hence no need to check that this
63
// is not a method or annotation field
64

65                 final int type = modifier.getType();
66                 if ((type == TokenTypes.LITERAL_PUBLIC)
67                     || (type == TokenTypes.ABSTRACT)
68                     || (type == TokenTypes.LITERAL_STATIC)
69                     || (type == TokenTypes.FINAL))
70                 {
71                     log(modifier.getLineNo(),
72                         modifier.getColumnNo(),
73                         "redundantModifier",
74                         new String JavaDoc[] {modifier.getText()});
75                     break;
76                 }
77
78                 modifier = (DetailAST) modifier.getNextSibling();
79             }
80         }
81         else if (aAST.getType() == TokenTypes.METHOD_DEF) {
82             final DetailAST modifiers =
83                             aAST.findFirstToken(TokenTypes.MODIFIERS);
84             // private method?
85
boolean checkFinal =
86                 modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
87             // declared in a final class?
88
DetailAST parent = aAST.getParent();
89             while (parent != null) {
90                 if (parent.getType() == TokenTypes.CLASS_DEF) {
91                     final DetailAST classModifiers =
92                         parent.findFirstToken(TokenTypes.MODIFIERS);
93                     checkFinal |=
94                         classModifiers.branchContains(TokenTypes.FINAL);
95                     break;
96                 }
97                 parent = parent.getParent();
98             }
99             if (checkFinal) {
100                 DetailAST modifier = (DetailAST) modifiers.getFirstChild();
101                 while (modifier != null) {
102                     final int type = modifier.getType();
103                     if (type == TokenTypes.FINAL) {
104                         log(modifier.getLineNo(),
105                             modifier.getColumnNo(),
106                             "redundantModifier",
107                             new String JavaDoc[] {modifier.getText()});
108                         break;
109                     }
110                     modifier = (DetailAST) modifier.getNextSibling();
111                 }
112             }
113         }
114     }
115 }
116
Popular Tags