KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import org.objectweb.asm.AnnotationVisitor;
41 import org.objectweb.asm.ClassVisitor;
42 import org.objectweb.asm.ClassWriter;
43 import org.objectweb.asm.FieldVisitor;
44 import org.objectweb.asm.MethodVisitor;
45 import org.objectweb.asm.Opcodes;
46 import org.objectweb.asm.Label;
47 import org.objectweb.asm.Type;
48
49 import org.xml.sax.Attributes JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51 import org.xml.sax.helpers.DefaultHandler JavaDoc;
52
53 /**
54  * A {@link org.xml.sax.ContentHandler ContentHandler} that transforms XML
55  * document into Java class file. This class can be feeded by any kind of SAX
56  * 2.0 event producers, e.g. XML parser, XSLT or XPath engines, or custom code.
57  *
58  * @see org.objectweb.asm.xml.SAXClassAdapter
59  * @see org.objectweb.asm.xml.Processor
60  *
61  * @author Eugene Kuleshov
62  */

63 public class ASMContentHandler extends DefaultHandler JavaDoc implements Opcodes {
64     /**
65      * Stack of the intermediate processing contexts.
66      */

67     private List JavaDoc stack = new ArrayList JavaDoc();
68
69     /**
70      * Complete name of the current element.
71      */

72     private String JavaDoc match = "";
73
74     /**
75      * <tt>true</tt> if the maximum stack size and number of local variables
76      * must be automatically computed.
77      */

78     protected boolean computeMax;
79
80     /**
81      * Output stream to write result bytecode.
82      */

83     protected OutputStream JavaDoc os;
84
85     /**
86      * Current instance of the {@link ClassWriter ClassWriter} used to write
87      * class bytecode.
88      */

89     protected ClassWriter cw;
90
91     /**
92      * Map of the active {@link Label Label} instances for current method.
93      */

94     protected Map JavaDoc labels;
95
96     private static final String JavaDoc BASE = "class";
97
98     private final RuleSet RULES = new RuleSet();
99     {
100         RULES.add(BASE, new ClassRule());
101         RULES.add(BASE + "/interfaces/interface", new InterfaceRule());
102         RULES.add(BASE + "/interfaces", new InterfacesRule());
103         RULES.add(BASE + "/outerclass", new OuterClassRule());
104         RULES.add(BASE + "/innerclass", new InnerClassRule());
105         RULES.add(BASE + "/source", new SourceRule());
106         RULES.add(BASE + "/field", new FieldRule());
107
108         RULES.add(BASE + "/method", new MethodRule());
109         RULES.add(BASE + "/method/exceptions/exception", new ExceptionRule());
110         RULES.add(BASE + "/method/exceptions", new ExceptionsRule());
111
112         RULES.add(BASE + "/method/annotationDefault",
113                 new AnnotationDefaultRule());
114
115         RULES.add(BASE + "/method/code/*", new OpcodesRule()); // opcodes
116

117         RULES.add(BASE + "/method/code/TABLESWITCH", new TableSwitchRule());
118         RULES.add(BASE + "/method/code/TABLESWITCH/label",
119                 new TableSwitchLabelRule());
120         RULES.add(BASE + "/method/code/LOOKUPSWITCH", new LookupSwitchRule());
121         RULES.add(BASE + "/method/code/LOOKUPSWITCH/label",
122                 new LookupSwitchLabelRule());
123
124         RULES.add(BASE + "/method/code/Label", new LabelRule());
125         RULES.add(BASE + "/method/code/TryCatch", new TryCatchRule());
126         RULES.add(BASE + "/method/code/LineNumber", new LineNumberRule());
127         RULES.add(BASE + "/method/code/LocalVar", new LocalVarRule());
128         RULES.add(BASE + "/method/code/Max", new MaxRule());
129
130         RULES.add("*/annotation", new AnnotationRule());
131         RULES.add("*/parameterAnnotation", new AnnotationParameterRule());
132         RULES.add("*/annotationValue", new AnnotationValueRule());
133         RULES.add("*/annotationValueAnnotation",
134                 new AnnotationValueAnnotationRule());
135         RULES.add("*/annotationValueEnum", new AnnotationValueEnumRule());
136         RULES.add("*/annotationValueArray", new AnnotationValueArrayRule());
137     };
138
139     private static interface OpcodeGroup {
140         public static final int INSN = 0;
141         public static final int INSN_INT = 1;
142         public static final int INSN_VAR = 2;
143         public static final int INSN_TYPE = 3;
144         public static final int INSN_FIELD = 4;
145         public static final int INSN_METHOD = 5;
146         public static final int INSN_JUMP = 6;
147         public static final int INSN_LDC = 7;
148         public static final int INSN_IINC = 8;
149         public static final int INSN_MULTIANEWARRAY = 9;
150     }
151
152     /**
153      * Map of the opcode names to opcode and opcode group
154      */

155     static final Map JavaDoc OPCODES = new HashMap JavaDoc();
156     static {
157         OPCODES.put("NOP", new Opcode(NOP, OpcodeGroup.INSN));
158         OPCODES.put("ACONST_NULL", new Opcode(ACONST_NULL, OpcodeGroup.INSN));
159         OPCODES.put("ICONST_M1", new Opcode(ICONST_M1, OpcodeGroup.INSN));
160         OPCODES.put("ICONST_0", new Opcode(ICONST_0, OpcodeGroup.INSN));
161         OPCODES.put("ICONST_1", new Opcode(ICONST_1, OpcodeGroup.INSN));
162         OPCODES.put("ICONST_2", new Opcode(ICONST_2, OpcodeGroup.INSN));
163         OPCODES.put("ICONST_3", new Opcode(ICONST_3, OpcodeGroup.INSN));
164         OPCODES.put("ICONST_4", new Opcode(ICONST_4, OpcodeGroup.INSN));
165         OPCODES.put("ICONST_5", new Opcode(ICONST_5, OpcodeGroup.INSN));
166         OPCODES.put("LCONST_0", new Opcode(LCONST_0, OpcodeGroup.INSN));
167         OPCODES.put("LCONST_1", new Opcode(LCONST_1, OpcodeGroup.INSN));
168         OPCODES.put("FCONST_0", new Opcode(FCONST_0, OpcodeGroup.INSN));
169         OPCODES.put("FCONST_1", new Opcode(FCONST_1, OpcodeGroup.INSN));
170         OPCODES.put("FCONST_2", new Opcode(FCONST_2, OpcodeGroup.INSN));
171         OPCODES.put("DCONST_0", new Opcode(DCONST_0, OpcodeGroup.INSN));
172         OPCODES.put("DCONST_1", new Opcode(DCONST_1, OpcodeGroup.INSN));
173         OPCODES.put("BIPUSH", new Opcode(BIPUSH, OpcodeGroup.INSN_INT));
174         OPCODES.put("SIPUSH", new Opcode(SIPUSH, OpcodeGroup.INSN_INT));
175         OPCODES.put("LDC", new Opcode(LDC, OpcodeGroup.INSN_LDC));
176         OPCODES.put("ILOAD", new Opcode(ILOAD, OpcodeGroup.INSN_VAR));
177         OPCODES.put("LLOAD", new Opcode(LLOAD, OpcodeGroup.INSN_VAR));
178         OPCODES.put("FLOAD", new Opcode(FLOAD, OpcodeGroup.INSN_VAR));
179         OPCODES.put("DLOAD", new Opcode(DLOAD, OpcodeGroup.INSN_VAR));
180         OPCODES.put("ALOAD", new Opcode(ALOAD, OpcodeGroup.INSN_VAR));
181         OPCODES.put("IALOAD", new Opcode(IALOAD, OpcodeGroup.INSN));
182         OPCODES.put("LALOAD", new Opcode(LALOAD, OpcodeGroup.INSN));
183         OPCODES.put("FALOAD", new Opcode(FALOAD, OpcodeGroup.INSN));
184         OPCODES.put("DALOAD", new Opcode(DALOAD, OpcodeGroup.INSN));
185         OPCODES.put("AALOAD", new Opcode(AALOAD, OpcodeGroup.INSN));
186         OPCODES.put("BALOAD", new Opcode(BALOAD, OpcodeGroup.INSN));
187         OPCODES.put("CALOAD", new Opcode(CALOAD, OpcodeGroup.INSN));
188         OPCODES.put("SALOAD", new Opcode(SALOAD, OpcodeGroup.INSN));
189         OPCODES.put("ISTORE", new Opcode(ISTORE, OpcodeGroup.INSN_VAR));
190         OPCODES.put("LSTORE", new Opcode(LSTORE, OpcodeGroup.INSN_VAR));
191         OPCODES.put("FSTORE", new Opcode(FSTORE, OpcodeGroup.INSN_VAR));
192         OPCODES.put("DSTORE", new Opcode(DSTORE, OpcodeGroup.INSN_VAR));
193         OPCODES.put("ASTORE", new Opcode(ASTORE, OpcodeGroup.INSN_VAR));
194         OPCODES.put("IASTORE", new Opcode(IASTORE, OpcodeGroup.INSN));
195         OPCODES.put("LASTORE", new Opcode(LASTORE, OpcodeGroup.INSN));
196         OPCODES.put("FASTORE", new Opcode(FASTORE, OpcodeGroup.INSN));
197         OPCODES.put("DASTORE", new Opcode(DASTORE, OpcodeGroup.INSN));
198         OPCODES.put("AASTORE", new Opcode(AASTORE, OpcodeGroup.INSN));
199         OPCODES.put("BASTORE", new Opcode(BASTORE, OpcodeGroup.INSN));
200         OPCODES.put("CASTORE", new Opcode(CASTORE, OpcodeGroup.INSN));
201         OPCODES.put("SASTORE", new Opcode(SASTORE, OpcodeGroup.INSN));
202         OPCODES.put("POP", new Opcode(POP, OpcodeGroup.INSN));
203         OPCODES.put("POP2", new Opcode(POP2, OpcodeGroup.INSN));
204         OPCODES.put("DUP", new Opcode(DUP, OpcodeGroup.INSN));
205         OPCODES.put("DUP_X1", new Opcode(DUP_X1, OpcodeGroup.INSN));
206         OPCODES.put("DUP_X2", new Opcode(DUP_X2, OpcodeGroup.INSN));
207         OPCODES.put("DUP2", new Opcode(DUP2, OpcodeGroup.INSN));
208         OPCODES.put("DUP2_X1", new Opcode(DUP2_X1, OpcodeGroup.INSN));
209         OPCODES.put("DUP2_X2", new Opcode(DUP2_X2, OpcodeGroup.INSN));
210         OPCODES.put("SWAP", new Opcode(SWAP, OpcodeGroup.INSN));
211         OPCODES.put("IADD", new Opcode(IADD, OpcodeGroup.INSN));
212         OPCODES.put("LADD", new Opcode(LADD, OpcodeGroup.INSN));
213         OPCODES.put("FADD", new Opcode(FADD, OpcodeGroup.INSN));
214         OPCODES.put("DADD", new Opcode(DADD, OpcodeGroup.INSN));
215         OPCODES.put("ISUB", new Opcode(ISUB, OpcodeGroup.INSN));
216         OPCODES.put("LSUB", new Opcode(LSUB, OpcodeGroup.INSN));
217         OPCODES.put("FSUB", new Opcode(FSUB, OpcodeGroup.INSN));
218         OPCODES.put("DSUB", new Opcode(DSUB, OpcodeGroup.INSN));
219         OPCODES.put("IMUL", new Opcode(IMUL, OpcodeGroup.INSN));
220         OPCODES.put("LMUL", new Opcode(LMUL, OpcodeGroup.INSN));
221         OPCODES.put("FMUL", new Opcode(FMUL, OpcodeGroup.INSN));
222         OPCODES.put("DMUL", new Opcode(DMUL, OpcodeGroup.INSN));
223         OPCODES.put("IDIV", new Opcode(IDIV, OpcodeGroup.INSN));
224         OPCODES.put("LDIV", new Opcode(LDIV, OpcodeGroup.INSN));
225         OPCODES.put("FDIV", new Opcode(FDIV, OpcodeGroup.INSN));
226         OPCODES.put("DDIV", new Opcode(DDIV, OpcodeGroup.INSN));
227         OPCODES.put("IREM", new Opcode(IREM, OpcodeGroup.INSN));
228         OPCODES.put("LREM", new Opcode(LREM, OpcodeGroup.INSN));
229         OPCODES.put("FREM", new Opcode(FREM, OpcodeGroup.INSN));
230         OPCODES.put("DREM", new Opcode(DREM, OpcodeGroup.INSN));
231         OPCODES.put("INEG", new Opcode(INEG, OpcodeGroup.INSN));
232         OPCODES.put("LNEG", new Opcode(LNEG, OpcodeGroup.INSN));
233         OPCODES.put("FNEG", new Opcode(FNEG, OpcodeGroup.INSN));
234         OPCODES.put("DNEG", new Opcode(DNEG, OpcodeGroup.INSN));
235         OPCODES.put("ISHL", new Opcode(ISHL, OpcodeGroup.INSN));
236         OPCODES.put("LSHL", new Opcode(LSHL, OpcodeGroup.INSN));
237         OPCODES.put("ISHR", new Opcode(ISHR, OpcodeGroup.INSN));
238         OPCODES.put("LSHR", new Opcode(LSHR, OpcodeGroup.INSN));
239         OPCODES.put("IUSHR", new Opcode(IUSHR, OpcodeGroup.INSN));
240         OPCODES.put("LUSHR", new Opcode(LUSHR, OpcodeGroup.INSN));
241         OPCODES.put("IAND", new Opcode(IAND, OpcodeGroup.INSN));
242         OPCODES.put("LAND", new Opcode(LAND, OpcodeGroup.INSN));
243         OPCODES.put("IOR", new Opcode(IOR, OpcodeGroup.INSN));
244         OPCODES.put("LOR", new Opcode(LOR, OpcodeGroup.INSN));
245         OPCODES.put("IXOR", new Opcode(IXOR, OpcodeGroup.INSN));
246         OPCODES.put("LXOR", new Opcode(LXOR, OpcodeGroup.INSN));
247         OPCODES.put("IINC", new Opcode(IINC, OpcodeGroup.INSN_IINC));
248         OPCODES.put("I2L", new Opcode(I2L, OpcodeGroup.INSN));
249         OPCODES.put("I2F", new Opcode(I2F, OpcodeGroup.INSN));
250         OPCODES.put("I2D", new Opcode(I2D, OpcodeGroup.INSN));
251         OPCODES.put("L2I", new Opcode(L2I, OpcodeGroup.INSN));
252         OPCODES.put("L2F", new Opcode(L2F, OpcodeGroup.INSN));
253         OPCODES.put("L2D", new Opcode(L2D, OpcodeGroup.INSN));
254         OPCODES.put("F2I", new Opcode(F2I, OpcodeGroup.INSN));
255         OPCODES.put("F2L", new Opcode(F2L, OpcodeGroup.INSN));
256         OPCODES.put("F2D", new Opcode(F2D, OpcodeGroup.INSN));
257         OPCODES.put("D2I", new Opcode(D2I, OpcodeGroup.INSN));
258         OPCODES.put("D2L", new Opcode(D2L, OpcodeGroup.INSN));
259         OPCODES.put("D2F", new Opcode(D2F, OpcodeGroup.INSN));
260         OPCODES.put("I2B", new Opcode(I2B, OpcodeGroup.INSN));
261         OPCODES.put("I2C", new Opcode(I2C, OpcodeGroup.INSN));
262         OPCODES.put("I2S", new Opcode(I2S, OpcodeGroup.INSN));
263         OPCODES.put("LCMP", new Opcode(LCMP, OpcodeGroup.INSN));
264         OPCODES.put("FCMPL", new Opcode(FCMPL, OpcodeGroup.INSN));
265         OPCODES.put("FCMPG", new Opcode(FCMPG, OpcodeGroup.INSN));
266         OPCODES.put("DCMPL", new Opcode(DCMPL, OpcodeGroup.INSN));
267         OPCODES.put("DCMPG", new Opcode(DCMPG, OpcodeGroup.INSN));
268         OPCODES.put("IFEQ", new Opcode(IFEQ, OpcodeGroup.INSN_JUMP));
269         OPCODES.put("IFNE", new Opcode(IFNE, OpcodeGroup.INSN_JUMP));
270         OPCODES.put("IFLT", new Opcode(IFLT, OpcodeGroup.INSN_JUMP));
271         OPCODES.put("IFGE", new Opcode(IFGE, OpcodeGroup.INSN_JUMP));
272         OPCODES.put("IFGT", new Opcode(IFGT, OpcodeGroup.INSN_JUMP));
273         OPCODES.put("IFLE", new Opcode(IFLE, OpcodeGroup.INSN_JUMP));
274         OPCODES.put("IF_ICMPEQ", new Opcode(IF_ICMPEQ, OpcodeGroup.INSN_JUMP));
275         OPCODES.put("IF_ICMPNE", new Opcode(IF_ICMPNE, OpcodeGroup.INSN_JUMP));
276         OPCODES.put("IF_ICMPLT", new Opcode(IF_ICMPLT, OpcodeGroup.INSN_JUMP));
277         OPCODES.put("IF_ICMPGE", new Opcode(IF_ICMPGE, OpcodeGroup.INSN_JUMP));
278         OPCODES.put("IF_ICMPGT", new Opcode(IF_ICMPGT, OpcodeGroup.INSN_JUMP));
279         OPCODES.put("IF_ICMPLE", new Opcode(IF_ICMPLE, OpcodeGroup.INSN_JUMP));
280         OPCODES.put("IF_ACMPEQ", new Opcode(IF_ACMPEQ, OpcodeGroup.INSN_JUMP));
281         OPCODES.put("IF_ACMPNE", new Opcode(IF_ACMPNE, OpcodeGroup.INSN_JUMP));
282         OPCODES.put("GOTO", new Opcode(GOTO, OpcodeGroup.INSN_JUMP));
283         OPCODES.put("JSR", new Opcode(JSR, OpcodeGroup.INSN_JUMP));
284         OPCODES.put("RET", new Opcode(RET, OpcodeGroup.INSN_VAR));
285         // OPCODES.put( "TABLESWITCH", new Opcode( TABLESWITCH,
286
// "visiTableSwitchInsn"));
287
// OPCODES.put( "LOOKUPSWITCH", new Opcode( LOOKUPSWITCH,
288
// "visitLookupSwitch"));
289
OPCODES.put("IRETURN", new Opcode(IRETURN, OpcodeGroup.INSN));
290         OPCODES.put("LRETURN", new Opcode(LRETURN, OpcodeGroup.INSN));
291         OPCODES.put("FRETURN", new Opcode(FRETURN, OpcodeGroup.INSN));
292         OPCODES.put("DRETURN", new Opcode(DRETURN, OpcodeGroup.INSN));
293         OPCODES.put("ARETURN", new Opcode(ARETURN, OpcodeGroup.INSN));
294         OPCODES.put("RETURN", new Opcode(RETURN, OpcodeGroup.INSN));
295         OPCODES.put("GETSTATIC", new Opcode(GETSTATIC, OpcodeGroup.INSN_FIELD));
296         OPCODES.put("PUTSTATIC", new Opcode(PUTSTATIC, OpcodeGroup.INSN_FIELD));
297         OPCODES.put("GETFIELD", new Opcode(GETFIELD, OpcodeGroup.INSN_FIELD));
298         OPCODES.put("PUTFIELD", new Opcode(PUTFIELD, OpcodeGroup.INSN_FIELD));
299         OPCODES.put("INVOKEVIRTUAL", new Opcode(INVOKEVIRTUAL,
300                 OpcodeGroup.INSN_METHOD));
301         OPCODES.put("INVOKESPECIAL", new Opcode(INVOKESPECIAL,
302                 OpcodeGroup.INSN_METHOD));
303         OPCODES.put("INVOKESTATIC", new Opcode(INVOKESTATIC,
304                 OpcodeGroup.INSN_METHOD));
305         OPCODES.put("INVOKEINTERFACE", new Opcode(INVOKEINTERFACE,
306                 OpcodeGroup.INSN_METHOD));
307         OPCODES.put("NEW", new Opcode(NEW, OpcodeGroup.INSN_TYPE));
308         OPCODES.put("NEWARRAY", new Opcode(NEWARRAY, OpcodeGroup.INSN_INT));
309         OPCODES.put("ANEWARRAY", new Opcode(ANEWARRAY, OpcodeGroup.INSN_TYPE));
310         OPCODES.put("ARRAYLENGTH", new Opcode(ARRAYLENGTH, OpcodeGroup.INSN));
311         OPCODES.put("ATHROW", new Opcode(ATHROW, OpcodeGroup.INSN));
312         OPCODES.put("CHECKCAST", new Opcode(CHECKCAST, OpcodeGroup.INSN_TYPE));
313         OPCODES.put("INSTANCEOF", new Opcode(INSTANCEOF, OpcodeGroup.INSN_TYPE));
314         OPCODES.put("MONITORENTER", new Opcode(MONITORENTER, OpcodeGroup.INSN));
315         OPCODES.put("MONITOREXIT", new Opcode(MONITOREXIT, OpcodeGroup.INSN));
316         OPCODES.put("MULTIANEWARRAY", new Opcode(MULTIANEWARRAY,
317                 OpcodeGroup.INSN_MULTIANEWARRAY));
318         OPCODES.put("IFNULL", new Opcode(IFNULL, OpcodeGroup.INSN_JUMP));
319         OPCODES.put("IFNONNULL", new Opcode(IFNONNULL, OpcodeGroup.INSN_JUMP));
320     }
321
322     /**
323      * Constructs a new {@link ASMContentHandler ASMContentHandler} object.
324      *
325      * @param os output stream to write generated class.
326      * @param computeMax <tt>true</tt> if the maximum stack size and the
327      * maximum number of local variables must be automatically computed.
328      * This value is passed to {@link ClassWriter ClassWriter} instance.
329      */

330     public ASMContentHandler(OutputStream JavaDoc os, boolean computeMax) {
331         this.os = os;
332         this.computeMax = computeMax;
333     }
334
335     /**
336      * Returns the bytecode of the class that was build with underneath class
337      * writer.
338      *
339      * @return the bytecode of the class that was build with underneath class
340      * writer or null if there are no classwriter created.
341      */

342     public byte[] toByteArray() {
343         return cw == null ? null : cw.toByteArray();
344     }
345
346     /**
347      * Process notification of the start of an XML element being reached.
348      *
349      * @param ns - The Namespace URI, or the empty string if the element has no
350      * Namespace URI or if Namespace processing is not being performed.
351      * @param localName - The local name (without prefix), or the empty string
352      * if Namespace processing is not being performed.
353      * @param qName - The qualified name (with prefix), or the empty string if
354      * qualified names are not available.
355      * @param list - The attributes attached to the element. If there are no
356      * attributes, it shall be an empty Attributes object.
357      * @exception SAXException if a parsing error is to be reported
358      */

359     public final void startElement(
360         String JavaDoc ns,
361         String JavaDoc localName,
362         String JavaDoc qName,
363         Attributes JavaDoc list) throws SAXException JavaDoc
364     {
365         // the actual element name is either in localName or qName, depending
366
// on whether the parser is namespace aware
367
String JavaDoc name = localName;
368         if (name == null || name.length() < 1) {
369             name = qName;
370         }
371
372         // Compute the current matching rule
373
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(match);
374         if (match.length() > 0) {
375             sb.append('/');
376         }
377         sb.append(name);
378         match = sb.toString();
379
380         // Fire "begin" events for all relevant rules
381
Rule r = (Rule) RULES.match(match);
382         if (r != null)
383             r.begin(name, list);
384     }
385
386     /**
387      * Process notification of the end of an XML element being reached.
388      *
389      * @param ns - The Namespace URI, or the empty string if the element has no
390      * Namespace URI or if Namespace processing is not being performed.
391      * @param localName - The local name (without prefix), or the empty string
392      * if Namespace processing is not being performed.
393      * @param qName - The qualified XML 1.0 name (with prefix), or the empty
394      * string if qualified names are not available.
395      *
396      * @exception SAXException if a parsing error is to be reported
397      */

398     public final void endElement(String JavaDoc ns, String JavaDoc localName, String JavaDoc qName)
399             throws SAXException JavaDoc
400     {
401         // the actual element name is either in localName or qName, depending
402
// on whether the parser is namespace aware
403
String JavaDoc name = localName;
404         if (name == null || name.length() < 1) {
405             name = qName;
406         }
407
408         // Fire "end" events for all relevant rules in reverse order
409
Rule r = (Rule) RULES.match(match);
410         if (r != null)
411             r.end(name);
412
413         // Recover the previous match expression
414
int slash = match.lastIndexOf('/');
415         if (slash >= 0) {
416             match = match.substring(0, slash);
417         } else {
418             match = "";
419         }
420     }
421
422     /**
423      * Process notification of the end of a document and write generated
424      * bytecode into output stream.
425      *
426      * @exception SAXException if parsing or writing error is to be reported.
427      */

428     public final void endDocument() throws SAXException JavaDoc {
429         try {
430             os.write(cw.toByteArray());
431         } catch (IOException JavaDoc ex) {
432             throw new SAXException JavaDoc(ex.toString(), ex);
433         }
434     }
435
436     /**
437      * Return the top object on the stack without removing it. If there are no
438      * objects on the stack, return <code>null</code>.
439      */

440     final Object JavaDoc peek() {
441         return stack.size() == 0 ? null : stack.get(stack.size() - 1);
442     }
443
444     /**
445      * Return the n'th object down the stack, where 0 is the top element and
446      * [getCount()-1] is the bottom element. If the specified index is out of
447      * range, return <code>null</code>.
448      *
449      * @param n Index of the desired element, where 0 is the top of the stack, 1
450      * is the next element down, and so on.
451      */

452     final Object JavaDoc peek(int n) {
453         return stack.size() < (n + 1) ? null : stack.get(n);
454     }
455
456     /**
457      * Pop the top object off of the stack, and return it. If there are no
458      * objects on the stack, return <code>null</code>.
459      */

460     final Object JavaDoc pop() {
461         return stack.size() == 0 ? null : stack.remove(stack.size() - 1);
462     }
463
464     /**
465      * Push a new object onto the top of the object stack.
466      *
467      * @param object The new object
468      */

469     final void push(Object JavaDoc object) {
470         stack.add(object);
471     }
472
473     private static final class RuleSet {
474         private Map JavaDoc rules = new HashMap JavaDoc();
475
476         private List JavaDoc lpatterns = new ArrayList JavaDoc();
477
478         private List JavaDoc rpatterns = new ArrayList JavaDoc();
479
480         public void add(String JavaDoc path, Object JavaDoc rule) {
481             String JavaDoc pattern = path;
482             if (path.startsWith("*/")) {
483                 pattern = path.substring(1);
484                 lpatterns.add(pattern);
485             } else if (path.endsWith("/*")) {
486                 pattern = path.substring(0, path.length() - 1);
487                 rpatterns.add(pattern);
488             }
489             rules.put(pattern, rule);
490         }
491
492         public Object JavaDoc match(String JavaDoc path) {
493             if (rules.containsKey(path)) {
494                 return rules.get(path);
495             }
496
497             int n = path.lastIndexOf('/');
498             for (Iterator JavaDoc it = lpatterns.iterator(); it.hasNext();) {
499                 String JavaDoc pattern = (String JavaDoc) it.next();
500                 if (path.substring(n).endsWith(pattern)) {
501                     return rules.get(pattern);
502                 }
503             }
504
505             for (Iterator JavaDoc it = rpatterns.iterator(); it.hasNext();) {
506                 String JavaDoc pattern = (String JavaDoc) it.next();
507                 if (path.startsWith(pattern)) {
508                     return rules.get(pattern);
509                 }
510             }
511
512             return null;
513         }
514
515     }
516
517     /**
518      * Rule
519      */

520     protected abstract class Rule {
521
522         public void begin(String JavaDoc name, Attributes JavaDoc attrs) {
523         }
524
525         public void end(String JavaDoc name) {
526         }
527
528         protected final Object JavaDoc getValue(String JavaDoc desc, String JavaDoc val) {
529             Object JavaDoc value = null;
530             if (val != null) {
531                 if (desc.equals("Ljava/lang/String;")) {
532                     value = decode(val);
533                 } else if ("Ljava/lang/Integer;".equals(desc)
534                         || "I".equals(desc) || "S".equals(desc)
535                         || "B".equals(desc) || "C".equals(desc)
536                         || desc.equals("Z"))
537                 {
538                     value = new Integer JavaDoc(val);
539
540                 } else if ("Ljava/lang/Short;".equals(desc)) {
541                     value = new Short JavaDoc(val);
542
543                 } else if ("Ljava/lang/Byte;".equals(desc)) {
544                     value = new Byte JavaDoc(val);
545
546                 } else if ("Ljava/lang/Character;".equals(desc)) {
547                     value = new Character JavaDoc(decode(val).charAt(0));
548
549                 } else if ("Ljava/lang/Boolean;".equals(desc)) {
550                     value = Boolean.valueOf(val);
551
552                     // } else if ("Ljava/lang/Integer;".equals(desc)
553
// || desc.equals("I"))
554
// {
555
// value = new Integer(val);
556
// } else if ("Ljava/lang/Character;".equals(desc)
557
// || desc.equals("C"))
558
// {
559
// value = new Character(decode(val).charAt(0));
560
// } else if ("Ljava/lang/Short;".equals(desc) ||
561
// desc.equals("S"))
562
// {
563
// value = Short.valueOf(val);
564
// } else if ("Ljava/lang/Byte;".equals(desc) ||
565
// desc.equals("B"))
566
// {
567
// value = Byte.valueOf(val);
568

569                 } else if ("Ljava/lang/Long;".equals(desc) || desc.equals("J"))
570                 {
571                     value = new Long JavaDoc(val);
572                 } else if ("Ljava/lang/Float;".equals(desc) || desc.equals("F"))
573                 {
574                     value = new Float JavaDoc(val);
575                 } else if ("Ljava/lang/Double;".equals(desc)
576                         || desc.equals("D"))
577                 {
578                     value = new Double JavaDoc(val);
579                 } else if (Type.getDescriptor(Type.class).equals(desc)) {
580                     value = Type.getType(val);
581
582                     // } else if ("[I".equals(desc)) {
583
// value = new int[0]; // TODO
584
// } else if ("[C".equals(desc)) {
585
// value = new char[0]; // TODO
586
// } else if ("[Z".equals(desc)) {
587
// value = new boolean[0]; // TODO
588
// } else if ("[S".equals(desc)) {
589
// value = new short[0]; // TODO
590
// } else if ("[B".equals(desc)) {
591
// value = new byte[0]; // TODO
592
// } else if ("[J".equals(desc)) {
593
// value = new long[0]; // TODO
594
// } else if ("[F".equals(desc)) {
595
// value = new float[0]; // TODO
596
// } else if ("[D".equals(desc)) {
597
// value = new double[0]; // TODO
598

599                 } else {
600                     throw new RuntimeException JavaDoc("Invalid value:" + val
601                             + " desc:" + desc + " ctx:" + this);
602                 }
603             }
604             return value;
605         }
606
607         private final String JavaDoc decode(String JavaDoc val) {
608             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(val.length());
609             try {
610                 int n = 0;
611                 while (n < val.length()) {
612                     char c = val.charAt(n);
613                     if (c == '\\') {
614                         n++;
615                         c = val.charAt(n);
616                         if (c == '\\') {
617                             sb.append('\\');
618                         } else {
619                             n++; // skip 'u'
620
sb.append((char) Integer.parseInt(val.substring(n,
621                                     n + 4), 16));
622                             n += 3;
623                         }
624                     } else {
625                         sb.append(c);
626                     }
627                     n++;
628                 }
629
630             } catch (RuntimeException JavaDoc ex) {
631                 System.err.println(val + "\n" + ex.toString());
632                 ex.printStackTrace();
633                 throw ex;
634             }
635             return sb.toString();
636         }
637
638         protected final Label getLabel(Object JavaDoc label) {
639             Label lbl = (Label) labels.get(label);
640             if (lbl == null) {
641                 lbl = new Label();
642                 labels.put(label, lbl);
643             }
644             return lbl;
645         }
646
647         // TODO verify move to stack
648
protected final MethodVisitor getCodeVisitor() {
649             return (MethodVisitor) peek();
650         }
651         
652         protected final int getAccess(String JavaDoc s) {
653             int access = 0;
654             if (s.indexOf("public") != -1)
655                 access |= Opcodes.ACC_PUBLIC;
656             if (s.indexOf("private") != -1)
657                 access |= Opcodes.ACC_PRIVATE;
658             if (s.indexOf("protected") != -1)
659                 access |= Opcodes.ACC_PROTECTED;
660             if (s.indexOf("static") != -1)
661                 access |= Opcodes.ACC_STATIC;
662             if (s.indexOf("final") != -1)
663                 access |= Opcodes.ACC_FINAL;
664             if (s.indexOf("super") != -1)
665                 access |= Opcodes.ACC_SUPER;
666             if (s.indexOf("synchronized") != -1)
667                 access |= Opcodes.ACC_SYNCHRONIZED;
668             if (s.indexOf("volatile") != -1)
669                 access |= Opcodes.ACC_VOLATILE;
670             if (s.indexOf("bridge") != -1)
671                 access |= Opcodes.ACC_BRIDGE;
672             if (s.indexOf("varargs") != -1)
673                 access |= Opcodes.ACC_VARARGS;
674             if (s.indexOf("transient") != -1)
675                 access |= Opcodes.ACC_TRANSIENT;
676             if (s.indexOf("native") != -1)
677                 access |= Opcodes.ACC_NATIVE;
678             if (s.indexOf("interface") != -1)
679                 access |= Opcodes.ACC_INTERFACE;
680             if (s.indexOf("abstract") != -1)
681                 access |= Opcodes.ACC_ABSTRACT;
682             if (s.indexOf("strict") != -1)
683                 access |= Opcodes.ACC_STRICT;
684             if (s.indexOf("synthetic") != -1)
685                 access |= Opcodes.ACC_SYNTHETIC;
686             if (s.indexOf("annotation") != -1)
687                 access |= Opcodes.ACC_ANNOTATION;
688             if (s.indexOf("enum") != -1)
689                 access |= Opcodes.ACC_ENUM;
690             if (s.indexOf("deprecated") != -1)
691                 access |= Opcodes.ACC_DEPRECATED;
692             return access;
693         }
694
695     }
696
697     /**
698      * ClassRule
699      */

700     private final class ClassRule extends Rule {
701
702         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
703             int major = Integer.parseInt(attrs.getValue("major"));
704             int minor = Integer.parseInt(attrs.getValue("minor"));
705             cw = new ClassWriter(computeMax);
706             Map JavaDoc vals = new HashMap JavaDoc();
707             vals.put("version", new Integer JavaDoc(minor << 16 | major));
708             vals.put("access", attrs.getValue("access"));
709             vals.put("name", attrs.getValue("name"));
710             vals.put("parent", attrs.getValue("parent"));
711             vals.put("source", attrs.getValue("source"));
712             vals.put("signature", attrs.getValue("signature"));
713             vals.put("interfaces", new ArrayList JavaDoc());
714             push(vals);
715             // values will be extracted in InterfacesRule.end();
716
}
717
718     }
719
720     private final class SourceRule extends Rule {
721
722         public void begin(String JavaDoc name, Attributes JavaDoc attrs) {
723             String JavaDoc file = attrs.getValue("file");
724             String JavaDoc debug = attrs.getValue("debug");
725             cw.visitSource(file, debug);
726         }
727
728     }
729
730     /**
731      * InterfaceRule
732      */

