KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > xml > SAXCodeAdapter


1 /***
2  * ASM XML Adapter
3  * Copyright (c) 2004, Eugene Kuleshov
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.xml;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.objectweb.asm.AnnotationVisitor;
36 import org.objectweb.asm.Attribute;
37 import org.objectweb.asm.MethodVisitor;
38 import org.objectweb.asm.Opcodes;
39 import org.objectweb.asm.Label;
40 import org.objectweb.asm.Type;
41 import org.objectweb.asm.util.AbstractVisitor;
42 import org.xml.sax.ContentHandler JavaDoc;
43 import org.xml.sax.helpers.AttributesImpl JavaDoc;
44
45 /**
46  * A {@link MethodVisitor} that generates SAX 2.0 events from the visited
47  * method.
48  *
49  * @see org.objectweb.asm.xml.SAXClassAdapter
50  * @see org.objectweb.asm.xml.Processor
51  *
52  * @author Eugene Kuleshov
53  */

54 public final class SAXCodeAdapter extends SAXAdapter implements MethodVisitor {
55     private Map JavaDoc labelNames;
56
57     /**
58      * Constructs a new {@link SAXCodeAdapter SAXCodeAdapter} object.
59      *
60      * @param h content handler that will be used to send SAX 2.0 events.
61      * @param access
62      */

63     public SAXCodeAdapter(ContentHandler JavaDoc h, int access) {
64         super(h);
65         labelNames = new HashMap JavaDoc();
66
67         if ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_NATIVE)) == 0)
68         {
69             addStart("code", new AttributesImpl JavaDoc());
70         }
71     }
72
73     public final void visitCode() {
74     }
75
76     public final void visitInsn(int opcode) {
77         addElement(AbstractVisitor.OPCODES[opcode], new AttributesImpl JavaDoc());
78     }
79
80     public final void visitIntInsn(int opcode, int operand) {
81         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
82         attrs.addAttribute("", "value", "value", "", Integer.toString(operand));
83         addElement(AbstractVisitor.OPCODES[opcode], attrs);
84     }
85
86     public final void visitVarInsn(int opcode, int var) {
87         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
88         attrs.addAttribute("", "var", "var", "", Integer.toString(var));
89         addElement(AbstractVisitor.OPCODES[opcode], attrs);
90     }
91
92     public final void visitTypeInsn(int opcode, String JavaDoc desc) {
93         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
94         attrs.addAttribute("", "desc", "desc", "", desc);
95         addElement(AbstractVisitor.OPCODES[opcode], attrs);
96     }
97
98     public final void visitFieldInsn(
99         int opcode,
100         String JavaDoc owner,
101         String JavaDoc name,
102         String JavaDoc desc)
103     {
104         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
105         attrs.addAttribute("", "owner", "owner", "", owner);
106         attrs.addAttribute("", "name", "name", "", name);
107         attrs.addAttribute("", "desc", "desc", "", desc);
108         addElement(AbstractVisitor.OPCODES[opcode], attrs);
109     }
110
111     public final void visitMethodInsn(
112         int opcode,
113         String JavaDoc owner,
114         String JavaDoc name,
115         String JavaDoc desc)
116     {
117         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
118         attrs.addAttribute("", "owner", "owner", "", owner);
119         attrs.addAttribute("", "name", "name", "", name);
120         attrs.addAttribute("", "desc", "desc", "", desc);
121         addElement(AbstractVisitor.OPCODES[opcode], attrs);
122     }
123
124     public final void visitJumpInsn(int opcode, Label label) {
125         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
126         attrs.addAttribute("", "label", "label", "", getLabel(label));
127         addElement(AbstractVisitor.OPCODES[opcode], attrs);
128     }
129
130     public final void visitLabel(Label label) {
131         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
132         attrs.addAttribute("", "name", "name", "", getLabel(label));
133         addElement("Label", attrs);
134     }
135
136     public final void visitLdcInsn(Object JavaDoc cst) {
137         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
138         attrs.addAttribute("",
139                 "cst",
140                 "cst",
141                 "",
142                 SAXClassAdapter.encode(cst.toString()));
143         attrs.addAttribute("",
144                 "desc",
145                 "desc",
146                 "",
147                 Type.getDescriptor(cst.getClass()));
148         addElement(AbstractVisitor.OPCODES[Opcodes.LDC], attrs);
149     }
150
151     public final void visitIincInsn(int var, int increment) {
152         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
153         attrs.addAttribute("", "var", "var", "", Integer.toString(var));
154         attrs.addAttribute("", "inc", "inc", "", Integer.toString(increment));
155         addElement(AbstractVisitor.OPCODES[Opcodes.IINC], attrs);
156     }
157
158     public final void visitTableSwitchInsn(
159         int min,
160         int max,
161         Label dflt,
162         Label[] labels)
163     {
164         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
165         attrs.addAttribute("", "min", "min", "", Integer.toString(min));
166         attrs.addAttribute("", "max", "max", "", Integer.toString(max));
167         attrs.addAttribute("", "dflt", "dflt", "", getLabel(dflt));
168         String JavaDoc o = AbstractVisitor.OPCODES[Opcodes.TABLESWITCH];
169         addStart(o, attrs);
170         for (int i = 0; i < labels.length; i++) {
171             AttributesImpl JavaDoc att2 = new AttributesImpl JavaDoc();
172             att2.addAttribute("", "name", "name", "", getLabel(labels[i]));
173             addElement("label", att2);
174         }
175         addEnd(o);
176     }
177
178     public final void visitLookupSwitchInsn(
179         Label dflt,
180         int[] keys,
181         Label[] labels)
182     {
183         AttributesImpl JavaDoc att = new AttributesImpl JavaDoc();
184         att.addAttribute("", "dflt", "dflt", "", getLabel(dflt));
185         String JavaDoc o = AbstractVisitor.OPCODES[Opcodes.LOOKUPSWITCH];
186         addStart(o, att);
187         for (int i = 0; i < labels.length; i++) {
188             AttributesImpl JavaDoc att2 = new AttributesImpl JavaDoc();
189             att2.addAttribute("", "name", "name", "", getLabel(labels[i]));
190             att2.addAttribute("", "key", "key", "", Integer.toString(keys[i]));
191             addElement("label", att2);
192         }
193         addEnd(o);
194     }
195
196     public final void visitMultiANewArrayInsn(String JavaDoc desc, int dims) {
197         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
198         attrs.addAttribute("", "desc", "desc", "", desc);
199         attrs.addAttribute("", "dims", "dims", "", Integer.toString(dims));
200         addElement(AbstractVisitor.OPCODES[Opcodes.MULTIANEWARRAY], attrs);
201     }
202
203     public final void visitTryCatchBlock(
204         Label start,
205         Label end,
206         Label handler,
207         String JavaDoc type)
208     {
209         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
210         attrs.addAttribute("", "start", "start", "", getLabel(start));
211         attrs.addAttribute("", "end", "end", "", getLabel(end));
212         attrs.addAttribute("", "handler", "handler", "", getLabel(handler));
213         if (type != null)
214             attrs.addAttribute("", "type", "type", "", type);
215         addElement("TryCatch", attrs);
216     }
217
218     public final void visitMaxs(int maxStack, int maxLocals) {
219         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
220         attrs.addAttribute("",
221                 "maxStack",
222                 "maxStack",
223                 "",
224                 Integer.toString(maxStack));
225         attrs.addAttribute("",
226                 "maxLocals",
227                 "maxLocals",
228                 "",
229                 Integer.toString(maxLocals));
230         addElement("Max", attrs);
231
232         addEnd("code");
233     }
234
235     public void visitLocalVariable(
236         String JavaDoc name,
237         String JavaDoc desc,
238         String JavaDoc signature,
239         Label start,
240         Label end,
241         int index)
242     {
243         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
244         attrs.addAttribute("", "name", "name", "", name);
245         attrs.addAttribute("", "desc", "desc", "", desc);
246         if (signature != null)
247             attrs.addAttribute("",
248                     "signature",
249                     "signature",
250                     "",
251                     SAXClassAdapter.encode(signature));
252         attrs.addAttribute("", "start", "start", "", getLabel(start));
253         attrs.addAttribute("", "end", "end", "", getLabel(end));
254         attrs.addAttribute("", "var", "var", "", Integer.toString(index));
255         addElement("LocalVar", attrs);
256     }
257
258     public final void visitLineNumber(int line, Label start) {
259         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
260         attrs.addAttribute("", "line", "line", "", Integer.toString(line));
261         attrs.addAttribute("", "start", "start", "", getLabel(start));
262         addElement("LineNumber", attrs);
263     }
264
265     public AnnotationVisitor visitAnnotationDefault() {
266         return new SAXAnnotationAdapter(getContentHandler(),
267                 "annotationDefault",
268                 0,
269                 null,
270                 null);
271     }
272
273     public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
274         return new SAXAnnotationAdapter(getContentHandler(),
275                 "annotation",
276                 visible ? 1 : -1,
277                 null,
278                 desc);
279     }
280
281     public AnnotationVisitor visitParameterAnnotation(
282         int parameter,
283         String JavaDoc desc,
284         boolean visible)
285     {
286         return new SAXAnnotationAdapter(getContentHandler(),
287                 "parameterAnnotation",
288                 visible ? 1 : -1,
289                 parameter,
290                 desc);
291     }
292
293     public void visitEnd() {
294         addEnd("method");
295     }
296
297     public final void visitAttribute(Attribute attr) {
298         // TODO Auto-generated SAXCodeAdapter.visitAttribute
299
}
300
301     private final String JavaDoc getLabel(Label label) {
302         String JavaDoc name = (String JavaDoc) labelNames.get(label);
303         if (name == null) {
304             name = Integer.toString(labelNames.size());
305             labelNames.put(label, name);
306         }
307         return name;
308     }
309
310 }
311
Popular Tags