1 package csdl.jblanket.modifier; 2 3 import csdl.jblanket.JBlanketException; 4 import csdl.jblanket.methodset.MethodSetManager; 5 import csdl.jblanket.util.MethodCategories; 6 7 import java.io.File ; 8 import java.io.IOException ; 9 10 import org.apache.bcel.classfile.ClassParser; 11 import org.apache.bcel.classfile.JavaClass; 12 import org.apache.bcel.classfile.Method; 13 import org.apache.bcel.generic.ConstantPoolGen; 14 import org.apache.bcel.generic.MethodGen; 15 import org.apache.bcel.generic.ObjectType; 16 import org.apache.bcel.generic.Type; 17 18 26 class ClassModifier { 27 28 29 private boolean verbose; 30 31 private String testGrammar; 32 33 34 private boolean excludeOneLineMethods; 35 36 private boolean excludeConstructors; 37 38 private boolean excludeIndividualMethods; 39 40 41 private MethodCounter counter; 42 43 44 private File classFile; 45 46 private JavaClass clazz; 47 48 60 public ClassModifier(boolean verbose, String testGrammar, boolean excludeOneLineMethods, 61 boolean excludeConstructors, boolean excludeIndividualMethods, 62 MethodCounter counter, File classFile) 63 throws JBlanketException { 64 65 this.verbose = verbose; 66 this.testGrammar = testGrammar; 67 68 this.excludeOneLineMethods = excludeOneLineMethods; 69 this.excludeConstructors = excludeConstructors; 70 this.excludeIndividualMethods = excludeIndividualMethods; 71 72 this.counter = counter; 73 74 this.classFile = classFile; 75 76 try { 78 this.clazz = (new ClassParser(this.classFile.getAbsolutePath())).parse(); 79 } 80 catch (IOException e) { 81 throw new JBlanketException("Unable to parse " + this.classFile.getAbsolutePath(), e); 82 } 83 } 84 85 93 public ClassModifier(boolean verbose, MethodCounter counter, File classFile) 94 throws JBlanketException { 95 this(verbose, null, false, false, false, counter, classFile); 96 } 97 98 103 public void modifyMethods() throws JBlanketException { 104 105 Method[] methods = clazz.getMethods(); 106 107 this.counter.findAllMethods(clazz, this.excludeConstructors); 109 110 113 ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool()); 115 for (int j = 0; j < methods.length; j++) { 116 MethodGen method = new MethodGen(methods[j], clazz.getClassName(), pool); 117 118 MethodModifier methodModifier = new MethodModifier(this.verbose, this.testGrammar, 120 this.excludeOneLineMethods, 121 this.excludeIndividualMethods, 122 this.excludeConstructors, method); 123 methods[j] = methodModifier.processMethod(pool, isModified()); 124 } 125 clazz.setConstantPool(pool.getFinalConstantPool()); 126 128 try { 130 clazz.dump(this.classFile.getAbsoluteFile()); 131 } 132 catch (IOException e) { 133 throw new JBlanketException("Unable to save modified file " 134 + this.classFile.getAbsolutePath()); 135 } 136 } 137 138 148 boolean isModified() { 149 150 ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool()); 151 152 if (clazz.isNative()) { 155 return true; 156 } 157 158 Type[] parameters = new Type[]{Type.STRING, Type.STRING, new ObjectType("java.util.ArrayList"), 159 Type.STRING}; 160 String methodSignature = Type.getMethodSignature(Type.VOID, parameters); 161 162 int result = pool.lookupMethodref("csdl.jblanket.modifier.MethodCollector", 163 "storeMethodTypeSignature", methodSignature); 164 return result != -1; 165 } 166 167 170 public void excludeMethods() { 171 172 Method[] methods = clazz.getMethods(); 173 174 ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool()); 176 for (int j = 0; j < methods.length; j++) { 177 MethodGen method = new MethodGen(methods[j], clazz.getClassName(), pool); 178 MethodModifier methodModifier = new MethodModifier(this.verbose, this.testGrammar, 179 this.excludeOneLineMethods, 180 false, this.excludeConstructors, method); 181 methodModifier.excludeMethod(MethodSetManager.getInstance().getMethodSet( 182 MethodCategories.getInstance().getFileName("excludedFile"))); 183 } 184 } 185 } 186 | Popular Tags |