733     private final class InterfaceRule extends Rule {
734
735         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
736             ((List JavaDoc) ((Map JavaDoc) peek()).get("interfaces")).add(attrs.getValue("name"));
737         }
738
739     }
740
741     /**
742      * InterfacesRule
743      */

744     private final class InterfacesRule extends Rule {
745
746         public final void end(String JavaDoc element) {
747             Map JavaDoc vals = (Map JavaDoc) pop();
748             int version = ((Integer JavaDoc) vals.get("version")).intValue();
749             int access = getAccess((String JavaDoc) vals.get("access"));
750             String JavaDoc name = (String JavaDoc) vals.get("name");
751             String JavaDoc signature = (String JavaDoc) vals.get("signature");
752             String JavaDoc parent = (String JavaDoc) vals.get("parent");
753             String JavaDoc[] interfaces = (String JavaDoc[]) ((List JavaDoc) vals.get("interfaces")).toArray(new String JavaDoc[0]);
754             cw.visit(version, access, name, signature, parent, interfaces);
755             push(cw);
756         }
757
758     }
759
760     /**
761      * OuterClassRule
762      */

763     private final class OuterClassRule extends Rule {
764
765         public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
766             String JavaDoc owner = attrs.getValue("owner");
767             String JavaDoc name = attrs.getValue("name");
768             String JavaDoc desc = attrs.getValue("desc");
769             cw.visitOuterClass(owner, name, desc);
770         }
771
772     }
773
774     /**
775      * InnerClassRule
776      */

