KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > PackageMemberAnnotation


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import edu.umd.cs.findbugs.ba.AnalysisContext;
23
24 /**
25  * Abstract base class for BugAnnotations describing constructs
26  * which are contained in a Java package. Specifically,
27  * this includes classes, methods, and fields.
28  *
29  * @author David Hovemeyer
30  * @see BugAnnotation
31  */

32 public abstract class PackageMemberAnnotation implements BugAnnotation {
33     private static final long serialVersionUID = -8208567669352996892L;
34     
35     protected String JavaDoc className;
36     protected String JavaDoc sourceFileName;
37     protected String JavaDoc description;
38     protected SourceLineAnnotation sourceLines;
39      
40     /**
41      * Constructor.
42      *
43      * @param className name of the class
44      */

45     protected PackageMemberAnnotation(String JavaDoc className, String JavaDoc 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
54
@Override JavaDoc
55     public Object JavaDoc clone() {
56         try {
57             return super.clone();
58         } catch (CloneNotSupportedException JavaDoc e) {
59             throw new AssertionError JavaDoc(e);
60         }
61     }
62
63     /**
64      * Get the source file name.
65      */

66     public final String JavaDoc getSourceFileName() {
67         return sourceFileName;
68     }
69
70     /**
71      * Get the class name.
72      */

73     public final String JavaDoc getClassName() {
74         return className;
75     }
76
77     /**
78      * Get the package name.
79      */

80     public final String JavaDoc getPackageName() {
81         int lastDot = className.lastIndexOf('.');
82         if (lastDot < 0)
83             return "";
84         else
85             return className.substring(0, lastDot);
86     }
87
88     /**
89      * Format the annotation.
90      * Note that this version (defined by PackageMemberAnnotation)
91      * only handles the "class" and "package" keys, and calls
92      * formatPackageMember() for all other keys.
93      *
94      * @param key the key
95      * @return the formatted annotation
96      */

97     public final String JavaDoc format(String JavaDoc 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 JavaDoc description) {
107         this.description = description;
108     }
109
110     public String JavaDoc getDescription() {
111         return description;
112     }
113
114     /**
115      * Shorten a type name of remove extraneous components.
116      * Candidates for shortening are classes in same package as this annotation and
117      * classes in the <code>java.lang</code> package.
118      */

119     protected static String JavaDoc shorten(String JavaDoc pkgName, String JavaDoc typeName) {
120         int index = typeName.lastIndexOf('.');
121         if (index >= 0) {
122             String JavaDoc 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     /**
130      * Do default and subclass-specific formatting.
131      *
132      * @param key the key specifying how to do the formatting
133      * @param primaryClass TODO
134      */

135     protected abstract String JavaDoc formatPackageMember(String JavaDoc key, ClassAnnotation primaryClass);
136
137     /**
138      * All PackageMemberAnnotation object share a common toString() implementation.
139      * It uses the annotation description as a pattern for FindBugsMessageFormat,
140      * passing a reference to this object as the single message parameter.
141      */

142     @Override JavaDoc
143     public String JavaDoc toString() {
144         String JavaDoc pattern = I18N.instance().getAnnotationDescription(description);
145         FindBugsMessageFormat format = new FindBugsMessageFormat(pattern);
146         return format.format(new BugAnnotation[]{this}, null);
147     }
148     
149     /**
150      * Set a SourceLineAnnotation describing the source lines
151      * where the package element is defined.
152      */

153     public void setSourceLines(SourceLineAnnotation sourceLines) {
154         this.sourceLines = sourceLines;
155         sourceFileName = sourceLines.getSourceFile();
156     }
157
158     /**
159      * Get the SourceLineAnnotation describing the source lines
160      * where the method is defined.
161      *
162      * @return the SourceLineAnnotation, or null if there is no source information
163      * for this package element
164      */

165     public SourceLineAnnotation getSourceLines() {
166         return sourceLines;
167     }
168     
169
170     public boolean isSignificant() {
171         return true;
172     }
173
174     
175 }
176
177 // vim:ts=4
178
Popular Tags