KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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 package edu.umd.cs.findbugs;
20
21 import java.io.IOException JavaDoc;
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 /**
28  * Bug annotation class for java types.
29  * This is of lighter weight than ClassAnnotation,
30  * and can be used for things like array types.
31  *
32  * @see ClassAnnotation
33  */

34 public class TypeAnnotation implements BugAnnotation {
35     private static final long serialVersionUID = 1L;
36
37     private static final String JavaDoc DEFAULT_ROLE = "TYPE_DEFAULT";
38
39     final private String JavaDoc descriptor; // jvm type descriptor, such as "[I"
40
private String JavaDoc roleDescription;
41     
42     /**
43      * constructor.
44      *
45      * <p>For information on type descriptors,
46      * <br>see http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#14152
47      * <br>or http://www.murrayc.com/learning/java/java_classfileformat.shtml#TypeDescriptors
48      *
49      * @param typeDescriptor a jvm type descriptor, such as "[I"
50      */

51     public TypeAnnotation(String JavaDoc typeDescriptor) {
52         this(typeDescriptor, DEFAULT_ROLE);
53     }
54     
55     public TypeAnnotation(String JavaDoc typeDescriptor, String JavaDoc roleDescription) {
56         descriptor = typeDescriptor;
57         this.roleDescription = roleDescription;
58     }
59     
60     //@Override
61
@Override JavaDoc
62     public Object JavaDoc clone() {
63         try {
64             return super.clone();
65         } catch (CloneNotSupportedException JavaDoc e) {
66             throw new AssertionError JavaDoc(e);
67         }
68     }
69
70     /**
71      * Get the type descriptor.
72      * @return the jvm type descriptor, such as "[I"
73      */

74     public String JavaDoc getTypeDescriptor() {
75         return descriptor;
76     }
77
78     public void accept(BugAnnotationVisitor visitor) {
79         visitor.visitTypeAnnotation(this);
80     }
81
82     
83     public String JavaDoc format(String JavaDoc key, ClassAnnotation primaryClass) {
84         return new SignatureConverter(descriptor).parseNext();
85     }
86
87     public void setDescription(String JavaDoc roleDescription) {
88         this.roleDescription = roleDescription;
89     }
90
91     public String JavaDoc getDescription() {
92         return roleDescription;
93     }
94
95     @Override JavaDoc
96     public int hashCode() {
97         return descriptor.hashCode();
98     }
99
100     @Override JavaDoc
101     public boolean equals(Object JavaDoc 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)) // BugAnnotations must be Comparable with any type of BugAnnotation
109
return this.getClass().getName().compareTo(o.getClass().getName());
110         return descriptor.compareTo(((TypeAnnotation) o).descriptor);
111         // could try to determine equivalence with ClassAnnotation, but don't see how this would be useful
112
}
113
114     @Override JavaDoc
115     public String JavaDoc toString() {
116         String JavaDoc pattern = I18N.instance().getAnnotationDescription(roleDescription);
117         FindBugsMessageFormat format = new FindBugsMessageFormat(pattern);
118         return format.format(new BugAnnotation[]{this}, null);
119     }
120
121     /* ----------------------------------------------------------------------
122      * XML Conversion support
123      * ---------------------------------------------------------------------- */

124
125     private static final String JavaDoc ELEMENT_NAME = "Type";
126
127     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
128         writeXML(xmlOutput, false);
129     }
130
131     public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException JavaDoc {
132         XMLAttributeList attributeList = new XMLAttributeList()
133             .addAttribute("descriptor", descriptor);
134         
135         String JavaDoc 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