777     private final class InnerClassRule extends Rule {
778
779         public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
780             int access = getAccess(attrs.getValue("access"));
781             String JavaDoc name = attrs.getValue("name");
782             String JavaDoc outerName = attrs.getValue("outerName");
783             String JavaDoc innerName = attrs.getValue("innerName");
784             cw.visitInnerClass(name, outerName, innerName, access);
785         }
786
787     }
788
789     /**
790      * FieldRule
791      */

792     private final class FieldRule extends Rule {
793
794         public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
795             int access = getAccess(attrs.getValue("access"));
796             String JavaDoc name = attrs.getValue("name");
797             String JavaDoc signature = attrs.getValue("signature");
798             String JavaDoc desc = attrs.getValue("desc");
799             Object JavaDoc value = getValue(desc, attrs.getValue("value"));
800             push(cw.visitField(access, name, desc, signature, value));
801         }
802
803         public void end(String JavaDoc name) {
804             ((FieldVisitor) pop()).visitEnd();
805         }
806
807     }
808
809     /**
810      * MethodRule
811      */

812     private final class MethodRule extends Rule {
813
814         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
815             labels = new HashMap JavaDoc();
816             Map JavaDoc vals = new HashMap JavaDoc();
817             vals.put("access", attrs.getValue("access"));
818             vals.put("name", attrs.getValue("name"));
819             vals.put("desc", attrs.getValue("desc"));
820             vals.put("signature", attrs.getValue("signature"));
821             vals.put("exceptions", new ArrayList JavaDoc());
822             push(vals);
823             // values will be extracted in ExceptionsRule.end();
824
}
825
826         public final void end(String JavaDoc name) {
827             ((MethodVisitor) pop()).visitEnd();
828             labels = null;
829         }
830
831     }
832
833     /**
834      * ExceptionRule
835      */

