001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.util.*;
036:
037: import org.apache.log4j.*;
038:
039: public class MetricsGatherer extends VisitorBase {
040: private Collection classes = new LinkedList();
041: private Collection interfaces = new LinkedList();
042: private Collection methods = new LinkedList();
043: private Collection fields = new LinkedList();
044: private Collection syntheticClasses = new LinkedList();
045: private Collection syntheticFields = new LinkedList();
046: private Collection syntheticMethods = new LinkedList();
047: private Collection deprecatedClasses = new LinkedList();
048: private Collection deprecatedFields = new LinkedList();
049: private Collection deprecatedMethods = new LinkedList();
050: private Collection publicClasses = new LinkedList();
051: private Collection publicFields = new LinkedList();
052: private Collection publicMethods = new LinkedList();
053: private Collection publicInnerClasses = new LinkedList();
054: private Collection protectedFields = new LinkedList();
055: private Collection protectedMethods = new LinkedList();
056: private Collection protectedInnerClasses = new LinkedList();
057: private Collection privateFields = new LinkedList();
058: private Collection privateMethods = new LinkedList();
059: private Collection privateInnerClasses = new LinkedList();
060: private Collection packageClasses = new LinkedList();
061: private Collection packageFields = new LinkedList();
062: private Collection packageMethods = new LinkedList();
063: private Collection packageInnerClasses = new LinkedList();
064: private Collection abstractClasses = new LinkedList();
065: private Collection abstractMethods = new LinkedList();
066: private Collection abstractInnerClasses = new LinkedList();
067: private Collection staticFields = new LinkedList();
068: private Collection staticMethods = new LinkedList();
069: private Collection staticInnerClasses = new LinkedList();
070: private Collection finalClasses = new LinkedList();
071: private Collection finalFields = new LinkedList();
072: private Collection finalMethods = new LinkedList();
073: private Collection finalInnerClasses = new LinkedList();
074: private Collection synchronizedMethods = new LinkedList();
075: private Collection nativeMethods = new LinkedList();
076: private Collection volatileFields = new LinkedList();
077: private Collection transientFields = new LinkedList();
078: private Collection customAttributes = new LinkedList();
079: private long[] instructionCounts = new long[256];
080:
081: public Collection getClasses() {
082: return classes;
083: }
084:
085: public Collection getInterfaces() {
086: return interfaces;
087: }
088:
089: public Collection getMethods() {
090: return methods;
091: }
092:
093: public Collection getFields() {
094: return fields;
095: }
096:
097: public Collection getSyntheticClasses() {
098: return syntheticClasses;
099: }
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 visitSynthetic_attribute(Synthetic_attribute attribute) {
337: Object owner = attribute.getOwner();
338:
339: if (owner instanceof Classfile) {
340: syntheticClasses.add(owner);
341: } else if (owner instanceof Field_info) {
342: syntheticFields.add(owner);
343: } else if (owner instanceof Method_info) {
344: syntheticMethods.add(owner);
345: } else {
346: Logger.getLogger(getClass()).warn(
347: "Synthetic attribute on unknown Visitable: "
348: + owner.getClass().getName());
349: }
350: }
351:
352: public void visitDeprecated_attribute(Deprecated_attribute attribute) {
353: Object owner = attribute.getOwner();
354:
355: if (owner instanceof Classfile) {
356: deprecatedClasses.add(owner);
357: } else if (owner instanceof Field_info) {
358: deprecatedFields.add(owner);
359: } else if (owner instanceof Method_info) {
360: deprecatedMethods.add(owner);
361: } else {
362: Logger.getLogger(getClass()).warn(
363: "Deprecated attribute on unknown Visitable: "
364: + owner.getClass().getName());
365: }
366: }
367:
368: public void visitCustom_attribute(Custom_attribute attribute) {
369: customAttributes.add(attribute);
370: }
371:
372: // Attribute helpers
373: public void visitInstruction(Instruction helper) {
374: getInstructionCounts()[helper.getOpcode()]++;
375:
376: super .visitInstruction(helper);
377: }
378:
379: public void visitInnerClass(InnerClass helper) {
380: if ((helper.getAccessFlag() & InnerClass.ACC_PUBLIC) != 0) {
381: publicInnerClasses.add(helper);
382: } else if ((helper.getAccessFlag() & InnerClass.ACC_PRIVATE) != 0) {
383: privateInnerClasses.add(helper);
384: } else if ((helper.getAccessFlag() & InnerClass.ACC_PROTECTED) != 0) {
385: protectedInnerClasses.add(helper);
386: } else {
387: packageInnerClasses.add(helper);
388: }
389:
390: if ((helper.getAccessFlag() & InnerClass.ACC_STATIC) != 0) {
391: staticInnerClasses.add(helper);
392: }
393:
394: if ((helper.getAccessFlag() & InnerClass.ACC_FINAL) != 0) {
395: finalInnerClasses.add(helper);
396: }
397:
398: if ((helper.getAccessFlag() & InnerClass.ACC_INTERFACE) != 0) {
399: interfaces.add(helper);
400: } else {
401: classes.add(helper);
402: }
403:
404: if ((helper.getAccessFlag() & InnerClass.ACC_ABSTRACT) != 0) {
405: abstractInnerClasses.add(helper);
406: }
407: }
408: }
|