KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > 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.classreader;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 public class MetricsGatherer extends VisitorBase {
40     private Collection classes = new LinkedList();
41     private Collection interfaces = new LinkedList();
42     private Collection methods = new LinkedList();
43     private Collection fields = new LinkedList();
44     private Collection syntheticClasses = new LinkedList();
45     private Collection syntheticFields = new LinkedList();
46     private Collection syntheticMethods = new LinkedList();
47     private Collection deprecatedClasses = new LinkedList();
48     private Collection deprecatedFields = new LinkedList();
49     private Collection deprecatedMethods = new LinkedList();
50     private Collection publicClasses = new LinkedList();
51     private Collection publicFields = new LinkedList();
52     private Collection publicMethods = new LinkedList();
53     private Collection publicInnerClasses = new LinkedList();
54     private Collection protectedFields = new LinkedList();
55     private Collection protectedMethods = new LinkedList();
56     private Collection protectedInnerClasses = new LinkedList();
57     private Collection privateFields = new LinkedList();
58     private Collection privateMethods = new LinkedList();
59     private Collection privateInnerClasses = new LinkedList();
60     private Collection packageClasses = new LinkedList();
61     private Collection packageFields = new LinkedList();
62     private Collection packageMethods = new LinkedList();
63     private Collection packageInnerClasses = new LinkedList();
64     private Collection abstractClasses = new LinkedList();
65     private Collection abstractMethods = new LinkedList();
66     private Collection abstractInnerClasses = new LinkedList();
67     private Collection staticFields = new LinkedList();
68     private Collection staticMethods = new LinkedList();
69     private Collection staticInnerClasses = new LinkedList();
70     private Collection finalClasses = new LinkedList();
71     private Collection finalFields = new LinkedList();
72     private Collection finalMethods = new LinkedList();
73     private Collection finalInnerClasses = new LinkedList();
74     private Collection synchronizedMethods = new LinkedList();
75     private Collection nativeMethods = new LinkedList();
76     private Collection volatileFields = new LinkedList();
77     private Collection transientFields = new LinkedList();
78     private Collection customAttributes = new LinkedList();
79     private long[] instructionCounts = new long[256];
80     
81     public Collection getClasses() {
82         return classes;
83     }
84
85     public Collection getInterfaces() {
86         return interfaces;
87     }
88
89     public Collection getMethods() {
90         return methods;
91     }
92
93     public Collection getFields() {
94         return fields;
95     }
96
97     public Collection getSyntheticClasses() {
98         return syntheticClasses;
99     }
100
101     public Collection getSyntheticFields() {
102         return syntheticFields;
103     }
104
105     public Collection getSyntheticMethods() {
106         return syntheticMethods;
107     }
108
109     public Collection getDeprecatedClasses() {
110         return deprecatedClasses;
111     }
112
113     public Collection getDeprecatedFields() {
114         return deprecatedFields;
115     }
116
117     public Collection getDeprecatedMethods() {
118         return deprecatedMethods;
119     }
120
121     public Collection getPublicClasses() {
122         return publicClasses;
123     }
124
125     public Collection getPublicFields() {
126         return publicFields;
127     }
128
129     public Collection getPublicMethods() {
130         return publicMethods;
131     }
132
133     public Collection getPublicInnerClasses() {
134         return publicInnerClasses;
135     }
136
137     public Collection getProtectedFields() {
138         return protectedFields;
139     }
140
141     public Collection getProtectedMethods() {
142         return protectedMethods;
143     }
144
145     public Collection getProtectedInnerClasses() {
146         return protectedInnerClasses;
147     }
148
149     public Collection getPrivateFields() {
150         return privateFields;
151     }
152
153     public Collection getPrivateMethods() {
154         return privateMethods;
155     }
156
157     public Collection getPrivateInnerClasses() {
158         return privateInnerClasses;
159     }
160
161     public Collection getPackageClasses() {
162         return packageClasses;
163     }
164
165     public Collection getPackageFields() {
166         return packageFields;
167     }
168
169     public Collection getPackageMethods() {
170         return packageMethods;
171     }
172
173     public Collection getPackageInnerClasses() {
174         return packageInnerClasses;
175     }
176
177     public Collection getAbstractClasses() {
178         return abstractClasses;
179     }
180
181     public Collection getAbstractMethods() {
182         return abstractMethods;
183     }
184
185     public Collection getAbstractInnerClasses() {
186         return abstractInnerClasses;
187     }
188
189     public Collection getStaticFields() {
190         return staticFields;
191     }
192
193     public Collection getStaticMethods() {
194         return staticMethods;
195     }
196
197     public Collection getStaticInnerClasses() {
198         return staticInnerClasses;
199     }
200
201     public Collection getFinalClasses() {
202         return finalClasses;
203     }
204
205     public Collection getFinalFields() {
206         return finalFields;
207     }
208
209     public Collection getFinalMethods() {
210         return finalMethods;
211     }
212
213     public Collection getFinalInnerClasses() {
214         return finalInnerClasses;
215     }
216
217     public Collection getSynchronizedMethods() {
218         return synchronizedMethods;
219     }
220
221     public Collection getNativeMethods() {
222         return nativeMethods;
223     }
224
225     public Collection getVolatileFields() {
226         return volatileFields;
227     }
228
229     public Collection getTransientFields() {
230         return transientFields;
231     }
232
233     public Collection getCustomAttributes() {
234         return customAttributes;
235     }
236
237     public long[] getInstructionCounts() {
238         return instructionCounts;
239     }
240     
241     // Classfile
242
public void visitClassfile(Classfile classfile) {
243         if ((classfile.getAccessFlag() & Classfile.ACC_PUBLIC) != 0) {
244             publicClasses.add(classfile);
245         } else {
246             packageClasses.add(classfile);
247         }
248
249         if ((classfile.getAccessFlag() & Classfile.ACC_FINAL) != 0) {
250             finalClasses.add(classfile);
251         }
252
253         if ((classfile.getAccessFlag() & Classfile.ACC_INTERFACE) != 0) {
254             interfaces.add(classfile);
255         } else {
256             classes.add(classfile);
257         }
258
259         if ((classfile.getAccessFlag() & Classfile.ACC_ABSTRACT) != 0) {
260             abstractClasses.add(classfile);
261         }
262
263         super.visitClassfile(classfile);
264     }
265
266     // Features
267
public void visitField_info(Field_info entry) {
268         fields.add(entry);
269
270         if ((entry.getAccessFlag() & Field_info.ACC_PUBLIC) != 0) {
271             publicFields.add(entry);
272         } else if ((entry.getAccessFlag() & Field_info.ACC_PRIVATE) != 0) {
273             privateFields.add(entry);
274         } else if ((entry.getAccessFlag() & Field_info.ACC_PROTECTED) != 0) {
275             protectedFields.add(entry);
276         } else {
277             packageFields.add(entry);
278         }
279
280         if ((entry.getAccessFlag() & Field_info.ACC_STATIC) != 0) {
281             staticFields.add(entry);
282         }
283
284         if ((entry.getAccessFlag() & Field_info.ACC_FINAL) != 0) {
285             finalFields.add(entry);
286         }
287
288         if ((entry.getAccessFlag() & Field_info.ACC_VOLATILE) != 0) {
289             volatileFields.add(entry);
290         }
291
292         if ((entry.getAccessFlag() & Field_info.ACC_TRANSIENT) != 0) {
293             transientFields.add(entry);
294         }
295
296         super.visitField_info(entry);
297     }
298
299     public void visitMethod_info(Method_info entry) {
300         methods.add(entry);
301
302         if ((entry.getAccessFlag() & Method_info.ACC_PUBLIC) != 0) {
303             publicMethods.add(entry);
304         } else if ((entry.getAccessFlag() & Method_info.ACC_PRIVATE) != 0) {
305             privateMethods.add(entry);
306         } else if ((entry.getAccessFlag() & Method_info.ACC_PROTECTED) != 0) {
307             protectedMethods.add(entry);
308         } else {
309             packageMethods.add(entry);
310         }
311
312         if ((entry.getAccessFlag() & Method_info.ACC_STATIC) != 0) {
313             staticMethods.add(entry);
314         }
315
316         if ((entry.getAccessFlag() & Method_info.ACC_FINAL) != 0) {
317             finalMethods.add(entry);
318         }
319
320         if ((entry.getAccessFlag() & Method_info.ACC_SYNCHRONIZED) != 0) {
321             synchronizedMethods.add(entry);
322         }
323
324         if ((entry.getAccessFlag() & Method_info.ACC_NATIVE) != 0) {
325             nativeMethods.add(entry);
326         }
327
328         if ((entry.getAccessFlag() & Method_info.ACC_ABSTRACT) != 0) {
329             abstractMethods.add(entry);
330         }
331
332         super.visitMethod_info(entry);
333     }
334
335     // Attributes
336
public void visitCode_attribute(Code_attribute attribute) {
337         super.visitCode_attribute(attribute);
338
339         Iterator i = attribute.iterator();
340         while (i.hasNext()) {
341             Instruction instr = (Instruction) i.next();
342             
343             getInstructionCounts()[instr.getOpcode()]++;
344         }
345     }
346
347     public void visitSynthetic_attribute(Synthetic_attribute attribute) {
348         Object JavaDoc owner = attribute.getOwner();
349     
350         if (owner instanceof Classfile) {
351             syntheticClasses.add(owner);
352         } else if (owner instanceof Field_info) {
353             syntheticFields.add(owner);
354         } else if (owner instanceof Method_info) {
355             syntheticMethods.add(owner);
356         } else {
357             Logger.getLogger(getClass()).warn("Synthetic attribute on unknown Visitable: " + owner.getClass().getName());
358         }
359     }
360
361     public void visitDeprecated_attribute(Deprecated_attribute attribute) {
362         Object JavaDoc owner = attribute.getOwner();
363     
364         if (owner instanceof Classfile) {
365             deprecatedClasses.add(owner);
366         } else if (owner instanceof Field_info) {
367             deprecatedFields.add(owner);
368         } else if (owner instanceof Method_info) {
369             deprecatedMethods.add(owner);
370         } else {
371             Logger.getLogger(getClass()).warn("Deprecated attribute on unknown Visitable: " + owner.getClass().getName());
372         }
373     }
374
375     public void visitCustom_attribute(Custom_attribute attribute) {
376         customAttributes.add(attribute);
377     }
378
379     // Attribute helpers
380
public void visitInnerClass(InnerClass helper) {
381         if ((helper.getAccessFlag() & InnerClass.ACC_PUBLIC) != 0) {
382             publicInnerClasses.add(helper);
383         } else if ((helper.getAccessFlag() & InnerClass.ACC_PRIVATE) != 0) {
384             privateInnerClasses.add(helper);
385         } else if ((helper.getAccessFlag() & InnerClass.ACC_PROTECTED) != 0) {
386             protectedInnerClasses.add(helper);
387         } else {
388             packageInnerClasses.add(helper);
389         }
390
391         if ((helper.getAccessFlag() & InnerClass.ACC_STATIC) != 0) {
392             staticInnerClasses.add(helper);
393         }
394
395         if ((helper.getAccessFlag() & InnerClass.ACC_FINAL) != 0) {
396             finalInnerClasses.add(helper);
397         }
398
399         if ((helper.getAccessFlag() & InnerClass.ACC_INTERFACE) != 0) {
400             interfaces.add(helper);
401         } else {
402             classes.add(helper);
403         }
404
405         if ((helper.getAccessFlag() & InnerClass.ACC_ABSTRACT) != 0) {
406             abstractInnerClasses.add(helper);
407         }
408     }
409 }
410
Popular Tags