001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.metrics;
010:
011: import org.acm.seguin.summary.*;
012: import org.acm.seguin.util.TextFormatter;
013: import java.util.Iterator;
014:
015: /**
016: * Gathers metrics data
017: *
018: *@author Chris Seguin
019: *@created July 1, 1999
020: */
021: public class GatherData implements SummaryVisitor {
022: // Instance Variables
023: private MetricsReport metricsReport;
024:
025: /**
026: * Constructor for the StatementReportVisitor object
027: *
028: *@param init Description of Parameter
029: */
030: public GatherData(MetricsReport init) {
031: metricsReport = init;
032: }
033:
034: /**
035: * Visit everything in all packages
036: *
037: *@param data a data value
038: *@return Description of the Returned Value
039: */
040: public Object visit(Object data) {
041: ProjectMetrics projectData = new ProjectMetrics();
042:
043: Iterator iter = PackageSummary.getAllPackages();
044: if (iter != null) {
045: while (iter.hasNext()) {
046: PackageSummary next = (PackageSummary) iter.next();
047: next.accept(this , projectData);
048: }
049: }
050:
051: return projectData;
052: }
053:
054: /**
055: * Visit a summary node. This is the default method.
056: *
057: *@param node the summary that we are visiting
058: *@param data the data that was passed in
059: *@return the result
060: */
061: public Object visit(Summary node, Object data) {
062: // Shouldn't have to do anything about one of these nodes
063: return data;
064: }
065:
066: /**
067: * Visit a package summary.
068: *
069: *@param node the summary that we are visiting
070: *@param data the data that was passed in
071: *@return the result
072: */
073: public Object visit(PackageSummary node, Object data) {
074: PackageMetrics packageData = new PackageMetrics(node.getName());
075:
076: Iterator iter = node.getFileSummaries();
077: if (iter != null) {
078: while (iter.hasNext()) {
079: FileSummary next = (FileSummary) iter.next();
080: next.accept(this , packageData);
081: }
082: }
083:
084: // Add to total
085: ProjectMetrics projectData = (ProjectMetrics) data;
086: projectData.add(packageData);
087:
088: // Return the metrics gathered at this level
089: return packageData;
090: }
091:
092: /**
093: * Visit a file summary.
094: *
095: *@param node the summary that we are visiting
096: *@param data the data that was passed in
097: *@return the result
098: */
099: public Object visit(FileSummary node, Object data) {
100: if (node.getFile() == null) {
101: return data;
102: }
103:
104: // Over the types
105: Iterator iter = node.getTypes();
106:
107: if (iter != null) {
108: while (iter.hasNext()) {
109: TypeSummary next = (TypeSummary) iter.next();
110: next.accept(this , data);
111: }
112: }
113:
114: // Return some value
115: return data;
116: }
117:
118: /**
119: * Visit a import summary.
120: *
121: *@param node the summary that we are visiting
122: *@param data the data that was passed in
123: *@return the result
124: */
125: public Object visit(ImportSummary node, Object data) {
126: // No children so just return
127: return data;
128: }
129:
130: /**
131: * Visit a type summary.
132: *
133: *@param node the summary that we are visiting
134: *@param data the data that was passed in
135: *@return the result
136: */
137: public Object visit(TypeSummary node, Object data) {
138: // Local Variables
139: PackageMetrics packageData = (PackageMetrics) data;
140: TypeMetrics typeData = new TypeMetrics(packageData
141: .getPackageName(), node.getName());
142:
143: // Over the fields
144: Iterator iter = node.getFields();
145: if (iter != null) {
146: while (iter.hasNext()) {
147: FieldSummary next = (FieldSummary) iter.next();
148: if (next.isStatic()) {
149: typeData.incrClassVariableCount();
150: } else {
151: typeData.incrInstanceVariableCount();
152: }
153: }
154: }
155:
156: // Over the methods
157: iter = node.getMethods();
158: if (iter != null) {
159: while (iter.hasNext()) {
160: MethodSummary next = (MethodSummary) iter.next();
161: next.accept(this , typeData);
162:
163: if (next.isStatic()) {
164: typeData.incrClassMethodCount();
165: } else if (next.isPublic()) {
166: typeData.incrPublicMethodCount();
167: } else {
168: typeData.incrOtherMethodCount();
169: }
170: }
171: }
172:
173: // Over the types
174: iter = node.getTypes();
175: if (iter != null) {
176: while (iter.hasNext()) {
177: TypeSummary next = (TypeSummary) iter.next();
178: next.accept(this , data);
179: }
180: }
181:
182: // Print the results
183: if (metricsReport != null) {
184: metricsReport.typeReport(typeData);
185: }
186:
187: // Update the totals
188: packageData.add(typeData);
189:
190: if (node.isAbstract()) {
191: packageData.incrAbstractClassCount();
192: }
193:
194: if (node.isInterface()) {
195: packageData.incrInterfaceCount();
196: }
197:
198: // Return the metrics gathered at this level
199: return typeData;
200: }
201:
202: /**
203: * Visit a method summary.
204: *
205: *@param node the summary that we are visiting
206: *@param data the data that was passed in
207: *@return the result
208: */
209: public Object visit(MethodSummary node, Object data) {
210: // Local Variables
211: TypeMetrics typeData = (TypeMetrics) data;
212:
213: // Gather metrics
214: int count = node.getStatementCount();
215: int params = node.getParameterCount();
216:
217: // Create method metrics object
218: MethodMetrics methodMetrics = new MethodMetrics(typeData
219: .getPackageName(), typeData.getTypeName(), node
220: .getName());
221: methodMetrics.setStatementCount(count);
222: methodMetrics.setParameterCount(params);
223: methodMetrics.setLinesOfCode(node.getEndLine()
224: - node.getStartLine());
225: methodMetrics.setBlockDepth(node.getMaxBlockDepth());
226:
227: // Report the metrics
228: if (metricsReport != null) {
229: metricsReport.methodReport(methodMetrics);
230: }
231:
232: // Type data
233: typeData.add(methodMetrics);
234:
235: // Return the metrics collected
236: return methodMetrics;
237: }
238:
239: /**
240: * Visit a field summary.
241: *
242: *@param node the summary that we are visiting
243: *@param data the data that was passed in
244: *@return the result
245: */
246: public Object visit(FieldSummary node, Object data) {
247: return data;
248: }
249:
250: /**
251: * Visit a parameter summary.
252: *
253: *@param node the summary that we are visiting
254: *@param data the data that was passed in
255: *@return the result
256: */
257: public Object visit(ParameterSummary node, Object data) {
258: return data;
259: }
260:
261: /**
262: * Visit a local variable summary.
263: *
264: *@param node the summary that we are visiting
265: *@param data the data that was passed in
266: *@return the result
267: */
268: public Object visit(LocalVariableSummary node, Object data) {
269: return data;
270: }
271:
272: /**
273: * Visit a variable summary.
274: *
275: *@param node the summary that we are visiting
276: *@param data the data that was passed in
277: *@return the result
278: */
279: public Object visit(VariableSummary node, Object data) {
280: return data;
281: }
282:
283: /**
284: * Visit a type declaration summary.
285: *
286: *@param node the summary that we are visiting
287: *@param data the data that was passed in
288: *@return the result
289: */
290: public Object visit(TypeDeclSummary node, Object data) {
291: return data;
292: }
293:
294: /**
295: * Visit a message send summary.
296: *
297: *@param node the summary that we are visiting
298: *@param data the data that was passed in
299: *@return the result
300: */
301: public Object visit(MessageSendSummary node, Object data) {
302: return data;
303: }
304:
305: /**
306: * Visit a field access summary.
307: *
308: *@param node the summary that we are visiting
309: *@param data the data that was passed in
310: *@return the result
311: */
312: public Object visit(FieldAccessSummary node, Object data) {
313: return data;
314: }
315:
316: /**
317: * Main program
318: *
319: *@param args the command line arguments
320: */
321: public static void main(String[] args) {
322: if (args.length == 0) {
323: (new SummaryTraversal(System.getProperty("user.dir")))
324: .run();
325: } else {
326: (new SummaryTraversal(args[0])).run();
327: }
328:
329: // Now print everything
330: MetricsReport metricsReport = new TextReport();
331: GatherData visitor = new GatherData(metricsReport);
332: ProjectMetrics projectData = (ProjectMetrics) visitor.visit("");
333:
334: metricsReport.finalReport(projectData);
335: }
336: }
|