836     private final class ExceptionRule extends Rule {
837
838         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
839             ((List JavaDoc) ((Map JavaDoc) peek()).get("exceptions")).add(attrs.getValue("name"));
840         }
841
842     }
843
844     /**
845      * ExceptionsRule
846      */

847     private final class ExceptionsRule extends Rule {
848
849         public final void end(String JavaDoc element) {
850             Map JavaDoc vals = (Map JavaDoc) pop();
851             int access = getAccess((String JavaDoc) vals.get("access"));
852             String JavaDoc name = (String JavaDoc) vals.get("name");
853             String JavaDoc desc = (String JavaDoc) vals.get("desc");
854             String JavaDoc signature = (String JavaDoc) vals.get("signature");
855             String JavaDoc[] exceptions = (String JavaDoc[]) ((List JavaDoc) vals.get("exceptions")).toArray(new String JavaDoc[0]);
856
857             push(cw.visitMethod(access, name, desc, signature, exceptions));
858         }
859
860     }
861
862     /**
863      * TableSwitchRule
864      */

865     private class TableSwitchRule extends Rule {
866
867         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
868             Map JavaDoc vals = new HashMap JavaDoc();
869             vals.put("min", attrs.getValue("min"));
870             vals.put("max", attrs.getValue("max"));
871             vals.put("dflt", attrs.getValue("dflt"));
872             vals.put("labels", new ArrayList JavaDoc());
873             push(vals);
874         }
875
876         public final void end(String JavaDoc name) {
877             Map JavaDoc vals = (Map JavaDoc) pop();
878             int min = Integer.parseInt((String JavaDoc) vals.get("min"));
879             int max = Integer.parseInt((String JavaDoc) vals.get("max"));
880             Label dflt = getLabel(vals.get("dflt"));
881             Label[] lbls = (Label[]) ((List JavaDoc) vals.get("labels")).toArray(new Label[0]);
882             getCodeVisitor().visitTableSwitchInsn(min, max, dflt, lbls);
883         }
884
885     }
886
887     /**
888      * TableSwitchLabelRule
889      */

