1 19 20 package edu.umd.cs.findbugs; 21 22 import edu.umd.cs.findbugs.ba.AnalysisContext; 23 24 32 public abstract class PackageMemberAnnotation implements BugAnnotation { 33 private static final long serialVersionUID = -8208567669352996892L; 34 35 protected String className; 36 protected String sourceFileName; 37 protected String description; 38 protected SourceLineAnnotation sourceLines; 39 40 45 protected PackageMemberAnnotation(String className, String description) { 46 this.className = className; 47 AnalysisContext context = AnalysisContext.currentAnalysisContext(); 48 if (context != null) this.sourceFileName = context.lookupSourceFile(className); 49 else this.sourceFileName = SourceLineAnnotation.UNKNOWN_SOURCE_FILE; 50 this.description = description; 51 } 52 53 @Override 55 public Object clone() { 56 try { 57 return super.clone(); 58 } catch (CloneNotSupportedException e) { 59 throw new AssertionError (e); 60 } 61 } 62 63 66 public final String getSourceFileName() { 67 return sourceFileName; 68 } 69 70 73 public final String getClassName() { 74 return className; 75 } 76 77 80 public final String getPackageName() { 81 int lastDot = className.lastIndexOf('.'); 82 if (lastDot < 0) 83 return ""; 84 else 85 return className.substring(0, lastDot); 86 } 87 88 97 public final String format(String key, ClassAnnotation primaryClass) { 98 if (key.equals("class")) 99 return className; 100 else if (key.equals("package")) 101 return getPackageName(); 102 else 103 return formatPackageMember(key, primaryClass); 104 } 105 106 public void setDescription(String description) { 107 this.description = description; 108 } 109 110 public String getDescription() { 111 return description; 112 } 113 114 119 protected static String shorten(String pkgName, String typeName) { 120 int index = typeName.lastIndexOf('.'); 121 if (index >= 0) { 122 String otherPkg = typeName.substring(0, index); 123 if (otherPkg.equals(pkgName) || otherPkg.equals("java.lang")) 124 typeName = typeName.substring(index + 1); 125 } 126 return typeName; 127 } 128 129 135 protected abstract String formatPackageMember(String key, ClassAnnotation primaryClass); 136 137 142 @Override 143 public String toString() { 144 String pattern = I18N.instance().getAnnotationDescription(description); 145 FindBugsMessageFormat format = new FindBugsMessageFormat(pattern); 146 return format.format(new BugAnnotation[]{this}, null); 147 } 148 149 153 public void setSourceLines(SourceLineAnnotation sourceLines) { 154 this.sourceLines = sourceLines; 155 sourceFileName = sourceLines.getSourceFile(); 156 } 157 158 165 public SourceLineAnnotation getSourceLines() { 166 return sourceLines; 167 } 168 169 170 public boolean isSignificant() { 171 return true; 172 } 173 174 175 } 176 177 | Popular Tags |