1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.IOException ; 23 24 import org.apache.bcel.classfile.LocalVariable; 25 import org.apache.bcel.classfile.LocalVariableTable; 26 import org.apache.bcel.classfile.Method; 27 import org.apache.bcel.generic.IndexedInstruction; 28 import org.apache.bcel.generic.InstructionHandle; 29 30 import edu.umd.cs.findbugs.ba.Location; 31 import edu.umd.cs.findbugs.xml.XMLAttributeList; 32 import edu.umd.cs.findbugs.xml.XMLOutput; 33 34 40 public class LocalVariableAnnotation implements BugAnnotation { 41 private static final long serialVersionUID = 1L; 42 43 private static final String DEFAULT_ROLE = "LOCAL_VARIABLE_DEFAULT"; 44 45 final private String value; 46 final int register, pc; 47 private String description; 48 49 57 public LocalVariableAnnotation(String name, int register, int pc) { 58 this.value = name; 59 this.register = register; 60 this.pc = pc; 61 this.description = DEFAULT_ROLE; 62 this.setDescription(name.equals("?") ? "LOCAL_VARIABLE_UNKNOWN" : "LOCAL_VARIABLE_NAMED"); 63 } 64 65 public static LocalVariableAnnotation getLocalVariableAnnotation( 66 Method method, Location location, IndexedInstruction ins) { 67 int local = ins.getIndex(); 68 InstructionHandle handle = location.getHandle(); 69 int position1 = handle.getNext().getPosition(); 70 int position2 = handle.getPosition(); 71 return getLocalVariableAnnotation(method, local, position1, position2); 72 } 73 74 public static LocalVariableAnnotation getLocalVariableAnnotation( 75 Method method, int local, int position1, int position2) { 76 77 LocalVariableTable localVariableTable = method.getLocalVariableTable(); 78 String localName = "?"; 79 if (localVariableTable != null) { 80 LocalVariable lv1 = localVariableTable.getLocalVariable(local, position1); 81 if (lv1 == null) { 82 lv1 = localVariableTable.getLocalVariable(local, position2); 83 position1 = position2; 84 } 85 if (lv1 != null) localName = lv1.getName(); 86 } 87 return new LocalVariableAnnotation(localName, local, position1); 88 } 89 90 @Override 91 public Object clone() { 92 try { 93 return super.clone(); 94 } catch (CloneNotSupportedException e) { 95 throw new AssertionError (e); 96 } 97 } 98 99 100 public void accept(BugAnnotationVisitor visitor) { 101 visitor.visitLocalVariableAnnotation(this); 102 } 103 104 public String format(String key, ClassAnnotation primaryClass) { 105 if (key.equals("hash")) { 107 if (register < 0) return "??"; 108 return value; 109 } 110 if (register < 0) return "?"; 111 if (key.equals("register")) return String.valueOf(register); 112 else if (key.equals("pc")) return String.valueOf(pc); 113 else if (key.equals("name")) return value; 114 else if (!value.equals("?")) return value; 115 return "$L"+register; 116 } 117 118 public void setDescription(String description) { 119 this.description = description; 120 } 121 122 public String getDescription() { 123 return description; 124 } 125 126 @Override 127 public int hashCode() { 128 return value.hashCode(); 129 } 130 131 @Override 132 public boolean equals(Object o) { 133 if (!(o instanceof LocalVariableAnnotation)) 134 return false; 135 return value.equals(((LocalVariableAnnotation) o).value); 136 } 137 138 public int compareTo(BugAnnotation o) { 139 if (!(o instanceof LocalVariableAnnotation)) return this.getClass().getName().compareTo(o.getClass().getName()); 141 return value.compareTo(((LocalVariableAnnotation) o).value); 142 } 143 144 @Override 145 public String toString() { 146 String pattern = I18N.instance().getAnnotationDescription(description); 147 FindBugsMessageFormat format = new FindBugsMessageFormat(pattern); 148 return format.format(new BugAnnotation[]{this}, null); 149 } 150 151 154 155 private static final String ELEMENT_NAME = "LocalVariable"; 156 157 public void writeXML(XMLOutput xmlOutput) throws IOException { 158 writeXML(xmlOutput, false); 159 } 160 161 public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException { 162 XMLAttributeList attributeList = new XMLAttributeList() 163 .addAttribute("name", value) 164 .addAttribute("register", String.valueOf(register)) 165 .addAttribute("pc", String.valueOf(pc)); 166 167 String role = getDescription(); 168 if (!role.equals(DEFAULT_ROLE)) 169 attributeList.addAttribute("role", role); 170 171 BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages); 172 } 173 174 177 public String getName() { 178 179 return value; 180 } 181 182 183 public boolean isSignificant() { 184 return !value.equals("?"); 185 } 186 } 187 188 | Popular Tags |