KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > NamingStandardRule


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors;
24
25 import org.hammurapi.InspectorBase;
26
27 import com.pavelvlasov.jsel.Method;
28 import com.pavelvlasov.jsel.Parameter;
29 import com.pavelvlasov.jsel.TypeDefinition;
30 import com.pavelvlasov.jsel.VariableDefinition;
31
32 /**
33  * Classes, methods and variables should be named according to Sun's naming convention.
34  *
35  * @author Pavel Vlasov
36  * @version $Revision: 1.4 $
37  */

38 public class NamingStandardRule extends InspectorBase {
39     
40     /**
41      * Reviews the name of the classes.
42      *
43      * @param typeDefinition the typedefinition to be reviewed.
44      */

45     public void visit(TypeDefinition typeDefinition) {
46         if (typeDefinition.getName().indexOf('_')!=-1 || !Character.isUpperCase(typeDefinition.getName().charAt(0))) {
47             context.reportViolation(typeDefinition);
48         }
49     }
50     
51     
52     /**
53      * Reviews the name of the methods.
54      *
55      * @param method the method to be reviewed
56      */

57     public void visit(Method method) {
58         if (method.getName().indexOf('_')!=-1 || !Character.isLowerCase(method.getName().charAt(0))) {
59             context.reportViolation(method);
60         }
61     }
62     
63     /**
64      * Reviews the name of the attributes.
65      *
66      * @param variableDefinition the variable definition to be reviewed.
67      */

68     public void visit(VariableDefinition variableDefinition) {
69         if (variableDefinition.getModifiers().contains("static") && variableDefinition.getModifiers().contains("final")) {
70             if (!"serialVersionUID".equals(variableDefinition.getName()) && !variableDefinition.getName().toUpperCase().equals(variableDefinition.getName())) {
71                 context.reportViolation(variableDefinition);
72             }
73         } else if (variableDefinition.getName().indexOf('_')!=-1 || !Character.isLowerCase(variableDefinition.getName().charAt(0))) {
74             context.reportViolation(variableDefinition);
75         } else if ("enum".equals(variableDefinition.getName())) {
76             context.reportViolation(variableDefinition);
77         }
78     }
79     
80     /**
81      * Reviews the name of the parameters.
82      *
83      * @param parameter the parameter declaration to be reviewed.
84      */

85     public void visit(Parameter parameter) {
86         if (parameter.getName().indexOf('_')!=-1 || !Character.isLowerCase(parameter.getName().charAt(0))) {
87             context.reportViolation(parameter);
88         } else if ("enum".equals(parameter.getName())) {
89             context.reportViolation(parameter);
90         }
91     }
92     
93     public void visit(com.pavelvlasov.jsel.Package pkg) {
94         if (pkg.getName().toLowerCase()!=pkg.getName()) {
95             context.reportViolation(null, "Package name shall be in lower case: "+pkg.getName());
96         }
97         
98         if (pkg.getName().indexOf('_')!=-1) {
99             context.reportViolation(null, "Packages name shall not contain underscore character: "+pkg.getName());
100         }
101     }
102 }
103
Popular Tags