1 6 package de.uka.ipd.coverage.recording; 7 8 9 import java.io.Serializable ; 10 import java.util.Iterator ; 11 import java.util.LinkedList ; 12 import java.util.List ; 13 14 import org.apache.bcel.classfile.JavaClass; 15 import org.apache.bcel.classfile.Method; 16 17 import de.uka.ipd.coverage.natures.CoverageNatureVisitor; 18 import de.uka.ipd.coverage.utils.StringFormatter; 19 20 32 public class RegisteredMethod implements Serializable { 33 34 private BasicBlock[] basicBlocks = null; 35 private List paths = new LinkedList (); private Path currentPath; 37 private Method method; 38 private JavaClass jclass; 39 private BasicBlock currentBlock; 40 41 45 public RegisteredMethod(Method method, JavaClass jclass) { 46 this.method = method; 47 this.jclass = jclass; 48 } 49 50 55 public CoverageState isCovered(Path path) { 56 CoverageState result = CoverageState.NO_COVERAGE; 57 for (Iterator iter = paths.iterator(); iter.hasNext();) { 58 Path cPath = (Path) iter.next(); 59 CoverageState elementResult = cPath.includes(path); 60 if (elementResult.isGreaterThan(result)) { 61 result = elementResult; 62 } 63 } 64 return result; 65 } 66 67 public CoverageState isCoveredIgnoreLoops(Path path) { 68 CoverageState result = CoverageState.NO_COVERAGE; 69 for (Iterator iter = paths.iterator(); iter.hasNext();) { 70 Path cPath = (Path) iter.next(); 71 CoverageState elementResult = cPath.loopIncludes(path); 72 if (elementResult.isGreaterThan(result)) { 73 result = elementResult; 74 } 75 } 76 return result; 77 } 78 79 84 public void triggerEntered(int start, int end) { 85 assert basicBlocks != null : "RegisteredMethod for " + getHashKey() + ": no BasicBlocks registered but entered"; currentBlock = getBasicBlock(start, end); 88 if (currentBlock.referencesSameBytecodeAs(basicBlocks[0])) { 91 currentPath = new Path(); 92 paths.add(0, currentPath); 93 } 94 95 assert currentPath != null : "CurrentPath not initialized!"; 97 currentPath.addEnteredBlock(currentBlock); 99 } 100 101 106 public void triggerExited(int start, int end) { 107 assert currentBlock.referencesSameBytecodeAs(getBasicBlock(start, end)) : 108 "Call triggerEntered before calling triggerExited"; currentPath.addExitedBlock(currentBlock); 110 currentBlock = null; 111 } 112 113 private BasicBlock getBasicBlock(int start, int end) { 114 for (int i = 0; i < basicBlocks.length; i++) { 116 if (basicBlocks[i].getStartLine() == start 117 && basicBlocks[i].getEndLine() == end) { 118 return basicBlocks[i]; 119 } 120 } 121 throw new AssertionError ("Block requested that is not in the method!"); } 123 124 130 public void leavingMethod() { 131 Iterator iter = paths.iterator(); 134 if (iter.hasNext()) { 135 iter.next(); 136 while (iter.hasNext()) { 137 if (currentPath.equals(iter.next())) { 138 paths.remove(0); 139 return; 140 } 141 } 142 } 143 } 144 145 public void acceptVisitor(CoverageNatureVisitor visitor) { 146 visitor.visitCode(this, method.getCode()); 147 } 148 149 public JavaClass getJavaClass() { 150 return jclass; 151 } 152 153 public Method getMethod() { 154 return method; 155 } 156 157 public void setBasicBlocks(BasicBlock[] blocks) { 158 this.basicBlocks = blocks; 159 } 160 161 public boolean equals(Object obj) { 162 boolean result = false; 163 if (obj instanceof RegisteredMethod) { 164 RegisteredMethod compMethod = (RegisteredMethod) obj; 165 if (this.method.equals(compMethod.method) 166 && this.jclass.equals(compMethod.jclass)) { 167 result = true; 168 } 169 } 170 return result; 171 } 172 173 174 public int hashCode() { 175 return getHashKey().hashCode(); 176 } 177 181 public String getHashKey() { 182 return createHashKey(this.jclass, this.method); 183 } 184 185 public static String createHashKey(JavaClass jclass, Method method) { 186 return jclass.getClassName() + ":" + method.toString(); } 188 189 public String toString() { 190 String pathString = ""; for (Iterator iter = paths.iterator(); iter.hasNext();) { 192 Path element = (Path) iter.next(); 193 pathString += "\t" + element.toString() + "\n"; } 195 String result = StringFormatter.format( 196 "Klasse {0}: Methode {1}\n " + "Alle Pfade: \n" + "{2}", new Object [] {this.jclass.getClassName(), 200 this.method.getName(), 201 pathString}); 202 return result; 203 } 204 205 public BasicBlock[] getBasicBlocks() { 206 return basicBlocks; 207 } 208 209 public Path[] getPaths() { 210 return (Path[]) paths.toArray(new Path[paths.size()]); 211 } 212 213 } 214 | Popular Tags |