KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > commons > GASMifierClassVisitor


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
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.commons;
31
32 import java.io.FileInputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34
35 import org.objectweb.asm.ClassReader;
36 import org.objectweb.asm.MethodVisitor;
37 import org.objectweb.asm.util.ASMifierClassVisitor;
38
39 public class GASMifierClassVisitor extends ASMifierClassVisitor {
40
41     /**
42      * Prints the ASM source code to generate the given class to the standard
43      * output. <p> Usage: ASMifierClassVisitor [-debug] &lt;fully qualified
44      * class name or class file name&gt;
45      *
46      * @param args the command line arguments.
47      *
48      * @throws Exception if the class cannot be found, or if an IO exception
49      * occurs.
50      */

51     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
52         int i = 0;
53         boolean skipDebug = true;
54
55         boolean ok = true;
56         if (args.length < 1 || args.length > 2) {
57             ok = false;
58         }
59         if (ok && args[0].equals("-debug")) {
60             i = 1;
61             skipDebug = false;
62             if (args.length != 2) {
63                 ok = false;
64             }
65         }
66         if (!ok) {
67             System.err.println("Prints the ASM code to generate the given class.");
68             System.err.println("Usage: GASMifierClassVisitor [-debug] "
69                     + "<fully qualified class name or class file name>");
70             System.exit(-1);
71         }
72         ClassReader cr;
73         if (args[i].endsWith(".class")) {
74             cr = new ClassReader(new FileInputStream JavaDoc(args[i]));
75         } else {
76             cr = new ClassReader(args[i]);
77         }
78         cr.accept(new GASMifierClassVisitor(new PrintWriter JavaDoc(System.out)),
79                 getDefaultAttributes(),
80                 skipDebug);
81     }
82
83     public GASMifierClassVisitor(PrintWriter JavaDoc pw) {
84         super(pw);
85     }
86
87     public void visit(
88         final int version,
89         final int access,
90         final String JavaDoc name,
91         final String JavaDoc signature,
92         final String JavaDoc superName,
93         final String JavaDoc[] interfaces)
94     {
95         super.visit(version, access, name, signature, superName, interfaces);
96         int n;
97         if (name.lastIndexOf('/') != -1) {
98             n = 1;
99         } else {
100             n = 0;
101         }
102         text.set(n + 5, "ClassWriter cw = new ClassWriter(true);\n");
103         text.set(n + 7, "GeneratorAdapter mg;\n");
104         text.add(n + 1, "import org.objectweb.asm.commons.*;\n");
105     }
106
107     public MethodVisitor visitMethod(
108         final int access,
109         final String JavaDoc name,
110         final String JavaDoc desc,
111         final String JavaDoc signature,
112         final String JavaDoc[] exceptions)
113     {
114         buf.setLength(0);
115         buf.append("{\n");
116         buf.append("mg = new GeneratorAdapter(");
117         buf.append(access);
118         buf.append(", ");
119         buf.append(GASMifierMethodVisitor.getMethod(name, desc));
120         buf.append(", ");
121         if (signature == null) {
122             buf.append("null");
123         } else {
124             buf.append('"').append(signature).append('"');
125         }
126         buf.append(", ");
127         if (exceptions != null && exceptions.length > 0) {
128             buf.append("new Type[] {");
129             for (int i = 0; i < exceptions.length; ++i) {
130                 buf.append(i == 0 ? " " : ", ");
131                 buf.append(GASMifierMethodVisitor.getType(exceptions[i]));
132             }
133             buf.append(" }");
134         } else {
135             buf.append("null");
136         }
137         buf.append(", cw);\n");
138         text.add(buf.toString());
139         GASMifierMethodVisitor acv = new GASMifierMethodVisitor(access, desc);
140         text.add(acv.getText());
141         text.add("}\n");
142         return new LocalVariablesSorter(access, desc, acv);
143     }
144 }
145
Popular Tags