1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.IOException ; 23 24 import edu.umd.cs.findbugs.ba.AnalysisContext; 25 import edu.umd.cs.findbugs.ba.SourceInfoMap; 26 import edu.umd.cs.findbugs.classfile.ClassDescriptor; 27 import edu.umd.cs.findbugs.xml.XMLAttributeList; 28 import edu.umd.cs.findbugs.xml.XMLOutput; 29 30 37 public class ClassAnnotation extends PackageMemberAnnotation { 38 private static final long serialVersionUID = 1L; 39 40 private static final String DEFAULT_ROLE = "CLASS_DEFAULT"; 41 42 47 public ClassAnnotation(String className) { 48 super(className, DEFAULT_ROLE); 49 } 50 51 57 public static ClassAnnotation fromClassDescriptor(ClassDescriptor classDescriptor) { 58 return new ClassAnnotation(classDescriptor.toDottedClassName()); 59 } 60 61 public void accept(BugAnnotationVisitor visitor) { 62 visitor.visitClassAnnotation(this); 63 } 64 65 @Override 66 protected String formatPackageMember(String key, ClassAnnotation primaryClass) { 67 if (key.equals("") || key.equals("hash")) 68 return className; 69 else 70 throw new IllegalArgumentException ("unknown key " + key); 71 } 72 73 @Override 74 public int hashCode() { 75 return className.hashCode(); 76 } 77 78 @Override 79 public boolean equals(Object o) { 80 if (!(o instanceof ClassAnnotation)) 81 return false; 82 ClassAnnotation other = (ClassAnnotation) o; 83 return className.equals(other.className); 84 } 85 86 public boolean contains(ClassAnnotation other) { 87 return other.className.startsWith(className); 88 } 89 public ClassAnnotation getTopLevelClass() { 90 int firstDollar = className.indexOf('$'); 91 if (firstDollar == -1) return this; 92 return new ClassAnnotation(className.substring(0,firstDollar)); 93 94 } 95 public int compareTo(BugAnnotation o) { 96 if (!(o instanceof ClassAnnotation)) return this.getClass().getName().compareTo(o.getClass().getName()); 98 ClassAnnotation other = (ClassAnnotation) o; 99 return className.compareTo(other.className); 100 } 101 102 105 @Override 106 public SourceLineAnnotation getSourceLines() { 107 if (sourceLines == null) { 108 110 AnalysisContext currentAnalysisContext = AnalysisContext.currentAnalysisContext(); 111 if (currentAnalysisContext == null) 112 sourceLines = new SourceLineAnnotation(className, sourceFileName, -1, -1, -1, -1); 113 else { 114 SourceInfoMap.SourceLineRange classLine = currentAnalysisContext 115 .getSourceInfoMap() 116 .getClassLine(className); 117 118 if (classLine == null) 119 sourceLines = new SourceLineAnnotation( 120 className, sourceFileName, -1,-1, -1, -1); 121 else sourceLines = new SourceLineAnnotation( 122 className, sourceFileName, classLine.getStart(), classLine.getEnd(), -1, -1); 123 } 124 } 125 return sourceLines; 126 } 127 128 131 132 private static final String ELEMENT_NAME = "Class"; 133 134 public void writeXML(XMLOutput xmlOutput) throws IOException { 135 writeXML(xmlOutput, false); 136 } 137 138 public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException { 139 XMLAttributeList attributeList = new XMLAttributeList() 140 .addAttribute("classname", getClassName()); 141 String role = getDescription(); 142 if (!role.equals(DEFAULT_ROLE)) 143 attributeList.addAttribute("role", role); 144 145 xmlOutput.openTag(ELEMENT_NAME, attributeList); 146 getSourceLines().writeXML(xmlOutput, addMessages); 147 if (addMessages) { 148 xmlOutput.openTag(BugAnnotation.MESSAGE_TAG); 149 xmlOutput.writeText(this.toString()); 150 xmlOutput.closeTag(BugAnnotation.MESSAGE_TAG); 151 } 152 xmlOutput.closeTag(ELEMENT_NAME); 153 154 } 155 } 156 157 | Popular Tags |