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 ; 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Date ; 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 31 public class MethodCounter { 32 33 34 private MethodSet methodSet; 35 36 37 private String totalFile; 38 39 private MethodSet totalSet; 40 41 44 public MethodCounter() { 45 46 this.totalFile = MethodCategories.getInstance().getFileName("totalFile"); 47 this.totalSet = MethodSetManager.getInstance().getMethodSet(totalFile); 48 } 49 50 60 public void findAllMethods(JavaClass clazz, boolean excludeConstructors) { 61 62 ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool()); 64 Method[] methods = clazz.getMethods(); 65 String clazzName = clazz.getClassName(); 66 67 for (int i = 0; i < methods.length; i++) { 69 MethodGen method = new MethodGen(methods[i], clazzName, pool); 70 String methodName = method.getName(); 71 72 LineNumberGen[] lineNumbers = method.getLineNumbers(); 74 87 if ("<clinit>".equals(methodName) || "class$".equals(methodName)) { 91 continue; 92 } 93 99 100 Type[] paramType = method.getArgumentTypes(); 102 ArrayList paramList = new ArrayList (); 103 for (int j = 0; j < paramType.length; j++) { 104 paramList.add(MethodCollector.reconstructType(paramType[j].getSignature())); 105 } 106 107 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 125 public void storeAllMethods() throws JBlanketException { 126 127 try { 129 FileOutputStream fostream = new FileOutputStream (this.totalFile); 131 this.totalSet.store(fostream, null, new Date ()); 133 fostream.close(); 135 } 136 catch (IOException e) { 137 throw new JBlanketException("Unable to store methods to " + this.totalFile, e); 138 } 139 } 140 } | Popular Tags |