KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > cmd > makesubclass


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" and DARPA/AFRL contract
7  * F30602-00-C-0183 "Survivable Server" as part of the
8  * DARPA OASIS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */

31
32 /* Writes a source file defining all non-final,non-static methods of a binary class. */
33
34 package jbet.cmd;
35 import jbet.*;
36 import java.io.*;
37
38 public class makesubclass implements Command
39 {
40     public int helplevel() { return 3; }
41     public String JavaDoc shorthelp() { return "make subclass skeleton"; }
42     public String JavaDoc longhelp() { return "jbet makesubclass <class> <outname>\n"; }
43
44     public void run (Lexer lexer) throws Exception JavaDoc
45     {
46     boolean callsuper = true;
47     String JavaDoc precode = null;
48
49     loop:
50     while(true) {
51         switch(lexer.read().type) {
52         case 'p': lexer.push (Lexer.ST_STRING); precode = lexer.parse_string(); lexer.pop(); break;
53         case 'r': lexer.push (Lexer.ST_STRING); precode = lexer.parse_string(); lexer.pop(); callsuper = false; break;
54         case Token.END_OF_OPTS: break loop;
55         default: System.out.println ("!!!!"); lexer.unexpected(lexer.justread());
56         }
57     }
58
59     ClassInfo cr = lexer.getClass(null);
60     lexer.push (Lexer.ST_STRING);
61     String JavaDoc out = lexer.parse_string();
62     lexer.pop();
63     lexer.term();
64
65     FileOutputStream outf = new FileOutputStream (out + ".java");
66     PrintStream outp = new PrintStream (outf);
67
68     if (out.indexOf ('/') != -1)
69         outp.println ("package " + out.substring (0, out.lastIndexOf ('/')).replace ('/','.') + ";\n");
70     outp.println ("class " + out.substring (1 + out.lastIndexOf ('/')) + " extends " + Util.srcClassName (cr.name()));
71     outp.println ("{\n");
72
73     for (int i = 0; i < cr.numMethods(); i++) {
74         MethodInfo mi = cr.methodAt (i);
75
76         if (!mi.name.equals ("<init>") &&
77         (mi.accessFlags & MethodInfo.ACC_STATIC) == 0 &&
78         (mi.accessFlags & MethodInfo.ACC_FINAL) == 0 &&
79         (mi.accessFlags & MethodInfo.ACC_PRIVATE) == 0 &&
80         ((mi.accessFlags & MethodInfo.ACC_PROTECTED) != 0 ||
81          (mi.accessFlags & MethodInfo.ACC_PUBLIC) != 0)) {
82
83         outp.println (" " + Util.flags2str (mi.accessFlags, false) + " " + mi.descriptor.ret.declaration() + " " + mi.name + " " +
84                   mi.descriptor.declaration() + " {");
85         if (precode != null)
86             outp.println (" " + precode + ";");
87         if (callsuper) {
88             StringBuffer JavaDoc b = new StringBuffer JavaDoc(mi.descriptor.ret.base == 'V' ? " super." : " return super.");
89             b.append (mi.name);
90             b.append (" (");
91             for (int j = 0; j < mi.descriptor.args.length; j++) {
92             b.append ("arg");
93             b.append (j);
94             if (j != mi.descriptor.args.length - 1)
95                 b.append (", ");
96             }
97
98             b.append (");");
99             outp.println (b);
100         }
101         outp.println (" }\n");
102         }
103     }
104
105     outp.println ("}");
106     }
107 }
108
Popular Tags