890     private final class TableSwitchLabelRule extends Rule {
891
892         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
893             ((List JavaDoc) ((Map JavaDoc) peek()).get("labels")).add(getLabel(attrs.getValue("name")));
894         }
895
896     }
897
898     /**
899      * LookupSwitchRule
900      */

901     private final class LookupSwitchRule extends Rule {
902
903         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
904             Map JavaDoc vals = new HashMap JavaDoc();
905             vals.put("dflt", attrs.getValue("dflt"));
906             vals.put("labels", new ArrayList JavaDoc());
907             vals.put("keys", new ArrayList JavaDoc());
908             push(vals);
909         }
910
911         public final void end(String JavaDoc name) {
912             Map JavaDoc vals = (Map JavaDoc) pop();
913             Label dflt = getLabel(vals.get("dflt"));
914             List JavaDoc keyList = (List JavaDoc) vals.get("keys");
915             Label[] lbls = (Label[]) ((List JavaDoc) vals.get("labels")).toArray(new Label[0]);
916             int[] keys = new int[keyList.size()];
917             for (int i = 0; i < keys.length; i++) {
918                 keys[i] = Integer.parseInt((String JavaDoc) keyList.get(i));
919             }
920             getCodeVisitor().visitLookupSwitchInsn(dflt, keys, lbls);
921         }
922
923     }
924
925     /**
926      * LookupSwitchLabelRule
927      */

