KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > modifier > MethodCounter


1 package csdl.jblanket.modifier;
2
3 import csdl.jblanket.JBlanketException;
4 import csdl.jblanket.methodset.MethodInfo;
5 import csdl.jblanket.methodset.MethodSet;
6 import csdl.jblanket.methodset.MethodSetManager;
7 import csdl.jblanket.util.MethodCategories;
8
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Date JavaDoc;
13
14 import org.apache.bcel.classfile.JavaClass;
15 import org.apache.bcel.classfile.Method;
16 import org.apache.bcel.generic.ConstantPoolGen;
17 import org.apache.bcel.generic.LineNumberGen;
18 import org.apache.bcel.generic.MethodGen;
19 import org.apache.bcel.generic.Type;
20
21 /**
22  * Provides a counter for all of the methods in the system. All methods are found and stored in an
23  * XML file specified by 'totalFile' in csdl.jblanket.util.MethodCategories.
24  * <p>
25  * <i>Debug must be turned "on" when compiling to add line numbers.</i> If no line numbers are
26  * detected, then no methods are recorded.
27  *
28  * @author Joy M. Agustin
29  * @version $Id: MethodCounter.java,v 1.3 2005/03/08 08:02:01 timshadel Exp $
30  */

31 public class MethodCounter {
32   
33   /** Container for all of the methods found */
34   private MethodSet methodSet;
35
36   /** Output file for total methods, including abstract and native */
37   private String JavaDoc totalFile;
38   /** Container for byte code methods */
39   private MethodSet totalSet;
40
41   /**
42    * Constructs a new MethodCounter object for 'totalFile'.
43    */

44   public MethodCounter() {
45     
46     this.totalFile = MethodCategories.getInstance().getFileName("totalFile");
47     this.totalSet = MethodSetManager.getInstance().getMethodSet(totalFile);
48   }
49
50   /**
51    * Finds the type signatures of all methods in a Java class. If no line numbers are detected in
52    * a method, then it must be an abstract method.
53    * <p>
54    * NOTE: Unable to distinguish between the user implemented and compiler created constructors.
55    * Therefore, use the excludeConstructors to either exclude or include all constructors.
56    *
57    * @param clazz the class to be recorded.
58    * @param excludeConstructors describes if constructors should be excluded from coverage.
59    */

60   public void findAllMethods(JavaClass clazz, boolean excludeConstructors) {
61
62     // get all methods
63
ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool());
64     Method[] methods = clazz.getMethods();
65     String JavaDoc clazzName = clazz.getClassName();
66
67     // check each method in the class
68
for (int i = 0; i < methods.length; i++) {
69       MethodGen method = new MethodGen(methods[i], clazzName, pool);
70       String JavaDoc methodName = method.getName();
71
72       // get debug line numbers of source code
73
LineNumberGen[] lineNumbers = method.getLineNumbers();
74       // check if line numbers are present
75
/* if (lineNumbers.length == 0) {
76         // found abstract method, do nothing
77         continue;
78       }
79
80       // check for void method containing only a
81       // return statement, perhaps inserted just by javac
82       // treat like abstract methods, since there's nothing to test
83       if (method.getType() == Type.VOID && lineNumbers.length == 1) {
84         continue;
85       }
86 */

87       // these methods are not implemented by the programmer
88
// <clinit> - class or interface initialization method
89
// class$ - constructor for super class
90
if ("<clinit>".equals(methodName) || "class$".equals(methodName)) {
91         continue;
92       }
93       /*
94       // init - user implemented and default compiler created constructors;
95       else if (methodName.equals(constructor) && excludeConstructors) {
96         continue;
97       }
98       */

99
100       // found method
101
Type[] paramType = method.getArgumentTypes();
102       ArrayList JavaDoc paramList = new ArrayList JavaDoc();
103       for (int j = 0; j < paramType.length; j++) {
104         paramList.add(MethodCollector.reconstructType(paramType[j].getSignature()));
105       }
106
107       // reconstruct names of constructors from default names, removing package prefix
108
if ("<init>".equals(methodName)) {
109         methodName = MethodCollector.removePackagePrefix(clazzName);
110       }
111       if ("<clinit>".equals(methodName)) {
112         methodName = MethodCollector.removePackagePrefix(clazzName) + "[static initializer]";
113       }
114       methodName = methodName.replaceAll("<", "_").replaceAll(">", "_");
115       MethodInfo methodInfo = new MethodInfo(clazzName, methodName, paramList);
116       this.totalSet.add(methodInfo);
117     }
118   }
119
120   /**
121    * Stores all methods found to XML file 'totalFile'.
122    *
123    * @throws JBlanketException if cannot store methods.
124    */

125   public void storeAllMethods() throws JBlanketException {
126
127     // store total methods to totalFile
128
try {
129       // throws FileNotFoundException
130
FileOutputStream JavaDoc fostream = new FileOutputStream JavaDoc(this.totalFile);
131       // throws IOException
132
this.totalSet.store(fostream, null, new Date JavaDoc());
133       // throws FileNotFoundException
134
fostream.close();
135     }
136     catch (IOException JavaDoc e) {
137       throw new JBlanketException("Unable to store methods to " + this.totalFile, e);
138     }
139   }
140 }
Popular Tags