KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MetricsGatherer


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 import com.jeantessier.classreader.*;
40
41 /**
42  * Collects metrics from Classfile instances.
43  *
44  * This class can only approximate SLOC based on information provided
45  * by the compiler.
46  */

47 public class MetricsGatherer extends VisitorBase {
48     private String JavaDoc projectName;
49     private MetricsFactory factory;
50
51     private Collection scope = null;
52     private Collection filter = null;
53     
54     private Metrics currentProject;
55     private Metrics currentGroup;
56     private Metrics currentClass;
57     private Metrics currentMethod;
58
59     private int sloc;
60     private boolean isSynthetic;
61     
62     private HashSet metricsListeners = new HashSet();
63
64     public MetricsGatherer(String JavaDoc projectName, MetricsFactory factory) {
65         this.projectName = projectName;
66         this.factory = factory;
67
68         setCurrentProject(getMetricsFactory().createProjectMetrics(getProjectName()));
69     }
70
71     public String JavaDoc getProjectName() {
72         return projectName;
73     }
74     
75     public MetricsFactory getMetricsFactory() {
76         return factory;
77     }
78
79     public void setScopeIncludes(Collection scope) {
80         this.scope = scope;
81     }
82     
83     public void setFilterIncludes(Collection filter) {
84         this.filter = filter;
85     }
86     
87     private Metrics getCurrentProject() {
88         return currentProject;
89     }
90
91     private void setCurrentProject(Metrics currentProject) {
92         this.currentProject = currentProject;
93     }
94
95     private Metrics getCurrentGroup() {
96         return currentGroup;
97     }
98
99     private void setCurrentGroup(Metrics currentGroup) {
100         this.currentGroup = currentGroup;
101     }
102
103     private Metrics getCurrentClass() {
104         return currentClass;
105     }
106
107     private void setCurrentClass(Metrics currentClass) {
108         this.currentClass = currentClass;
109     }
110
111     private Metrics getCurrentMethod() {
112         return currentMethod;
113     }
114
115     private void setCurrentMethod(Metrics currentMethod) {
116         this.currentMethod = currentMethod;
117     }
118
119     public void visitClassfiles(Collection classfiles) {
120         fireBeginSession(classfiles.size());
121         
122         Iterator i = classfiles.iterator();
123         while (i.hasNext()) {
124             ((Classfile) i.next()).accept(this);
125         }
126         
127         fireEndSession();
128     }
129     
130     // Classfile
131
public void visitClassfile(Classfile classfile) {
132         Logger.getLogger(getClass()).debug("VisitClassfile():");
133         Logger.getLogger(getClass()).debug(" class = \"" + classfile.getClassName() + "\"");
134
135         fireBeginClass(classfile);
136         
137         setCurrentMethod(null);
138         setCurrentClass(getMetricsFactory().createClassMetrics(classfile.getClassName()));
139         setCurrentGroup(getCurrentClass().getParent());
140         setCurrentProject(getCurrentGroup().getParent());
141
142         getMetricsFactory().includeClassMetrics(getCurrentClass());
143
144         getCurrentProject().addToMeasurement(Metrics.PACKAGES, getCurrentGroup().getName());
145         
146         if ((classfile.getAccessFlag() & Classfile.ACC_PUBLIC) != 0) {
147             getCurrentProject().addToMeasurement(Metrics.PUBLIC_CLASSES);
148             getCurrentGroup().addToMeasurement(Metrics.PUBLIC_CLASSES);
149         }
150
151         if ((classfile.getAccessFlag() & Classfile.ACC_FINAL) != 0) {
152             getCurrentProject().addToMeasurement(Metrics.FINAL_CLASSES);
153             getCurrentGroup().addToMeasurement(Metrics.FINAL_CLASSES);
154         }
155
156         if ((classfile.getAccessFlag() & Classfile.ACC_INTERFACE) != 0) {
157             getCurrentProject().addToMeasurement(Metrics.INTERFACES);
158             getCurrentGroup().addToMeasurement(Metrics.INTERFACES);
159         }
160
161         if ((classfile.getAccessFlag() & Classfile.ACC_ABSTRACT) != 0) {
162             getCurrentProject().addToMeasurement(Metrics.ABSTRACT_CLASSES);
163             getCurrentGroup().addToMeasurement(Metrics.ABSTRACT_CLASSES);
164         }
165
166         if (classfile.getSuperclassIndex() != 0) {
167             classfile.getRawSuperclass().accept(this);
168
169             Classfile superclass = classfile.getLoader().getClassfile(classfile.getSuperclassName());
170
171             if (superclass != null) {
172                 getMetricsFactory().createClassMetrics(superclass.getClassName()).addToMeasurement(Metrics.SUBCLASSES);
173             }
174             getCurrentClass().addToMeasurement(Metrics.DEPTH_OF_INHERITANCE, computeDepthOfInheritance(superclass));
175         }
176
177         Iterator i;
178
179         i = classfile.getAllInterfaces().iterator();
180         while (i.hasNext()) {
181             ((Class_info) i.next()).accept(this);
182         }
183
184         i = classfile.getAllFields().iterator();
185         while (i.hasNext()) {
186             ((Visitable) i.next()).accept(this);
187         }
188
189         i = classfile.getAllMethods().iterator();
190         while (i.hasNext()) {
191             ((Visitable) i.next()).accept(this);
192         }
193
194         sloc = 1;
195         isSynthetic = false;
196
197         i = classfile.getAttributes().iterator();
198         while (i.hasNext()) {
199             ((Visitable) i.next()).accept(this);
200         }
201         
202         if (!isSynthetic) {
203             getCurrentClass().addToMeasurement(Metrics.CLASS_SLOC, sloc);
204         }
205
206         fireEndClass(classfile, getCurrentClass());
207     }
208     
209     // ConstantPool entries
210
public void visitClass_info(Class_info entry) {
211         Logger.getLogger(getClass()).debug("VisitClass_info():");
212         Logger.getLogger(getClass()).debug(" name = \"" + entry.getName() + "\"");
213         if (entry.getName().startsWith("[")) {
214             addClassDependencies(processDescriptor(entry.getName()));
215         } else {
216             addClassDependency(entry.getName());
217         }
218     }
219     
220     public void visitFieldRef_info(FieldRef_info entry) {
221         Logger.getLogger(getClass()).debug("VisitFieldRef_info():");
222         Logger.getLogger(getClass()).debug(" class = \"" + entry.getClassName() + "\"");
223         Logger.getLogger(getClass()).debug(" name = \"" + entry.getRawNameAndType().getName() + "\"");
224         Logger.getLogger(getClass()).debug(" type = \"" + entry.getRawNameAndType().getType() + "\"");
225
226         // Dependencies on attributes are accounted as dependencies on their class
227
entry.getRawClass().accept(this);
228         addClassDependencies(processDescriptor(entry.getRawNameAndType().getType()));
229     }
230
231     public void visitMethodRef_info(MethodRef_info entry) {
232         Logger.getLogger(getClass()).debug("VisitMethodRef_info():");
233         Logger.getLogger(getClass()).debug(" class = \"" + entry.getClassName() + "\"");
234         Logger.getLogger(getClass()).debug(" name = \"" + entry.getRawNameAndType().getName() + "\"");
235         Logger.getLogger(getClass()).debug(" type = \"" + entry.getRawNameAndType().getType() + "\"");
236         addMethodDependency(entry.getFullSignature());
237         addClassDependencies(processDescriptor(entry.getRawNameAndType().getType()));
238     }
239
240     public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) {
241         Logger.getLogger(getClass()).debug("VisitInterfaceMethodRef_info():");
242         Logger.getLogger(getClass()).debug(" class = \"" + entry.getClassName() + "\"");
243         Logger.getLogger(getClass()).debug(" name = \"" + entry.getRawNameAndType().getName() + "\"");
244         Logger.getLogger(getClass()).debug(" type = \"" + entry.getRawNameAndType().getType() + "\"");
245         addMethodDependency(entry.getFullSignature());
246         addClassDependencies(processDescriptor(entry.getRawNameAndType().getType()));
247     }
248
249     public void visitField_info(Field_info entry) {
250         getCurrentClass().addToMeasurement(Metrics.ATTRIBUTES);
251
252         Logger.getLogger(getClass()).debug("VisitField_info(" + entry.getFullSignature() + ")");
253         Logger.getLogger(getClass()).debug("Current class: " + getCurrentClass().getName());
254         Logger.getLogger(getClass()).debug("Access flag: " + entry.getAccessFlag());
255         Logger.getLogger(getClass()).debug("Public: " + (entry.getAccessFlag() & Method_info.ACC_PUBLIC));
256         Logger.getLogger(getClass()).debug("Private: " + (entry.getAccessFlag() & Method_info.ACC_PRIVATE));
257         Logger.getLogger(getClass()).debug("Protected: " + (entry.getAccessFlag() & Method_info.ACC_PROTECTED));
258         Logger.getLogger(getClass()).debug("Static: " + (entry.getAccessFlag() & Method_info.ACC_STATIC));
259         
260         if ((entry.getAccessFlag() & Field_info.ACC_PUBLIC) != 0) {
261             getCurrentClass().addToMeasurement(Metrics.PUBLIC_ATTRIBUTES);
262         } else if ((entry.getAccessFlag() & Field_info.ACC_PRIVATE) != 0) {
263             getCurrentClass().addToMeasurement(Metrics.PRIVATE_ATTRIBUTES);
264         } else if ((entry.getAccessFlag() & Field_info.ACC_PROTECTED) != 0) {
265             getCurrentClass().addToMeasurement(Metrics.PROTECTED_ATTRIBUTES);
266         } else {
267             getCurrentClass().addToMeasurement(Metrics.PACKAGE_ATTRIBUTES);
268         }
269
270         if ((entry.getAccessFlag() & Field_info.ACC_STATIC) != 0) {
271             getCurrentClass().addToMeasurement(Metrics.STATIC_ATTRIBUTES);
272         }
273
274         if ((entry.getAccessFlag() & Field_info.ACC_FINAL) != 0) {
275             getCurrentClass().addToMeasurement(Metrics.FINAL_ATTRIBUTES);
276         }
277
278         if ((entry.getAccessFlag() & Field_info.ACC_VOLATILE) != 0) {
279             getCurrentClass().addToMeasurement(Metrics.VOLATILE_ATTRIBUTES);
280         }
281
282         if ((entry.getAccessFlag() & Field_info.ACC_TRANSIENT) != 0) {
283             getCurrentClass().addToMeasurement(Metrics.TRANSIENT_ATTRIBUTES);
284         }
285
286         sloc = 1;
287         isSynthetic = false;
288         
289         super.visitField_info(entry);
290         
291         if (!isSynthetic) {
292             getCurrentClass().addToMeasurement(Metrics.CLASS_SLOC, sloc);
293         }
294
295         addClassDependencies(processDescriptor(entry.getDescriptor()));
296     }
297
298     public void visitMethod_info(Method_info entry) {
299         fireBeginMethod(entry);
300         
301         setCurrentMethod(getMetricsFactory().createMethodMetrics(entry.getFullSignature()));
302         getMetricsFactory().includeMethodMetrics(getCurrentMethod());
303         
304         Logger.getLogger(getClass()).debug("VisitMethod_info(" + entry.getFullSignature() + ")");
305         Logger.getLogger(getClass()).debug("Current class: " + getCurrentClass().getName());
306         Logger.getLogger(getClass()).debug("Access flag: " + entry.getAccessFlag());
307         Logger.getLogger(getClass()).debug("Public: " + (entry.getAccessFlag() & Method_info.ACC_PUBLIC));
308         Logger.getLogger(getClass()).debug("Private: " + (entry.getAccessFlag() & Method_info.ACC_PRIVATE));
309         Logger.getLogger(getClass()).debug("Protected: " + (entry.getAccessFlag() & Method_info.ACC_PROTECTED));
310         Logger.getLogger(getClass()).debug("Static: " + (entry.getAccessFlag() & Method_info.ACC_STATIC));
311
312         sloc = 0;
313         isSynthetic = false;
314         
315         if ((entry.getAccessFlag() & Method_info.ACC_PUBLIC) != 0) {
316             getCurrentClass().addToMeasurement(Metrics.PUBLIC_METHODS);
317         } else if ((entry.getAccessFlag() & Method_info.ACC_PRIVATE) != 0) {
318             getCurrentClass().addToMeasurement(Metrics.PRIVATE_METHODS);
319         } else if ((entry.getAccessFlag() & Method_info.ACC_PROTECTED) != 0) {
320             getCurrentClass().addToMeasurement(Metrics.PROTECTED_METHODS);
321         } else {
322             getCurrentClass().addToMeasurement(Metrics.PACKAGE_METHODS);
323         }
324
325         if ((entry.getAccessFlag() & Method_info.ACC_STATIC) != 0) {
326             getCurrentClass().addToMeasurement(Metrics.STATIC_METHODS);
327         }
328
329         if ((entry.getAccessFlag() & Method_info.ACC_FINAL) != 0) {
330             getCurrentClass().addToMeasurement(Metrics.FINAL_METHODS);
331         }
332
333         if ((entry.getAccessFlag() & Method_info.ACC_SYNCHRONIZED) != 0) {
334             getCurrentClass().addToMeasurement(Metrics.SYNCHRONIZED_METHODS);
335         }
336
337         if ((entry.getAccessFlag() & Method_info.ACC_NATIVE) != 0) {
338             getCurrentClass().addToMeasurement(Metrics.NATIVE_METHODS);
339         }
340
341         if ((entry.getAccessFlag() & Method_info.ACC_ABSTRACT) != 0) {
342             getCurrentClass().addToMeasurement(Metrics.ABSTRACT_METHODS);
343             sloc = 1;
344         }
345
346         getCurrentMethod().addToMeasurement(Metrics.PARAMETERS, SignatureHelper.getParameterCount(entry.getDescriptor()));
347         
348         super.visitMethod_info(entry);
349         
350         if (!isSynthetic) {
351             getCurrentMethod().addToMeasurement(Metrics.SLOC, sloc);
352         }
353
354         addClassDependencies(processDescriptor(entry.getDescriptor()));
355
356         fireEndMethod(entry, getCurrentMethod());
357     }
358
359     //
360
// Attributes
361
//
362

