KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2005 CraftOfObjects
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: CraftOfObjects@gmail.com
22
23  */

24
25 package org.hammurapi.inspectors;
26
27 import java.util.Vector JavaDoc;
28
29 import org.hammurapi.HammurapiException;
30 import org.hammurapi.InspectorBase;
31
32 import com.pavelvlasov.jsel.CompilationUnit;
33 import com.pavelvlasov.jsel.JselException;
34 import com.pavelvlasov.jsel.LanguageElement;
35 import com.pavelvlasov.jsel.Method;
36 import com.pavelvlasov.jsel.Operation;
37 import com.pavelvlasov.jsel.TypeDefinition;
38 import com.pavelvlasov.jsel.VariableDefinition;
39
40 /**
41  * @author CraftOfObjects
42  *
43  * This inspector identify singleton pattern by checking for,
44  * those class variable type equal to the implementing class.
45  * ER-213 is reported.
46  *
47  * If the Singleton keeps not final variables > 1, the class is statefull.
48  * ER-212 is reported.
49  *
50  */

51 public class StatelessSingleton extends InspectorBase {
52   
53     private com.pavelvlasov.jsel.Class currentType = null ;
54     private boolean currentIsSingleton = false;
55     private Vector JavaDoc listOfNotFinalFields = null;
56     
57     
58     public void init (){;
59         currentIsSingleton = false;
60         listOfNotFinalFields = new Vector JavaDoc();
61     }
62         
63     public void visit(com.pavelvlasov.jsel.Class type) {
64         init();
65         currentType = type;
66     }
67     
68      public boolean isSingleton(VariableDefinition element) throws JselException {
69             LanguageElement parent=element.getParent();
70             if (parent instanceof TypeDefinition) {
71                 // System.out.println( " parent instanceof TypeDefinition TRUE " );
72
boolean ret = currentType.getFcn().equals( element.getTypeSpecification().getType().getName() );
73                 // System.out.println( " currentType.getFcn().equals( element.getTypeSpecification().getType().getName() --> " + ret );
74
return ret ;
75             } else {
76                 // System.out.println( " parent instanceof TypeDefinition FALSE " );
77
return false;
78             }
79         }
80
81         public void visit(VariableDefinition element) {
82             // // System.out.println( "++ " + element.getSignature() );
83

84             // is this a instance or class variable?
85
LanguageElement el = element.getParent();
86            // // System.out.println( "** " + el.getClass() );
87
if ( el.getClass().equals( com.pavelvlasov.jsel.impl.ClassImpl.class) ){
88             try {
89                 if (el != null && isSingleton(element) && element.getModifiers().contains("static")
90                     // || element.getModifiers().contains("final"))
91
) {
92                     context.info(currentType, "Singleton detected" );
93                     context.getSession().getContext("ER-213").reportViolation( element, "Singleton detected" );
94                     this.currentIsSingleton = true;
95                 }
96                 if ( ! element.getModifiers().contains("final") ){
97                     // System.out.println( " added !! " );
98
listOfNotFinalFields.add (element);
99                 }
100             } catch (JselException e) {
101                 context.warn(element.getEnclosingType(), e);
102             }
103             } // fi
104
}
105         
106         public void leave(TypeDefinition cu) throws HammurapiException {
107             // System.out.println( "leave: " + this.currentType.getName() );
108
this.checkStatelessSingleton();
109         }
110         
111         public void checkStatelessSingleton(){
112             
113             //-- the singleton can be NOT final
114
if ( currentIsSingleton && listOfNotFinalFields.size() > 1){
115                 context.info(currentType, "Singleton has " + listOfNotFinalFields.size() + " non final fields.");
116                 context.getSession().getContext("ER-212").reportViolation( currentType, "Singleton contains " + listOfNotFinalFields.size() + " not final fields (together with the Singleton field)" );
117             }
118         }
119 }
120
121
Popular Tags