KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005, 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 java.io.IOException JavaDoc;
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 /**
31  * A BugAnnotation object specifying a Java class involved in the bug.
32  *
33  * @author David Hovemeyer
34  * @see BugAnnotation
35  * @see BugInstance
36  */

37 public class ClassAnnotation extends PackageMemberAnnotation {
38     private static final long serialVersionUID = 1L;
39
40     private static final String JavaDoc DEFAULT_ROLE = "CLASS_DEFAULT";
41
42     /**
43      * Constructor.
44      *
45      * @param className the name of the class
46      */

47     public ClassAnnotation(String JavaDoc className) {
48         super(className, DEFAULT_ROLE);
49     }
50
51     /**
52      * Factory method to create a ClassAnnotation from a ClassDescriptor.
53      *
54      * @param classDescriptor the ClassDescriptor
55      * @return the ClassAnnotation
56      */

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 JavaDoc
66     protected String JavaDoc formatPackageMember(String JavaDoc key, ClassAnnotation primaryClass) {
67         if (key.equals("") || key.equals("hash"))
68             return className;
69         else
70             throw new IllegalArgumentException JavaDoc("unknown key " + key);
71     }
72
73     @Override JavaDoc
74     public int hashCode() {
75         return className.hashCode();
76     }
77
78     @Override JavaDoc
79     public boolean equals(Object JavaDoc 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)) // BugAnnotations must be Comparable with any type of BugAnnotation
97
return this.getClass().getName().compareTo(o.getClass().getName());
98         ClassAnnotation other = (ClassAnnotation) o;
99         return className.compareTo(other.className);
100     }
101     
102     /* (non-Javadoc)
103      * @see edu.umd.cs.findbugs.PackageMemberAnnotation#getSourceLines()
104      */

105     @Override JavaDoc
106     public SourceLineAnnotation getSourceLines() {
107         if (sourceLines == null) {
108             // Create source line annotation for class on demand
109

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     /* ----------------------------------------------------------------------
129      * XML Conversion support
130      * ---------------------------------------------------------------------- */

131
132     private static final String JavaDoc ELEMENT_NAME = "Class";
133
134     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
135         writeXML(xmlOutput, false);
136     }
137
138     public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException JavaDoc {
139         XMLAttributeList attributeList = new XMLAttributeList()
140             .addAttribute("classname", getClassName());
141         String JavaDoc 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 // vim:ts=4
158
Popular Tags