363     public void visitCode_attribute(Code_attribute attribute) {
364         super.visitCode_attribute(attribute);
365
366         Logger.getLogger(getClass()).debug("Walking bytecode ...");
367
368         byte[] code = attribute.getCode();
369
370         /*
371          * We can skip the "new" (0xbb) instruction as it is always
372          * followed by a call to the constructor method.
373          */

374         
375         Iterator ci = attribute.iterator();
376         while (ci.hasNext()) {
377             Instruction instr = (Instruction) ci.next();
378             switch (instr.getOpcode()) {
379                 case 0xb2: // getstatic
380
case 0xb3: // putstatic
381
case 0xb4: // getfield
382
case 0xb5: // putfield
383
case 0xb6: // invokevirtual
384
case 0xb7: // invokespecial
385
case 0xb8: // invokestatic
386
case 0xb9: // invokeinterface
387
// case 0xbb: // new
388
case 0xbd: // anewarray
389
case 0xc0: // checkcast
390
case 0xc1: // instanceof
391
case 0xc5: // multianewarray
392
int start = instr.getStart();
393                     int index = ((code[start+1] & 0xff) << 8) | (code[start+2] & 0xff);
394                     ((Visitable) attribute.getClassfile().getConstantPool().get(index)).accept(this);
395                     break;
396                 default:
397                     // Do nothing
398
break;
399             }
400         }
401     }
402
403     public void visitSynthetic_attribute(Synthetic_attribute attribute) {
404         Object JavaDoc owner = attribute.getOwner();
405
406         isSynthetic = true;
407         
408         if (owner instanceof Classfile) {
409             getCurrentProject().addToMeasurement(Metrics.SYNTHETIC_CLASSES);
410             getCurrentGroup().addToMeasurement(Metrics.SYNTHETIC_CLASSES);
411         } else if (owner instanceof Field_info) {
412             getCurrentClass().addToMeasurement(Metrics.SYNTHETIC_ATTRIBUTES);
413         } else if (owner instanceof Method_info) {
414             getCurrentClass().addToMeasurement(Metrics.SYNTHETIC_METHODS);
415         } else {
416             Logger.getLogger(getClass()).warn("Synthetic attribute on unknown Visitable: " + owner.getClass().getName());
417         }
418     }
419
420     public void visitDeprecated_attribute(Deprecated_attribute attribute) {
421         Object JavaDoc owner = attribute.getOwner();
422     
423         if (owner instanceof Classfile) {
424             getCurrentProject().addToMeasurement(Metrics.DEPRECATED_CLASSES);
425             getCurrentGroup().addToMeasurement(Metrics.DEPRECATED_CLASSES);
426         } else if (owner instanceof Field_info) {
427             getCurrentClass().addToMeasurement(Metrics.DEPRECATED_ATTRIBUTES);
428         } else if (owner instanceof Method_info) {
429             getCurrentClass().addToMeasurement(Metrics.DEPRECATED_METHODS);
430         } else {
431             Logger.getLogger(getClass()).warn("Deprecated attribute on unknown Visitable: " + owner.getClass().getName());
432         }
433     }
434
435     //
436
// Attribute helpers
437
//
438

