KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.classfile.LocalVariable;
25 import org.apache.bcel.classfile.LocalVariableTable;
26 import org.apache.bcel.classfile.Method;
27 import org.apache.bcel.generic.IndexedInstruction;
28 import org.apache.bcel.generic.InstructionHandle;
29
30 import edu.umd.cs.findbugs.ba.Location;
31 import edu.umd.cs.findbugs.xml.XMLAttributeList;
32 import edu.umd.cs.findbugs.xml.XMLOutput;
33
34 /**
35  * Bug annotation class for local variable names
36  *
37  * @author William Pugh
38  * @see BugAnnotation
39  */

40 public class LocalVariableAnnotation implements BugAnnotation {
41     private static final long serialVersionUID = 1L;
42
43     private static final String JavaDoc DEFAULT_ROLE = "LOCAL_VARIABLE_DEFAULT";
44
45     final private String JavaDoc value;
46     final int register, pc;
47      private String JavaDoc description;
48
49     /**
50      * Constructor.
51      *
52      * @param name the name of the local variable
53      * @param register the local variable index
54      * @param pc the bytecode offset of the instruction that mentions
55      * this local variable
56      */

57     public LocalVariableAnnotation(String JavaDoc name, int register, int pc) {
58         this.value = name;
59         this.register = register;
60         this.pc = pc;
61         this.description = DEFAULT_ROLE;
62         this.setDescription(name.equals("?") ? "LOCAL_VARIABLE_UNKNOWN" : "LOCAL_VARIABLE_NAMED");
63     }
64     
65     public static LocalVariableAnnotation getLocalVariableAnnotation(
66             Method method, Location location, IndexedInstruction ins) {
67         int local = ins.getIndex();
68         InstructionHandle handle = location.getHandle();
69         int position1 = handle.getNext().getPosition();
70         int position2 = handle.getPosition();
71         return getLocalVariableAnnotation(method, local, position1, position2);
72     }
73
74     public static LocalVariableAnnotation getLocalVariableAnnotation(
75             Method method, int local, int position1, int position2) {
76
77         LocalVariableTable localVariableTable = method.getLocalVariableTable();
78         String JavaDoc localName = "?";
79         if (localVariableTable != null) {
80             LocalVariable lv1 = localVariableTable.getLocalVariable(local, position1);
81             if (lv1 == null) {
82                 lv1 = localVariableTable.getLocalVariable(local, position2);
83                 position1 = position2;
84             }
85             if (lv1 != null) localName = lv1.getName();
86         }
87         return new LocalVariableAnnotation(localName, local, position1);
88     }
89     
90     @Override JavaDoc
91     public Object JavaDoc clone() {
92         try {
93             return super.clone();
94         } catch (CloneNotSupportedException JavaDoc e) {
95             throw new AssertionError JavaDoc(e);
96         }
97     }
98
99
100     public void accept(BugAnnotationVisitor visitor) {
101         visitor.visitLocalVariableAnnotation(this);
102     }
103
104     public String JavaDoc format(String JavaDoc key, ClassAnnotation primaryClass) {
105         // System.out.println("format: " + key + " reg: " + register + " name: " + value);
106
if (key.equals("hash")) {
107             if (register < 0) return "??";
108             return value;
109         }
110         if (register < 0) return "?";
111         if (key.equals("register")) return String.valueOf(register);
112         else if (key.equals("pc")) return String.valueOf(pc);
113         else if (key.equals("name")) return value;
114         else if (!value.equals("?")) return value;
115         return "$L"+register;
116     }
117
118     public void setDescription(String JavaDoc description) {
119         this.description = description;
120     }
121
122     public String JavaDoc getDescription() {
123         return description;
124     }
125
126     @Override JavaDoc
127     public int hashCode() {
128         return value.hashCode();
129     }
130
131     @Override JavaDoc
132     public boolean equals(Object JavaDoc o) {
133         if (!(o instanceof LocalVariableAnnotation))
134             return false;
135         return value.equals(((LocalVariableAnnotation) o).value);
136     }
137
138     public int compareTo(BugAnnotation o) {
139         if (!(o instanceof LocalVariableAnnotation)) // BugAnnotations must be Comparable with any type of BugAnnotation
140
return this.getClass().getName().compareTo(o.getClass().getName());
141         return value.compareTo(((LocalVariableAnnotation) o).value);
142     }
143
144     @Override JavaDoc
145     public String JavaDoc toString() {
146         String JavaDoc pattern = I18N.instance().getAnnotationDescription(description);
147         FindBugsMessageFormat format = new FindBugsMessageFormat(pattern);
148         return format.format(new BugAnnotation[]{this}, null);
149     }
150
151     /* ----------------------------------------------------------------------
152      * XML Conversion support
153      * ---------------------------------------------------------------------- */

154
155     private static final String JavaDoc ELEMENT_NAME = "LocalVariable";
156
157     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
158         writeXML(xmlOutput, false);
159     }
160
161     public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException JavaDoc {
162         XMLAttributeList attributeList = new XMLAttributeList()
163             .addAttribute("name", value)
164         .addAttribute("register", String.valueOf(register))
165         .addAttribute("pc", String.valueOf(pc));
166         
167         String JavaDoc role = getDescription();
168         if (!role.equals(DEFAULT_ROLE))
169             attributeList.addAttribute("role", role);
170         
171         BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages);
172     }
173
174     /**
175      * @return name of local variable
176      */

177     public String JavaDoc getName() {
178         
179         return value;
180     }
181
182
183     public boolean isSignificant() {
184         return !value.equals("?");
185     }
186 }
187
188 // vim:ts=4
189
Popular Tags