928     private final class LookupSwitchLabelRule extends Rule {
929
930         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
931             Map JavaDoc vals = (Map JavaDoc) peek();
932             ((List JavaDoc) vals.get("labels")).add(getLabel(attrs.getValue("name")));
933             ((List JavaDoc) vals.get("keys")).add(attrs.getValue("key"));
934         }
935
936     }
937
938     /**
939      * LabelRule
940      */

941     private final class LabelRule extends Rule {
942
943         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
944             getCodeVisitor().visitLabel(getLabel(attrs.getValue("name")));
945         }
946
947     }
948
949     /**
950      * TryCatchRule
951      */

952     private final class TryCatchRule extends Rule {
953
954         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
955             Label start = getLabel(attrs.getValue("start"));
956             Label end = getLabel(attrs.getValue("end"));
957             Label handler = getLabel(attrs.getValue("handler"));
958             String JavaDoc type = attrs.getValue("type");
959             getCodeVisitor().visitTryCatchBlock(start, end, handler, type);
960         }
961
962     }
963
964     /**
965      * LineNumberRule
966      */

967     private final class LineNumberRule extends Rule {
968
969         public final void begin(String JavaDoc name, Attributes JavaDoc attrs) {
970             int line = Integer.parseInt(attrs.getValue("line"));
971             Label start = getLabel(attrs.getValue("start"));
972             getCodeVisitor().visitLineNumber(line, start);
973         }
974
975     }
976
977     /**
978      * LocalVarRule
979      */

