KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
25
26 import org.hammurapi.InspectorBase;
27
28 import com.pavelvlasov.config.ConfigurationException;
29 import com.pavelvlasov.config.Parameterizable;
30 import com.pavelvlasov.jsel.Initializer;
31 import com.pavelvlasov.jsel.JselException;
32 import com.pavelvlasov.jsel.Repository;
33 import com.pavelvlasov.jsel.VariableDefinition;
34 import com.pavelvlasov.jsel.expressions.Expression;
35 import com.pavelvlasov.jsel.expressions.MethodCall;
36
37 /**
38  * ER-066 Unify logging strategy - define individual logger for class
39  *
40  * @author Pavel Vlasov
41  * @version $Revision: 1.4 $
42  */

43 public class DefineLoggerForClassRule
44     extends InspectorBase
45     implements Parameterizable {
46     
47     /**
48      * Reviews the type definition and cheks if it has a logger field.
49      *
50      * @param type
51      * the type definition to be reviewed.
52      */

53     public void visit(com.pavelvlasov.jsel.Class clazz) {
54         if (loggerClassName!=null) {
55             Repository repository = clazz.getCompilationUnit().getPackage().getRepository();
56             try {
57                 Class JavaDoc loggerClass=repository.loadClass(loggerClassName);
58                 Iterator JavaDoc it=clazz.getFields().iterator();
59                 while (it.hasNext()) {
60                     Object JavaDoc field = it.next();
61                     if (field instanceof VariableDefinition) {
62                         VariableDefinition variableDefinition=(VariableDefinition) field;
63                         try {
64                             if (variableDefinition.getTypeSpecification().getType().isKindOf(loggerClassName)) {
65                                 context.waive(variableDefinition, "AvoidHidingInheritedInstanceFields");
66                                 
67                                 if (!("logger".equals(variableDefinition.getName()) || "LOGGER".equals(variableDefinition.getName()))) {
68                                     // TODO [0329] Move string to keyed message to descriptor
69
context.reportViolation(variableDefinition, "Use unified name 'LOGGER' for loggers");
70                                 }
71                                 
72                                 if (!variableDefinition.getModifiers().contains("static")) {
73                                     // TODO [0329] Move string to keyed message to descriptor
74
context.reportViolation(variableDefinition, "Logger must be static");
75                                 }
76                                 
77                                 if (!variableDefinition.getModifiers().contains("private")) {
78                                     // TODO [0329] Move string to keyed message to descriptor
79
context.reportViolation(variableDefinition, "Logger must be private");
80                                 }
81                                 
82                                 if (!variableDefinition.getModifiers().contains("final")) {
83                                     // TODO [0329] Move string to keyed message to descriptor
84
context.reportViolation(variableDefinition, "Logger must be final");
85                                 }
86                                                                 
87                                 Initializer initializer = variableDefinition.getInitializer();
88                                 if (initializer==null) {
89                                     context.reportViolation(variableDefinition, "Logger not initialized");
90                                 } else {
91                                     if (initializer instanceof MethodCall) {
92                                         MethodCall mc = (MethodCall) initializer;
93                                         if (mc.getParameters().size()==1) {
94                                             Expression expr = (Expression) mc.getParameters().get(0);
95                                             if ("java.lang.Class".equals(expr.getTypeSpecification().getType().getName())) {
96                                                 String JavaDoc paramTypeName = expr.toString();
97                                                 String JavaDoc classType = clazz.getFcn() + ".class";
98                                                 if (!paramTypeName.equals(classType) && !classType.endsWith("." + paramTypeName)) {
99
100                                                     context.reportViolation(variableDefinition, "Parameter is not correct");
101                                                 }
102                                             }
103                                         }
104                                     }
105                                 }
106                                 return;
107                             }
108                         } catch (JselException e) {
109                             context.warn(variableDefinition, e);
110                         }
111                     }
112                 }
113                 context.reportViolation(clazz);
114             } catch (ClassNotFoundException JavaDoc e) {
115                 context.warn(clazz, e);
116             }
117         }
118     }
119     
120     /**
121      * The fully quilified name of the Logger class.
122      */

123     private String JavaDoc loggerClassName;
124     
125     /**
126      * Configures rule. Reads in the values of the parameter logger-class-name
127      *
128      * @param name
129      * the name of the parameter being loaded from Hammurapi
130      * configuration
131      * @param value
132      * the value of the parameter being loaded from Hammurapi
133      * configuration
134      * @exception ConfigurationException
135      * in case of a not supported parameter
136      */

137     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
138         if ("logger-class-name".equals(name)) {
139             loggerClassName=value.toString();
140             return true;
141         }
142         
143         throw new ConfigurationException("Parameter " + name
144                 + " is not supported.");
145     }
146
147     /**
148      * Gives back the preconfigured values.
149      */

150     public String JavaDoc getConfigInfo() {
151         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed logger class:\n");
152         ret.append("logger-class-name: " + loggerClassName + "\n");
153         return ret.toString();
154     }
155 }
156
Popular Tags