439     public void visitExceptionHandler(ExceptionHandler helper) {
440         if (helper.getCatchTypeIndex() != 0) {
441             helper.getRawCatchType().accept(this);
442         }
443     }
444
445     public void visitInnerClass(InnerClass helper) {
446         if ((helper.getInnerClassInfoIndex() != helper.getInnerClasses().getClassfile().getClassIndex()) && (helper.getInnerClassInfo().startsWith(helper.getInnerClasses().getClassfile().getClassName()))) {
447             getCurrentProject().addToMeasurement(Metrics.INNER_CLASSES);
448             getCurrentGroup().addToMeasurement(Metrics.INNER_CLASSES);
449             getCurrentClass().addToMeasurement(Metrics.INNER_CLASSES);
450         
451             if ((helper.getAccessFlag() & InnerClass.ACC_PUBLIC) != 0) {
452                 getCurrentProject().addToMeasurement(Metrics.PUBLIC_INNER_CLASSES);
453                 getCurrentGroup().addToMeasurement(Metrics.PUBLIC_INNER_CLASSES);
454                 getCurrentClass().addToMeasurement(Metrics.PUBLIC_INNER_CLASSES);
455             } else if ((helper.getAccessFlag() & InnerClass.ACC_PRIVATE) != 0) {
456                 getCurrentProject().addToMeasurement(Metrics.PRIVATE_INNER_CLASSES);
457                 getCurrentGroup().addToMeasurement(Metrics.PRIVATE_INNER_CLASSES);
458                 getCurrentClass().addToMeasurement(Metrics.PRIVATE_INNER_CLASSES);
459             } else if ((helper.getAccessFlag() & InnerClass.ACC_PROTECTED) != 0) {
460                 getCurrentProject().addToMeasurement(Metrics.PROTECTED_INNER_CLASSES);
461                 getCurrentGroup().addToMeasurement(Metrics.PROTECTED_INNER_CLASSES);
462                 getCurrentClass().addToMeasurement(Metrics.PROTECTED_INNER_CLASSES);
463             } else {
464                 getCurrentProject().addToMeasurement(Metrics.PACKAGE_INNER_CLASSES);
465                 getCurrentGroup().addToMeasurement(Metrics.PACKAGE_INNER_CLASSES);
466                 getCurrentClass().addToMeasurement(Metrics.PACKAGE_INNER_CLASSES);
467             }
468
469             if ((helper.getAccessFlag() & InnerClass.ACC_STATIC) != 0) {
470                 getCurrentProject().addToMeasurement(Metrics.STATIC_INNER_CLASSES);
471                 getCurrentGroup().addToMeasurement(Metrics.STATIC_INNER_CLASSES);
472                 getCurrentClass().addToMeasurement(Metrics.STATIC_INNER_CLASSES);
473             }
474
475             if ((helper.getAccessFlag() & InnerClass.ACC_FINAL) != 0) {
476                 getCurrentProject().addToMeasurement(Metrics.FINAL_INNER_CLASSES);
477                 getCurrentGroup().addToMeasurement(Metrics.FINAL_INNER_CLASSES);
478                 getCurrentClass().addToMeasurement(Metrics.FINAL_INNER_CLASSES);
479             }
480
481             if ((helper.getAccessFlag() & InnerClass.ACC_ABSTRACT) != 0) {
482                 getCurrentProject().addToMeasurement(Metrics.ABSTRACT_INNER_CLASSES);
483                 getCurrentGroup().addToMeasurement(Metrics.ABSTRACT_INNER_CLASSES);
484                 getCurrentClass().addToMeasurement(Metrics.ABSTRACT_INNER_CLASSES);
485             }
486         }
487     }
488
489     public void visitLineNumber(LineNumber helper) {
490         sloc++;
491     }
492
493     public void visitLocalVariable(LocalVariable helper) {
494         getCurrentMethod().addToMeasurement(Metrics.LOCAL_VARIABLES);
495
496         addClassDependencies(processDescriptor(helper.getDescriptor()));
497     }
498
499     private int computeDepthOfInheritance(Classfile classfile) {
500         int result = 1;
501         
502         if (classfile != null && classfile.getSuperclassIndex() != 0) {
503             Classfile superclass = classfile.getLoader().getClassfile(classfile.getSuperclassName());
504             result += computeDepthOfInheritance(superclass);
505         }
506
507         return result;
508     }
509     
510     private Collection processDescriptor(String JavaDoc str) {
511         Collection result = new LinkedList();
512         
513         Logger.getLogger(getClass()).debug("ProcessDescriptor: " + str);
514
515         int currentPos = 0;
516         int startPos;
517         int endPos;
518
519         while ((startPos = str.indexOf('L', currentPos)) != -1) {
520             if ((endPos = str.indexOf(';', startPos)) != -1) {
521                 String JavaDoc classname = SignatureHelper.path2ClassName(str.substring(startPos + 1, endPos));
522                 result.add(classname);
523                 currentPos = endPos + 1;
524             } else {
525                 currentPos = startPos + 1;
526             }
527         }
528
529         Logger.getLogger(getClass()).debug("ProcessDescriptor: " + result);
530         
531         return result;
532     }
533
534     private void addClassDependencies(Collection classnames) {
535         Iterator i = classnames.iterator();
536         while (i.hasNext()) {
537             addClassDependency((String JavaDoc) i.next());
538         }
539     }
540     
541     private void addClassDependency(String JavaDoc name) {
542         Logger.getLogger(getClass()).debug("AddClassDependency(\"" + name + "\") ...");
543
544         if (!getCurrentClass().getName().equals(name) && isInFilter(name)) {
545             Metrics other = getMetricsFactory().createClassMetrics(name);
546                 
547             if (getCurrentMethod() != null && isInScope(getCurrentMethod().getName())) {
548                 Logger.getLogger(getClass()).debug("AddClassDependency " + getCurrentMethod().getName() + " -> " + name + " ...");
549                 
550                 if (getCurrentClass().getParent().equals(other.getParent())) {
551                     Logger.getLogger(getClass()).debug("Intra-Package ...");
552                     getCurrentMethod().addToMeasurement(Metrics.OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES, other.getName());
553                     other.addToMeasurement(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES, getCurrentMethod().getName());
554                 } else {
555                     Logger.getLogger(getClass()).debug("Extra-Package ...");
556                     getCurrentMethod().addToMeasurement(Metrics.OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES, other.getName());
557                     other.addToMeasurement(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES, getCurrentMethod().getName());
558                 }
559             } else if (isInScope(getCurrentClass().getName())) {
560                 Logger.getLogger(getClass()).debug("AddClassDependency " + getCurrentClass().getName() + " -> " + name + " ...");
561                 
562                 if (getCurrentClass().getParent().equals(other.getParent())) {
563                     Logger.getLogger(getClass()).debug("Intra-Package ...");
564                     getCurrentClass().addToMeasurement(Metrics.OUTBOUND_INTRA_PACKAGE_DEPENDENCIES, other.getName());
565                     other.addToMeasurement(Metrics.INBOUND_INTRA_PACKAGE_DEPENDENCIES, getCurrentClass().getName());
566                 } else {
567                     Logger.getLogger(getClass()).debug("Extra-Package ...");
568                     getCurrentClass().addToMeasurement(Metrics.OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES, other.getName());
569                     other.addToMeasurement(Metrics.INBOUND_EXTRA_PACKAGE_DEPENDENCIES, getCurrentClass().getName());
570                 }
571             }
572         }
573     }
574     
575     private void addMethodDependency(String JavaDoc name) {
576         Logger.getLogger(getClass()).debug("AddMethodDependency " + getCurrentMethod().getName() + " -> " + name + " ...");
577
578         if (!getCurrentMethod().getName().equals(name) && isInScope(getCurrentMethod().getName()) && isInFilter(name)) {
579             Metrics other = getMetricsFactory().createMethodMetrics(name);
580             
581             if (getCurrentClass().equals(other.getParent())) {
582                 Logger.getLogger(getClass()).debug("Intra-Class ...");
583                 getCurrentMethod().addToMeasurement(Metrics.OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES, other.getName());
584                 other.addToMeasurement(Metrics.INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES, getCurrentMethod().getName());
585             } else if (getCurrentGroup().equals(other.getParent().getParent())) {
586                 Logger.getLogger(getClass()).debug("Intra-Package ...");
587                 getCurrentMethod().addToMeasurement(Metrics.OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES, other.getName());
588                 other.addToMeasurement(Metrics.INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES, getCurrentMethod().getName());
589             } else {
590                 Logger.getLogger(getClass()).debug("Extra-Package ...");
591                 getCurrentMethod().addToMeasurement(Metrics.OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES, other.getName());
592                 other.addToMeasurement(Metrics.INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES, getCurrentMethod().getName());
593             }
594         }
595     }
596     
597     private boolean isInScope(String JavaDoc name) {
598         boolean result = true;
599
600         if (scope != null) {
601             result = scope.contains(name);
602         }
603
604         return result;
605     }
606     
607     private boolean isInFilter(String JavaDoc name) {
608         boolean result = true;
609
610         if (filter != null) {
611             result = filter.contains(name);
612         }
613
614         return result;
615     }
616
617     public void addMetricsListener(MetricsListener listener) {
618         synchronized(metricsListeners) {
619             metricsListeners.add(listener);
620         }
621     }
622
623     public void removeMetricsListener(MetricsListener listener) {
624         synchronized(metricsListeners) {
625             metricsListeners.remove(listener);
626         }
627     }
628
629     protected void fireBeginSession(int size) {
630         MetricsEvent event = new MetricsEvent(this, size);
631
632         HashSet listeners;
633         synchronized(metricsListeners) {
634             listeners = (HashSet) metricsListeners.clone();
635         }
636
637         Iterator i = listeners.iterator();
638         while(i.hasNext()) {
639             ((MetricsListener) i.next()).beginSession(event);
640         }
641     }
642
643     protected void fireBeginClass(Classfile classfile) {
644         MetricsEvent event = new MetricsEvent(this, classfile);
645
646         HashSet listeners;
647         synchronized(metricsListeners) {
648             listeners = (HashSet) metricsListeners.clone();
649         }
650
651         Iterator i = listeners.iterator();
652         while(i.hasNext()) {
653             ((MetricsListener) i.next()).beginClass(event);
654         }
655     }
656
657     protected void fireBeginMethod(Method_info method) {
658         MetricsEvent event = new MetricsEvent(this, method);
659
660         HashSet listeners;
661         synchronized(metricsListeners) {
662             listeners = (HashSet) metricsListeners.clone();
663         }
664
665         Iterator i = listeners.iterator();
666         while(i.hasNext()) {
667             ((MetricsListener) i.next()).beginMethod(event);
668         }
669     }
670
671     protected void fireEndMethod(Method_info method, Metrics metrics) {
672         MetricsEvent event = new MetricsEvent(this, method, metrics);
673
674         HashSet listeners;
675         synchronized(metricsListeners) {
676             listeners = (HashSet) metricsListeners.clone();
677         }
678
679         Iterator i = listeners.iterator();
680         while(i.hasNext()) {
681             ((MetricsListener) i.next()).endMethod(event);
682         }
683     }
684
685     protected void fireEndClass(Classfile classfile, Metrics metrics) {
686         MetricsEvent event = new MetricsEvent(this, classfile, metrics);
687
688         HashSet listeners;
689         synchronized(metricsListeners) {
690             listeners = (HashSet) metricsListeners.clone();
691         }
692
693         Iterator i = listeners.iterator();
694         while(i.hasNext()) {
695             ((MetricsListener) i.next()).endClass(event);
696         }
697     }
698
699     protected void fireEndSession() {
700         MetricsEvent event = new MetricsEvent(this);
701
702         HashSet listeners;
703         synchronized(metricsListeners) {
704             listeners = (HashSet) metricsListeners.clone();
705         }
706
707         Iterator i = listeners.iterator();
708         while(i.hasNext()) {
709             ((MetricsListener) i.next()).endSession(event);
710         }
711     }
712 }
713
Popular Tags