001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2007 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.commons;
030:
031: import java.io.FileInputStream;
032: import java.io.PrintWriter;
033:
034: import org.objectweb.asm.ClassReader;
035: import org.objectweb.asm.MethodVisitor;
036: import org.objectweb.asm.util.ASMifierClassVisitor;
037:
038: /**
039: * Sub class of the ASMifier class visitor used to test GeneratorAdapter.
040: *
041: * @author Eric Bruneton
042: */
043: public class GASMifierClassVisitor extends ASMifierClassVisitor {
044:
045: /**
046: * Prints the ASM source code to generate the given class to the standard
047: * output. <p> Usage: ASMifierClassVisitor [-debug] <fully qualified
048: * class name or class file name>
049: *
050: * @param args the command line arguments.
051: *
052: * @throws Exception if the class cannot be found, or if an IO exception
053: * occurs.
054: */
055: public static void main(final String[] args) throws Exception {
056: int i = 0;
057: int flags = ClassReader.SKIP_DEBUG;
058:
059: boolean ok = true;
060: if (args.length < 1 || args.length > 2) {
061: ok = false;
062: }
063: if (ok && args[0].equals("-debug")) {
064: i = 1;
065: flags = 0;
066: if (args.length != 2) {
067: ok = false;
068: }
069: }
070: if (!ok) {
071: System.err
072: .println("Prints the ASM code to generate the given class.");
073: System.err
074: .println("Usage: GASMifierClassVisitor [-debug] "
075: + "<fully qualified class name or class file name>");
076: System.exit(-1);
077: }
078: ClassReader cr;
079: if (args[i].endsWith(".class")) {
080: cr = new ClassReader(new FileInputStream(args[i]));
081: } else {
082: cr = new ClassReader(args[i]);
083: }
084: cr.accept(
085: new GASMifierClassVisitor(new PrintWriter(System.out)),
086: getDefaultAttributes(), ClassReader.EXPAND_FRAMES
087: | flags);
088: }
089:
090: public GASMifierClassVisitor(final PrintWriter pw) {
091: super (pw);
092: }
093:
094: public void visit(final int version, final int access,
095: final String name, final String signature,
096: final String super Name, final String[] interfaces) {
097: super .visit(version, access, name, signature, super Name,
098: interfaces);
099: int n;
100: if (name.lastIndexOf('/') != -1) {
101: n = 1;
102: } else {
103: n = 0;
104: }
105: text
106: .set(n + 5,
107: "ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);\n");
108: text.set(n + 7, "GeneratorAdapter mg;\n");
109: text.add(n + 1, "import org.objectweb.asm.commons.*;\n");
110: }
111:
112: public MethodVisitor visitMethod(final int access,
113: final String name, final String desc,
114: final String signature, final String[] exceptions) {
115: buf.setLength(0);
116: buf.append("{\n");
117: buf.append("mg = new GeneratorAdapter(");
118: buf.append(access);
119: buf.append(", ");
120: buf.append(GASMifierMethodVisitor.getMethod(name, desc));
121: buf.append(", ");
122: if (signature == null) {
123: buf.append("null");
124: } else {
125: buf.append('"').append(signature).append('"');
126: }
127: buf.append(", ");
128: if (exceptions != null && exceptions.length > 0) {
129: buf.append("new Type[] {");
130: for (int i = 0; i < exceptions.length; ++i) {
131: buf.append(i == 0 ? " " : ", ");
132: buf.append(GASMifierMethodVisitor
133: .getType(exceptions[i]));
134: }
135: buf.append(" }");
136: } else {
137: buf.append("null");
138: }
139: buf.append(", cw);\n");
140: text.add(buf.toString());
141: GASMifierMethodVisitor acv = new GASMifierMethodVisitor(access,
142: desc);
143: text.add(acv.getText());
144: text.add("}\n");
145: return new LocalVariablesSorter(access, desc, acv);
146: }
147: }
|