KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > Coupling


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Johannes Bellert
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.com
21  * e-Mail: Johannes.Bellert@ercgroup.com
22  *
23  * * Created on Apr 11, 2004
24  *
25  */

26 package org.hammurapi.inspectors.metrics;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33
34 import com.pavelvlasov.jsel.Parameter;
35 import com.pavelvlasov.jsel.Type;
36 import com.pavelvlasov.jsel.TypeBody;
37 import com.pavelvlasov.jsel.TypeDefinition;
38 import com.pavelvlasov.jsel.TypeSpecification;
39 import com.pavelvlasov.jsel.VariableDefinition;
40 import com.pavelvlasov.jsel.impl.ClassImpl;
41 import com.pavelvlasov.jsel.impl.ClassTypeSpecificationImpl;
42
43 /**
44  * @author mucbj0
45  *
46  * JDepend
47  *
48  * Afferent Couplings (Ca)
49  * The number of other packages that depend upon classes within the package
50  * is an indicator of the package's responsibility.
51  *
52  * Efferent Couplings (Ce)
53  * The number of other packages that the classes in the package depend upon
54  * is an indicator of the package's independence.
55  *
56  **/

57 public class Coupling {
58
59     public Hashtable JavaDoc classCouplingMetric = new Hashtable JavaDoc();
60     public Hashtable JavaDoc packageCouplingMetric = new Hashtable JavaDoc();
61     public Hashtable JavaDoc allClasses = null;
62     public Hashtable JavaDoc allMethodsImplemented = new Hashtable JavaDoc();
63     public Hashtable JavaDoc allMethodsInvoked = new Hashtable JavaDoc();
64
65     public Hashtable JavaDoc allVariables = new Hashtable JavaDoc();
66     
67
68     //!! tbd: Set an meaningful name to the CouplingMetricOfPackage or use a different class
69
private CouplingMetricOfPackage totalCouplingMetricOfProject = new CouplingMetricOfPackage("Project");
70
71 /* public Coupling(ClassMap _classes, File _projectBaseDir){
72         super();
73         allClasses = _classes;
74         projectBaseDir = _projectBaseDir;
75     }
76
77     public void init(){
78         logger.debug( "-> init " );
79         Enumeration enum = allClasses.elements();
80         while (enum.hasMoreElements()){
81             Type c =(Type)enum.nextElement();
82             logger.debug( "Class " + c.toKey() );
83             CouplingMetricOfClass cmc = new CouplingMetricOfClass(c);
84             this.classCouplingMetric.put( c.toKey(), cmc);
85         }
86         // logger.debug( "this.couplingMetric " + this.couplingMetric );
87         logger.debug( "<- init " );
88         return;
89     };
90 */

91     
92     
93     //-------------------------------------------------------------------------
94

95     public void incrementVariableCoupling( Parameter parameter){
96
97         try {
98         if (parameter.getTypeSpecification() instanceof ClassTypeSpecificationImpl ) {
99                 TypeSpecification ts = parameter.getTypeSpecification();
100                 CouplingMetricOfClass cmcParent =
101                     (CouplingMetricOfClass)classCouplingMetric.get( parameter.getParent().getEnclosingType().getFcn() );
102                 CouplingMetricOfClass cmcVar = (CouplingMetricOfClass)classCouplingMetric.get( ts.getName() );
103                 if( cmcVar == null){
104                     classCouplingMetric.put( ts.getName(), new CouplingMetricOfClass( ts ) );
105                 }else{
106                     cmcVar.afferentVariableCounter++;
107                 }
108                 if( cmcParent == null){
109                     classCouplingMetric.put( parameter.getParent().getEnclosingType().getFcn(),
110                             new CouplingMetricOfClass( parameter.getParent().getEnclosingType() ) );
111                 }else{
112                     cmcParent.efferentVariableCounter++;
113                 }
114             // System.out.println( cmcVar );
115
// System.out.println( cmcParent );
116
}
117         
118         } catch (Exception JavaDoc e) {
119             // TODO Auto-generated catch block
120
e.printStackTrace();
121         }
122         
123     
124     }
125     
126     public String JavaDoc determineKey( VariableDefinition var){
127         String JavaDoc str = "<undefined>";
128         if( var != null){
129             if( var.getParent() instanceof ClassImpl) {
130                 return ((ClassImpl)var.getParent()).getFcn();
131             } else {
132                 // System.out.println( ((ClassImpl)var.getCompilationUnit().getTypes().get(0)).getFcn() );
133
}
134         }
135         
136         return str;
137     }
138     
139     public void incrementVariableCoupling( VariableDefinition var){
140         try {
141             // System.out.println( ">> " + var.getTypeSpecification().getName() );
142
// System.out.println( ">> " + var.getParent().getEnclosingType().getFcn());
143
if( var != null){
144         
145                 String JavaDoc key = determineKey( var) ;
146             CouplingMetricOfClass cmcParent = (CouplingMetricOfClass)classCouplingMetric.get( key );
147             CouplingMetricOfClass cmcVar = (CouplingMetricOfClass)classCouplingMetric.get( var.getTypeSpecification().getName() );
148             if( cmcVar == null){
149                 classCouplingMetric.put( var.getTypeSpecification().getName(), new CouplingMetricOfClass( var.getTypeSpecification() ) );
150             }else{
151                 cmcVar.afferentVariableCounter++;
152             }
153             if( cmcParent == null){
154                 TypeBody parentEnclosingType = var.getParent().getEnclosingType();
155                 if (parentEnclosingType!=null) {
156                     classCouplingMetric.put( parentEnclosingType.getFcn(), new CouplingMetricOfClass( parentEnclosingType ) );
157                 }
158             }else{
159                 cmcParent.efferentVariableCounter++;
160             }
161             // System.out.println( cmcVar );
162
// System.out.println( cmcParent );
163
}
164             
165         } catch (Exception JavaDoc e) {
166             // TODO Auto-generated catch block
167
e.printStackTrace();
168         }
169     
170     }
171     
172     
173     
174 //-------------------------------------------------------------------------
175

176     public void doIt(){
177 // init();
178

179         Enumeration JavaDoc classEnum = allClasses.elements();
180         while (classEnum.hasMoreElements()){
181             TypeDefinition c =(TypeDefinition)classEnum.nextElement();
182             CouplingMetricOfClass thisCmc = (CouplingMetricOfClass)this.classCouplingMetric.get( c.getFcn() );
183
184 //!! putAll reinitialize allMethodsImplemented !!
185
// allMethodsImplemented.addAll(c.getMethodMap());
186

187             //determineMethodMetricForClass( c, thisCmc);
188
determineVariableMetricForClass( c, thisCmc);
189
190         }
191         this.aggregateToPackageMetric();
192         this.findDefects();
193         this.reporting();
194         return;
195     }
196
197
198     public void determineVariableMetricForClass(
199             TypeDefinition c,
200         CouplingMetricOfClass thisCmc) {
201 //!! job
202
/*
203         Enumeration enumMet = c.getMethodMap().elements();
204
205         // -- all local variables
206         while (enumMet.hasMoreElements()) {
207             MethodImplemented mi = (MethodImplemented) enumMet.nextElement();
208
209             Enumeration enumslvm = mi.getLocalVarMap().elements();
210             while (enumslvm.hasMoreElements()) {
211                 Variable v = (Variable) enumslvm.nextElement();
212                 assignVariableCountersTo(v, thisCmc);
213             }
214         }
215
216         Enumeration enumsivm = c.getInstanceVarMap().elements();
217         while (enumsivm.hasMoreElements()) {
218             Variable v = (Variable) enumsivm.nextElement();
219             assignVariableCountersTo(v, thisCmc);
220         }
221 */

222     }
223
224 public void assignVariableCountersTo(VariableDefinition v, CouplingMetricOfClass thisCmc) {
225     //!!
226
CouplingMetricOfClass cmc = (CouplingMetricOfClass) this.classCouplingMetric.get(v.getName());
227         // lazy intialization
228

229         if (cmc == null) {
230     // cmc = initializeCouplingMetricOfClassFor(v.getName());
231
}
232
233         //!! not correcth
234
// cmc.listAfferentTypes.add( (Type)v.getType() );
235
cmc.afferentVariableCounter++;
236     // thisCmc.listEfferentTypes.add(thisCmc.type);
237
thisCmc.efferentVariableCounter++;
238     }
239
240 public void determineMethodMetricForClass(Type c, CouplingMetricOfClass thisCmc) {
241     /*
242     Enumeration enumMet = c.getMethodMap().elements();
243     while (enumMet.hasMoreElements()) {
244         MethodImplemented mi = (MethodImplemented) enumMet.nextElement();
245         // logger.debug( "\t" +mi.toString() );
246
247         Enumeration enumMk = mi.getInvokedMethods().elements();
248         while (enumMk.hasMoreElements()) {
249             MethodInvoked mk = (MethodInvoked) enumMk.nextElement();
250             logger.debug("getSignature: " + mk.getSignature());
251             logger.debug("getImplementorType: " + mk.getImplementorType());
252
253             if (mk.getServiceVariable() != null && mk.getServiceVariable().getType() != null) {
254                 logger.debug("getServiceVariable: " + mk.getServiceVariable());
255                 logger.debug("mk.getServiceVariable().getType(): " + mk.getServiceVariable().getType());
256
257                 if (mk.getServiceVariable().getType().equals(c)) {
258                     thisCmc.reflectiveMethodCounter++;
259                 } else {
260                     CouplingMetricOfClass cmc =
261                         (CouplingMetricOfClass) this.classCouplingMetric.get(mk.getServiceVariable().getType().toKey());
262                     // lazy intialization
263
264                     if (cmc == null) {
265                         cmc = initializeCouplingMetricOfClassFor(mk.getServiceVariable().getType());
266                     }
267         //!! not correct
268                     cmc.listAfferentTypes.add(c);
269                     cmc.afferentMethodCounter++;
270                     thisCmc.listEfferentTypes.add(mk.getServiceVariable().getType());
271                     thisCmc.efferentMethodCounter += mk.occurranceCounter;
272
273                     //-- thisCmc is per se in the project scope: no check needed
274                     //!! tbd: parameterize root package check
275                     if (cmc.type.getRootPackage() != null
276                         && cmc.type.getRootPackage().getRootPackageName() != null
277                         && (cmc.type.getRootPackage().getRootPackageName().startsWith("com.")
278                             || cmc.type.getRootPackage().getRootPackageName().startsWith("org.apache.jsp"))) {
279                         cmc.afferentMethodCounterProjectInternal++;
280                         thisCmc.efferentMethodCounterProjectInternal += mk.occurranceCounter;
281                     }
282
283                     logger.debug("cmc.efferentMethodUnresolvedCounter++ " + cmc.type.getSignature());
284                 }
285             } else {
286                 logger.debug("thisCmc.efferentMethodUnresolvedCounter++ " + thisCmc.type.getSignature());
287                 thisCmc.efferentMethodUnresolvedCounter++;
288             }
289         }
290     }
291     */

292 }
293
294     public void aggregateToPackageMetric() {
295         /*
296         String rootPack = "";
297         Enumeration enum = classCouplingMetric.elements();
298         while (enum.hasMoreElements()) {
299             CouplingMetricOfClass cCmc =
300                 (CouplingMetricOfClass) enum.nextElement();
301             try {
302                 if (cCmc.type != null
303                     && cCmc.type.getRootPackage() != null
304                     && cCmc.type.getRootPackage().getRootPackageName() != null) {
305
306                 rootPack = cCmc.type.getRootPackage().getRootPackageName();
307                     }
308                 if (rootPack != null && !"".equals(rootPack)) {
309
310                     logger.fatal("rootPack is: " + rootPack);
311                     CouplingMetricOfPackage pCmc = (CouplingMetricOfPackage) this.packageCouplingMetric.get(rootPack);
312
313                     // lazy init
314                     if (pCmc == null) {
315                         logger.debug("Insert Entry: " + rootPack);
316                         pCmc = new CouplingMetricOfPackage(rootPack);
317                         this.packageCouplingMetric.put(rootPack, pCmc);
318                     }
319                     pCmc.aggregate(cCmc);
320                     this.totalCouplingMetricOfProject.aggregate(pCmc);
321                 }
322
323             } catch (Exception e) {
324                 logger.fatal(
325                     "Issues in this.packageCouplingMetric.get(cmc.type.getRootPackage());",
326                     e);
327             }
328         }
329         */

330     }
331
332     public void findDefects(){
333         Enumeration JavaDoc metricEnum = classCouplingMetric.elements();
334         while (metricEnum.hasMoreElements()){
335             CouplingMetricOfClass cmc = (CouplingMetricOfClass)metricEnum.nextElement();
336             // logger.debug(cmc.toXml());
337
}
338         Enumeration JavaDoc enump = packageCouplingMetric.elements();
339         while (enump.hasMoreElements()){
340             CouplingMetricOfPackage cmc = (CouplingMetricOfPackage)enump.nextElement();
341         }
342
343     }
344     public CouplingMetricOfClass initializeCouplingMetricOfClassFor(Type c){
345         /*
346         CouplingMetricOfClass cmc = new CouplingMetricOfClass(c);
347         this.classCouplingMetric.put(c.toKey(), cmc);
348         logger.debug( "<- initializeCouplingMetricOfClassFor");
349         return cmc;
350         */

351         return null;
352     }
353
354     public void reporting() {
355         /*
356         String extension = ".xml";
357         String dir = ".";
358         File outFile =
359             new File(
360                 projectBaseDir,
361                 File.separatorChar
362                     + "results"
363                     + File.separatorChar
364                     + "CouplingMetricOfPackage"
365                     + extension);
366         try {
367
368             FileWriter fw = new FileWriter(outFile);
369             fw.write("<Coupling>");
370             fw.write("<ListCouplingMetricOfPackage>");
371             fw.write("<ProjectSummary>");
372             fw.write(this.totalCouplingMetricOfProject.toXml().toString());
373             fw.write("</ProjectSummary>");
374             fw.flush();
375             Enumeration enump = packageCouplingMetric.elements();
376             while (enump.hasMoreElements()) {
377                 CouplingMetricOfPackage cmc =
378                     (CouplingMetricOfPackage) enump.nextElement();
379                 fw.write(cmc.toXml().toString());
380                 fw.flush();
381             }
382             fw.write("</ListCouplingMetricOfPackage>");
383
384             fw.write("<ListCouplingMetricOfClasses>");
385             Enumeration enumc = classCouplingMetric.elements();
386             while (enumc.hasMoreElements()) {
387                 CouplingMetricOfClass cmc =
388                     (CouplingMetricOfClass) enumc.nextElement();
389
390                 fw.write(cmc.toXml().toString());
391                 fw.flush();
392
393             }
394             fw.write("</ListCouplingMetricOfClasses>");
395             fw.write("</Coupling>");
396             fw.close();
397         } catch (Exception e) {
398             e.printStackTrace();
399         }
400
401 //
402 // try {
403 // outFile =
404 // new File(
405 // projectBaseDir,
406 // File.separatorChar
407 // + "results"
408 // + File.separatorChar
409 // + "CouplingMetricOfClasses"
410 //
411 // + extension);
412 //
413 // FileWriter fw = new FileWriter(outFile);
414 // fw.write("<ListCouplingMetricOfClasses>");
415 // Enumeration enump = classCouplingMetric.elements();
416 // while (enump.hasMoreElements()) {
417 // CouplingMetricOfClass cmc =
418 // (CouplingMetricOfClass) enump.nextElement();
419 //
420 // fw.write(cmc.toXml().toString());
421 // fw.flush();
422 //
423 // }
424 // fw.write("</ListCouplingMetricOfClasses>");
425 // fw.close();
426 //
427 // } catch (Exception e) {
428 // e.printStackTrace();
429 // }
430         logger.debug("<- reporting ");
431     */

432     }
433     
434     
435     public Element JavaDoc toDom(Document JavaDoc document){
436
437         Element JavaDoc root=document.createElement("Coupling");
438         Element JavaDoc ret=document.createElement("ListCouplingMetricOfClasses");
439         root.appendChild(ret);
440         
441         
442         ret.setAttribute("size", String.valueOf(this.classCouplingMetric.size()));
443         Enumeration JavaDoc enumc = this.classCouplingMetric.elements();
444         while (enumc.hasMoreElements()) {
445             CouplingMetricOfClass cmc =
446                 (CouplingMetricOfClass) enumc.nextElement();
447             ret.appendChild(cmc.toDom(document));
448         }
449         return ret;
450         
451     }
452     
453 }
454
455
Popular Tags