| 1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import org.apache.bcel.generic.InstructionHandle; 23 24 import edu.umd.cs.findbugs.annotations.NonNull; 25 26 41 public class Location implements Comparable <Location> { 42 private final InstructionHandle handle; 43 private final BasicBlock basicBlock; 44 45 51 public Location(@NonNull InstructionHandle handle, @NonNull BasicBlock basicBlock) { 52 if (handle == null) throw new NullPointerException ("handle cannot be null"); 53 if (basicBlock == null) throw new NullPointerException ("basicBlock cannot be null"); 54 this.handle = handle; 55 this.basicBlock = basicBlock; 56 } 57 58 public static Location getFirstLocation(@NonNull BasicBlock basicBlock) { 59 InstructionHandle location = basicBlock.getFirstInstruction(); 60 if (location == null) 61 return null; 62 return new Location(location, basicBlock); 63 } 64 public static Location getLastLocation(@NonNull BasicBlock basicBlock) { 65 InstructionHandle lastInstruction = basicBlock.getLastInstruction(); 66 72 if (lastInstruction == null) 73 return null; 74 return new Location(lastInstruction, basicBlock); 75 } 76 79 public InstructionHandle getHandle() { 80 return handle; 81 } 82 83 86 public BasicBlock getBasicBlock() { 87 return basicBlock; 88 } 89 90 94 public boolean isFirstInstructionInBasicBlock() { 95 return !basicBlock.isEmpty() && handle == basicBlock.getFirstInstruction(); 96 } 97 98 102 public boolean isLastInstructionInBasicBlock() { 103 return !basicBlock.isEmpty() && handle == basicBlock.getLastInstruction(); 104 } 105 106 public int compareTo(Location other) { 107 int cmp = basicBlock.getId() - other.basicBlock.getId(); 108 if (false && cmp != 0) 109 return cmp; 110 111 int pos = handle.getPosition() - other.handle.getPosition(); 112 if (pos != 0) return pos; 113 return cmp; 114 } 115 116 @Override  117 public int hashCode() { 118 return System.identityHashCode(basicBlock) + handle.getPosition(); 119 } 120 121 @Override  122 public boolean equals(Object o) { 123 if (!(o instanceof Location)) 124 return false; 125 Location other = (Location) o; 126 return basicBlock == other.basicBlock && handle == other.handle; 127 } 128 129 @Override  130 public String toString() { 131 return handle.toString() + " in basic block " + basicBlock.getId(); 132 } 133 } 134 135 | Popular Tags |