KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > UnusedPrivateMethodCheck


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.usage;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.FullIdent;
23 import com.puppycrawl.tools.checkstyle.api.Scope;
24 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28  * <p>Checks that a private method is used.
29  * </p>
30  * <p>
31  * An example of how to configure the check is:
32  * </p>
33  * <pre>
34  * &lt;module name="usage.UnusedPrivateMethod"/&gt;
35  * </pre>
36  *
37  * @author Rick Giles
38  */

39 public class UnusedPrivateMethodCheck
40     extends AbstractUsageCheck
41
42 {
43     /** Controls if checks skips serialization methods.*/
44     private boolean mAllowSerializationMethods;
45     /** @see com.puppycrawl.tools.checkstyle.api.Check */
46     public int[] getDefaultTokens()
47     {
48         return new int[] {
49             TokenTypes.METHOD_DEF,
50         };
51     }
52
53     /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
54     public String JavaDoc getErrorKey()
55     {
56         return "unused.method";
57     }
58
59     /**
60      * Configure the check to allow (or not) serialization-related methods.
61      * @param aFlag new value for allowSerializationMethods value.
62      */

63     public void setAllowSerializationMethods(boolean aFlag)
64     {
65         mAllowSerializationMethods = aFlag;
66     }
67
68     /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
69     public boolean mustCheckReferenceCount(DetailAST aAST)
70     {
71         final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
72         if ((mods == null)
73             || (ScopeUtils.getScopeFromMods(mods) != Scope.PRIVATE))
74         {
75             return false;
76         }
77
78         return !mAllowSerializationMethods
79                || !(isWriteObject(aAST) || isReadObject(aAST)
80                     || isWriteReplaceOrReadResolve(aAST));
81     }
82
83     /**
84      * Checks if a given method is writeObject().
85      * @param aAST method def to check
86      * @return true if this is a writeObject() definition
87      */

88     private boolean isWriteObject(DetailAST aAST)
89     {
90         // name is writeObject...
91
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
92         if (!"writeObject".equals(ident.getText())) {
93             return false;
94         }
95
96         // returns void...
97
final DetailAST typeAST =
98             (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
99         if (typeAST.getType() != TokenTypes.LITERAL_VOID) {
100             return false;
101         }
102
103         // should have one parameter...
104
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
105         if (params == null || params.getChildCount() != 1) {
106             return false;
107         }
108         // and paramter's type should be java.io.ObjectOutputStream
109
final DetailAST type =
110             (DetailAST) ((DetailAST) params.getFirstChild())
111                 .findFirstToken(TokenTypes.TYPE).getFirstChild();
112         final String JavaDoc typeName = FullIdent.createFullIdent(type).getText();
113         if (!"java.io.ObjectOutputStream".equals(typeName)
114             && !"ObjectOutputStream".equals(typeName))
115         {
116             return false;
117         }
118
119         // and, finally, it should throws java.io.IOException
120
final DetailAST throwsAST =
121             aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
122         if (throwsAST == null || throwsAST.getChildCount() != 1) {
123             return false;
124         }
125         final DetailAST expt = (DetailAST) throwsAST.getFirstChild();
126         final String JavaDoc exceptionName = FullIdent.createFullIdent(expt).getText();
127         if (!"java.io.IOException".equals(exceptionName)
128             && !"IOException".equals(exceptionName))
129         {
130             return false;
131         }
132
133         return true;
134     }
135
136     /**
137      * Checks if a given method is readObject().
138      * @param aAST method def to check
139      * @return true if this is a readObject() definition
140      */

141     private boolean isReadObject(DetailAST aAST)
142     {
143         // name is readObject...
144
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
145         if (!"readObject".equals(ident.getText())) {
146             return false;
147         }
148
149         // returns void...
150
final DetailAST typeAST =
151             (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
152         if (typeAST.getType() != TokenTypes.LITERAL_VOID) {
153             return false;
154         }
155
156         // should have one parameter...
157
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
158         if (params == null || params.getChildCount() != 1) {
159             return false;
160         }
161         // and paramter's type should be java.io.ObjectInputStream
162
final DetailAST type =
163             (DetailAST) ((DetailAST) params.getFirstChild())
164                 .findFirstToken(TokenTypes.TYPE).getFirstChild();
165         final String JavaDoc typeName = FullIdent.createFullIdent(type).getText();
166         if (!"java.io.ObjectInputStream".equals(typeName)
167             && !"ObjectInputStream".equals(typeName))
168         {
169             return false;
170         }
171
172         // and, finally, it should throws java.io.IOException
173
// and java.lang.ClassNotFoundException
174
final DetailAST throwsAST =
175             aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
176         if (throwsAST == null || throwsAST.getChildCount() != 3) {
177             return false;
178         }
179         final DetailAST excpt1 = (DetailAST) throwsAST.getFirstChild();
180         final String JavaDoc exception1 = FullIdent.createFullIdent(excpt1).getText();
181         final String JavaDoc exception2 =
182             FullIdent.createFullIdent(throwsAST.getLastChild()).getText();
183         if (!"java.io.IOException".equals(exception1)
184             && !"IOException".equals(exception1)
185             && !"java.io.IOException".equals(exception2)
186             && !"IOException".equals(exception2)
187             || !"java.lang.ClassNotFoundException".equals(exception1)
188             && !"ClassNotFoundException".equals(exception1)
189             && !"java.lang.ClassNotFoundException".equals(exception2)
190             && !"ClassNotFoundException".equals(exception2))
191         {
192             return false;
193         }
194
195         return true;
196     }
197
198     /**
199      * Checks if a given method is writeReplace() or readResolve().
200      * @param aAST method def to check
201      * @return true if this is a writeReplace() definition
202      */

203     private boolean isWriteReplaceOrReadResolve(DetailAST aAST)
204     {
205         // name is writeReplace or readResolve...
206
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
207         if (!"writeReplace".equals(ident.getText())
208             && !"readResolve".equals(ident.getText()))
209         {
210             return false;
211         }
212
213         // returns Object...
214
final DetailAST typeAST =
215             (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
216         if (typeAST.getType() != TokenTypes.DOT
217             && typeAST.getType() != TokenTypes.IDENT)
218         {
219             return false;
220         }
221
222         // should have no parameters...
223
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
224         if (params != null && params.getChildCount() != 0) {
225             return false;
226         }
227
228         // and, finally, it should throws java.io.ObjectStreamException
229
final DetailAST throwsAST =
230             aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
231         if (throwsAST == null || throwsAST.getChildCount() != 1) {
232             return false;
233         }
234         final DetailAST excpt = (DetailAST) throwsAST.getFirstChild();
235         final String JavaDoc exception = FullIdent.createFullIdent(excpt).getText();
236         if (!"java.io.ObjectStreamException".equals(exception)
237             && !"ObjectStreamException".equals(exception))
238         {
239             return false;
240         }
241
242         return true;
243     }
244 }
245
Popular Tags