1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.IOException ; 23 24 import edu.umd.cs.findbugs.xml.XMLAttributeList; 25 import edu.umd.cs.findbugs.xml.XMLOutput; 26 27 33 public class IntAnnotation implements BugAnnotation { 34 private static final long serialVersionUID = 1L; 35 36 private static final String DEFAULT_ROLE = "INT_DEFAULT"; 37 38 private int value; 39 private String description; 40 41 44 public static final String INT_SYNC_PERCENT = "INT_SYNC_PERCENT"; 45 public static final String INT_OCCURRENCES = "INT_OCCURRENCES"; 46 47 52 public IntAnnotation(int value) { 53 this.value = value; 54 this.description = DEFAULT_ROLE; 55 } 56 57 @Override 59 public Object clone() { 60 try { 61 return super.clone(); 62 } catch (CloneNotSupportedException e) { 63 throw new AssertionError (e); 64 } 65 } 66 67 72 public int getValue() { 73 return value; 74 } 75 76 public void accept(BugAnnotationVisitor visitor) { 77 visitor.visitIntAnnotation(this); 78 } 79 80 public String format(String key, ClassAnnotation primaryClass) { 81 if (!isSignificant()) return ""; 82 return String.valueOf(value); 83 } 84 85 public void setDescription(String description) { 86 this.description = description; 87 } 88 89 public String getDescription() { 90 return description; 91 } 92 93 @Override 94 public int hashCode() { 95 return value; 96 } 97 98 @Override 99 public boolean equals(Object o) { 100 if (!(o instanceof IntAnnotation)) 101 return false; 102 return value == ((IntAnnotation) o).value; 103 } 104 105 public int compareTo(BugAnnotation o) { 106 if (!(o instanceof IntAnnotation)) return this.getClass().getName().compareTo(o.getClass().getName()); 108 return value - ((IntAnnotation) o).value; 109 } 110 111 @Override 112 public String toString() { 113 String pattern = I18N.instance().getAnnotationDescription(description); 114 FindBugsMessageFormat format = new FindBugsMessageFormat(pattern); 115 return format.format(new BugAnnotation[]{this}, null); 116 } 117 118 121 122 private static final String ELEMENT_NAME = "Int"; 123 124 public void writeXML(XMLOutput xmlOutput) throws IOException { 125 writeXML(xmlOutput, false); 126 } 127 128 public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException { 129 XMLAttributeList attributeList = new XMLAttributeList() 130 .addAttribute("value", String.valueOf(value)); 131 132 String role = getDescription(); 133 if (!role.equals(DEFAULT_ROLE)) 134 attributeList.addAttribute("role", role); 135 136 BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages); 137 } 138 139 public boolean isSignificant() { 140 return !description.equals(INT_SYNC_PERCENT) && !description.equals(INT_OCCURRENCES); 141 } 142 } 143 144 | Popular Tags |