980     private final class LocalVarRule extends Rule {
981
982         public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
983             String JavaDoc name = attrs.getValue("name");
984             String JavaDoc desc = attrs.getValue("desc");
985             String JavaDoc signature = attrs.getValue("signature");
986             Label start = getLabel(attrs.getValue("start"));
987             Label end = getLabel(attrs.getValue("end"));
988             int var = Integer.parseInt(attrs.getValue("var"));
989             getCodeVisitor().visitLocalVariable(name,
990                     desc,
991                     signature,
992                     start,
993                     end,
994                     var);
995         }
996
997     }
998
999     /**
1000     * OpcodesRule
1001     */

1002    private final class OpcodesRule extends Rule {
1003
1004        // public boolean match( String match, String element) {
1005
// return match.startsWith( path) && OPCODES.containsKey( element);
1006
// }
1007

1008        public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
1009            Opcode o = ((Opcode) OPCODES.get(element));
1010            if (o == null)
1011                return;
1012
1013            switch (o.type) {
1014                case OpcodeGroup.INSN:
1015                    getCodeVisitor().visitInsn(o.opcode);
1016                    break;
1017
1018                case OpcodeGroup.INSN_FIELD:
1019                    getCodeVisitor().visitFieldInsn(o.opcode,
1020                            attrs.getValue("owner"),
1021                            attrs.getValue("name"),
1022                            attrs.getValue("desc"));
1023                    break;
1024
1025                case OpcodeGroup.INSN_INT:
1026                    getCodeVisitor().visitIntInsn(o.opcode,
1027                            Integer.parseInt(attrs.getValue("value")));
1028                    break;
1029
1030                case OpcodeGroup.INSN_JUMP:
1031                    getCodeVisitor().visitJumpInsn(o.opcode,
1032                            getLabel(attrs.getValue("label")));
1033                    break;
1034
1035                case OpcodeGroup.INSN_METHOD:
1036                    getCodeVisitor().visitMethodInsn(o.opcode,
1037                            attrs.getValue("owner"),
1038                            attrs.getValue("name"),
1039                            attrs.getValue("desc"));
1040                    break;
1041
1042                case OpcodeGroup.INSN_TYPE:
1043                    getCodeVisitor().visitTypeInsn(o.opcode,
1044                            attrs.getValue("desc"));
1045                    break;
1046
1047                case OpcodeGroup.INSN_VAR:
1048                    getCodeVisitor().visitVarInsn(o.opcode,
1049                            Integer.parseInt(attrs.getValue("var")));
1050                    break;
1051
1052                case OpcodeGroup.INSN_IINC:
1053                    getCodeVisitor().visitIincInsn(Integer.parseInt(attrs.getValue("var")),
1054                            Integer.parseInt(attrs.getValue("inc")));
1055                    break;
1056
1057                case OpcodeGroup.INSN_LDC:
1058                    getCodeVisitor().visitLdcInsn(getValue(attrs.getValue("desc"),
1059                            attrs.getValue("cst")));
1060                    break;
1061
1062                case OpcodeGroup.INSN_MULTIANEWARRAY:
1063                    getCodeVisitor().visitMultiANewArrayInsn(attrs.getValue("desc"),
1064                            Integer.parseInt(attrs.getValue("dims")));
1065                    break;
1066
1067                default:
1068                    throw new RuntimeException JavaDoc("Invalid element: " + element
1069                            + " at " + match);
1070
1071            }
1072        }
1073    }
1074
1075    /**
1076     * MaxRule
1077     */

