1 19 package edu.umd.cs.findbugs; 20 21 import java.io.IOException ; 22 23 import edu.umd.cs.findbugs.ba.SignatureConverter; 24 import edu.umd.cs.findbugs.xml.XMLAttributeList; 25 import edu.umd.cs.findbugs.xml.XMLOutput; 26 27 34 public class TypeAnnotation implements BugAnnotation { 35 private static final long serialVersionUID = 1L; 36 37 private static final String DEFAULT_ROLE = "TYPE_DEFAULT"; 38 39 final private String descriptor; private String roleDescription; 41 42 51 public TypeAnnotation(String typeDescriptor) { 52 this(typeDescriptor, DEFAULT_ROLE); 53 } 54 55 public TypeAnnotation(String typeDescriptor, String roleDescription) { 56 descriptor = typeDescriptor; 57 this.roleDescription = roleDescription; 58 } 59 60 @Override 62 public Object clone() { 63 try { 64 return super.clone(); 65 } catch (CloneNotSupportedException e) { 66 throw new AssertionError (e); 67 } 68 } 69 70 74 public String getTypeDescriptor() { 75 return descriptor; 76 } 77 78 public void accept(BugAnnotationVisitor visitor) { 79 visitor.visitTypeAnnotation(this); 80 } 81 82 83 public String format(String key, ClassAnnotation primaryClass) { 84 return new SignatureConverter(descriptor).parseNext(); 85 } 86 87 public void setDescription(String roleDescription) { 88 this.roleDescription = roleDescription; 89 } 90 91 public String getDescription() { 92 return roleDescription; 93 } 94 95 @Override 96 public int hashCode() { 97 return descriptor.hashCode(); 98 } 99 100 @Override 101 public boolean equals(Object o) { 102 if (!(o instanceof TypeAnnotation)) 103 return false; 104 return descriptor.equals(((TypeAnnotation) o).descriptor); 105 } 106 107 public int compareTo(BugAnnotation o) { 108 if (!(o instanceof TypeAnnotation)) return this.getClass().getName().compareTo(o.getClass().getName()); 110 return descriptor.compareTo(((TypeAnnotation) o).descriptor); 111 } 113 114 @Override 115 public String toString() { 116 String pattern = I18N.instance().getAnnotationDescription(roleDescription); 117 FindBugsMessageFormat format = new FindBugsMessageFormat(pattern); 118 return format.format(new BugAnnotation[]{this}, null); 119 } 120 121 124 125 private static final String ELEMENT_NAME = "Type"; 126 127 public void writeXML(XMLOutput xmlOutput) throws IOException { 128 writeXML(xmlOutput, false); 129 } 130 131 public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException { 132 XMLAttributeList attributeList = new XMLAttributeList() 133 .addAttribute("descriptor", descriptor); 134 135 String role = getDescription(); 136 if (!role.equals(DEFAULT_ROLE)) 137 attributeList.addAttribute("role", role); 138 139 BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages); 140 } 141 142 143 public boolean isSignificant() { 144 return true; 145 } 146 } 147 | Popular Tags |