1078    private final class MaxRule extends Rule {
1079
1080        public final void begin(String JavaDoc element, Attributes JavaDoc attrs) {
1081            int maxStack = Integer.parseInt(attrs.getValue("maxStack"));
1082            int maxLocals = Integer.parseInt(attrs.getValue("maxLocals"));
1083            getCodeVisitor().visitMaxs(maxStack, maxLocals);
1084        }
1085
1086    }
1087
1088    private final class AnnotationRule extends Rule {
1089
1090        public void begin(String JavaDoc name, Attributes JavaDoc attrs) {
1091            String JavaDoc desc = attrs.getValue("desc");
1092            boolean visible = Boolean.valueOf(attrs.getValue("visible"))
1093                    .booleanValue();
1094
1095            Object JavaDoc v = peek();
1096            if (v instanceof ClassVisitor) {
1097                push(((ClassVisitor) v).visitAnnotation(desc, visible));
1098            } else if (v instanceof FieldVisitor) {
1099                push(((FieldVisitor) v).visitAnnotation(desc, visible));
1100            } else if (v instanceof MethodVisitor) {
1101                push(((MethodVisitor) v).visitAnnotation(desc, visible));
1102            }
1103        }
1104
1105        public void end(String JavaDoc name) {
1106            ((AnnotationVisitor) pop()).visitEnd();
1107        }
1108
1109    }
1110
1111    private final class AnnotationParameterRule extends Rule {
1112
1113        public void begin(String JavaDoc name, Attributes JavaDoc attrs) {
1114            int parameter = Integer.parseInt(attrs.getValue("parameter"));
1115            String JavaDoc desc = attrs.getValue("desc");
1116            boolean visible = Boolean.valueOf(attrs.getValue("visible"))
1117                    .booleanValue();
1118
1119            push(((MethodVisitor) peek()).visitParameterAnnotation(parameter,
1120                    desc,
1121                    visible));
1122        }
1123
1124        public void end(String JavaDoc name) {
1125            ((AnnotationVisitor) pop()).visitEnd();
1126        }
1127
1128    }
1129
1130    private final class AnnotationValueRule extends Rule {
1131
1132        public void begin(String JavaDoc nm, Attributes JavaDoc attrs) {
1133            String JavaDoc name = attrs.getValue("name");
1134            String JavaDoc desc = attrs.getValue("desc");
1135            String JavaDoc value = attrs.getValue("value");
1136            ((AnnotationVisitor) peek()).visit(name, getValue(desc, value));
1137        }
1138
1139    }
1140
1141    private final class AnnotationValueEnumRule extends Rule {
1142
1143        public void begin(String JavaDoc nm, Attributes JavaDoc attrs) {
1144            String JavaDoc name = attrs.getValue("name");
1145            String JavaDoc desc = attrs.getValue("desc");
1146            String JavaDoc value = attrs.getValue("value");
1147            ((AnnotationVisitor) peek()).visitEnum(name, desc, value);
1148        }
1149
1150    }
1151
1152    private final class AnnotationValueAnnotationRule extends Rule {
1153
1154        public void begin(String JavaDoc nm, Attributes JavaDoc attrs) {
1155            String JavaDoc name = attrs.getValue("name");
1156            String JavaDoc desc = attrs.getValue("desc");
1157            push(((AnnotationVisitor) peek()).visitAnnotation(name, desc));
1158        }
1159
1160        public void end(String JavaDoc name) {
1161            ((AnnotationVisitor) pop()).visitEnd();
1162        }
1163
1164    }
1165
1166    private final class AnnotationValueArrayRule extends Rule {
1167
1168        public void begin(String JavaDoc nm, Attributes JavaDoc attrs) {
1169            String JavaDoc name = attrs.getValue("name");
1170            push(((AnnotationVisitor) peek()).visitArray(name));
1171        }
1172
1173        public void end(String JavaDoc name) {
1174            ((AnnotationVisitor) pop()).visitEnd();
1175        }
1176
1177    }
1178
1179    private final class AnnotationDefaultRule extends Rule {
1180
1181        public void begin(String JavaDoc nm, Attributes JavaDoc attrs) {
1182            push(((MethodVisitor) peek()).visitAnnotationDefault());
1183        }
1184
1185        public void end(String JavaDoc name) {
1186            ((AnnotationVisitor) pop()).visitEnd();
1187        }
1188
1189    }
1190
1191    /**
1192     * Opcode
1193     */

1194    private final static class Opcode {
1195        public int opcode;
1196
1197        public int type;
1198
1199        public Opcode(int opcode, int type) {
1200            this.opcode = opcode;
1201            this.type = type;
1202        }
1203
1204    }
1205
1206}
1207
Popular Tags