6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
package com.sun.org.apache.bcel.internal.util;
|
|
6 |
|
|
7 |
/* ====================================================================
|
|
8 |
* The Apache Software License, Version 1.1
|
|
9 |
*
|
|
10 |
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
|
11 |
* reserved.
|
|
12 |
*
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
14 |
* modification, are permitted provided that the following conditions
|
|
15 |
* are met:
|
|
16 |
*
|
|
17 |
* 1. Redistributions of source code must retain the above copyright
|
|
18 |
* notice, this list of conditions and the following disclaimer.
|
|
19 |
*
|
|
20 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
21 |
* notice, this list of conditions and the following disclaimer in
|
|
22 |
* the documentation and/or other materials provided with the
|
|
23 |
* distribution.
|
|
24 |
*
|
|
25 |
* 3. The end-user documentation included with the redistribution,
|
|
26 |
* if any, must include the following acknowledgment:
|
|
27 |
* "This product includes software developed by the
|
|
28 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
29 |
* Alternately, this acknowledgment may appear in the software itself,
|
|
30 |
* if and wherever such third-party acknowledgments normally appear.
|
|
31 |
*
|
|
32 |
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
33 |
* "Apache BCEL" must not be used to endorse or promote products
|
|
34 |
* derived from this software without prior written permission. For
|
|
35 |
* written permission, please contact apache@apache.org.
|
|
36 |
*
|
|
37 |
* 5. Products derived from this software may not be called "Apache",
|
|
38 |
* "Apache BCEL", nor may "Apache" appear in their name, without
|
|
39 |
* prior written permission of the Apache Software Foundation.
|
|
40 |
*
|
|
41 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
42 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
43 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
44 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
45 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
46 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
47 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
48 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
49 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
50 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
51 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
52 |
* SUCH DAMAGE.
|
|
53 |
* ====================================================================
|
|
54 |
*
|
|
55 |
* This software consists of voluntary contributions made by many
|
|
56 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
57 |
* information on the Apache Software Foundation, please see
|
|
58 |
* <http://www.apache.org/>.
|
|
59 |
*/
|
|
60 |
import com.sun.org.apache.bcel.internal.classfile.*;
|
|
61 |
import com.sun.org.apache.bcel.internal.generic.*;
|
|
62 |
import com.sun.org.apache.bcel.internal.Repository;
|
|
63 |
import com.sun.org.apache.bcel.internal.Constants;
|
|
64 |
import java.io.*;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* This class takes a given JavaClass object and converts it to a
|
|
68 |
* Java program that creates that very class using BCEL. This
|
|
69 |
* gives new users of BCEL a useful example showing how things
|
|
70 |
* are done with BCEL. It does not cover all features of BCEL,
|
|
71 |
* but tries to mimic hand-written code as close as possible.
|
|
72 |
*
|
|
73 |
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
|
|
74 |
*/
|
|
75 |
public class BCELifier extends com.sun.org.apache.bcel.internal.classfile.EmptyVisitor {
|
|
76 |
private JavaClass _clazz;
|
|
77 |
private PrintWriter _out;
|
|
78 |
private ConstantPoolGen _cp;
|
|
79 |
|
|
80 |
/** @param clazz Java class to "decompile"
|
|
81 |
* @param out where to output Java program
|
|
82 |
*/
|
|
83 |
public BCELifier(JavaClass clazz, OutputStream out) {
|
|
84 |
_clazz = clazz;
|
|
85 |
_out = new PrintWriter(out);
|
|
86 |
_cp = new ConstantPoolGen(_clazz.getConstantPool());
|
|
87 |
}
|
|
88 |
|
|
89 |
/** Start Java code generation
|
|
90 |
*/
|
|
91 |
public void start() {
|
|
92 |
visitJavaClass(_clazz);
|
|
93 |
_out.flush();
|
|
94 |
}
|
|
95 |
|
|
96 |
public void visitJavaClass(JavaClass clazz) {
|
|
97 |
String class_name = clazz.getClassName();
|
|
98 |
String super_name = clazz.getSuperclassName();
|
|
99 |
String package_name = clazz.getPackageName();
|
|
100 |
String inter = Utility.printArray(clazz.getInterfaceNames(),
|
|
101 |
false, true);
|
|
102 |
if(!"".equals(package_name)) {
|
|
103 |
class_name = class_name.substring(package_name.length() + 1);
|
|
104 |
_out.println("package " + package_name + ";\n");
|
|
105 |
}
|
|
106 |
|
|
107 |
_out.println("import com.sun.org.apache.bcel.internal.generic.*;");
|
|
108 |
_out.println("import com.sun.org.apache.bcel.internal.classfile.*;");
|
|
109 |
_out.println("import com.sun.org.apache.bcel.internal.*;");
|
|
110 |
_out.println("import java.io.*;\n");
|
|
111 |
|
|
112 |
_out.println("public class " + class_name + "Creator implements Constants {");
|
|
113 |
_out.println(" private InstructionFactory _factory;");
|
|
114 |
_out.println(" private ConstantPoolGen _cp;");
|
|
115 |
_out.println(" private ClassGen _cg;\n");
|
|
116 |
|
|
117 |
_out.println(" public " + class_name + "Creator() {");
|
|
118 |
_out.println(" _cg = new ClassGen(\"" +
|
|
119 |
(("".equals(package_name))? class_name :
|
|
120 |
package_name + "." + class_name) +
|
|
121 |
"\", \"" + super_name + "\", " +
|
|
122 |
"\"" + clazz.getSourceFileName() + "\", " +
|
|
123 |
printFlags(clazz.getAccessFlags(), true) + ", " +
|
|
124 |
"new String[] { " + inter + " });\n");
|
|
125 |
|
|
126 |
_out.println(" _cp = _cg.getConstantPool();");
|
|
127 |
_out.println(" _factory = new InstructionFactory(_cg, _cp);");
|
|
128 |
_out.println(" }\n");
|
|
129 |
|
|
130 |
printCreate();
|
|
131 |
|
|
132 |
Field[] fields = clazz.getFields();
|
|
133 |
|
|
134 |
if(fields.length > 0) {
|
|
135 |
_out.println(" private void createFields() {");
|
|
136 |
_out.println(" FieldGen field;");
|
|
137 |
|
|
138 |
for(int i=0; i < fields.length; i++) {
|
|
139 |
fields[i].accept(this);
|
|
140 |
}
|
|
141 |
|
|
142 |
_out.println(" }\n");
|
|
143 |
}
|
|
144 |
|
|
145 |
Method[] methods = clazz.getMethods();
|
|
146 |
|
|
147 |
for(int i=0; i < methods.length; i++) {
|
|
148 |
_out.println(" private void createMethod_" + i + "() {");
|
|
149 |
|
|
150 |
methods[i].accept(this);
|
|
151 |
_out.println(" }\n");
|
|
152 |
}
|
|
153 |
|
|
154 |
printMain();
|
|
155 |
_out.println("}");
|
|
156 |
}
|
|
157 |
|
|
158 |
private void printCreate() {
|
|
159 |
_out.println(" public void create(OutputStream out) throws IOException {");
|
|
160 |
|
|
161 |
Field[] fields = _clazz.getFields();
|
|
162 |
if(fields.length > 0) {
|
|
163 |
_out.println(" createFields();");
|
|
164 |
}
|
|
165 |
|
|
166 |
Method[] methods = _clazz.getMethods();
|
|
167 |
for(int i=0; i < methods.length; i++) {
|
|
168 |
_out.println(" createMethod_" + i + "();");
|
|
169 |
}
|
|
170 |
|
|
171 |
_out.println(" _cg.getJavaClass().dump(out);");
|
|
172 |
|
|
173 |
_out.println(" }\n");
|
|
174 |
}
|
|
175 |
|
|
176 |
private void printMain() {
|
|
177 |
String class_name = _clazz.getClassName();
|
|
178 |
|
|
179 |
_out.println(" public static void _main(String[] args) throws Exception {");
|
|
180 |
_out.println(" " + class_name + "Creator creator = new " +
|
|
181 |
class_name + "Creator();");
|
|
182 |
_out.println(" creator.create(new FileOutputStream(\"" + class_name +
|
|
183 |
".class\"));");
|
|
184 |
_out.println(" }");
|
|
185 |
}
|
|
186 |
|
|
187 |
public void visitField(Field field) {
|
|
188 |
_out.println("\n field = new FieldGen(" +
|
|
189 |
printFlags(field.getAccessFlags()) +
|
|
190 |
", " + printType(field.getSignature()) + ", \"" +
|
|
191 |
field.getName() + "\", _cp);");
|
|
192 |
|
|
193 |
ConstantValue cv = field.getConstantValue();
|
|
194 |
|
|
195 |
if(cv != null) {
|
|
196 |
String value = cv.toString();
|
|
197 |
_out.println(" field.setInitValue(" + value + ")");
|
|
198 |
}
|
|
199 |
|
|
200 |
_out.println(" _cg.addField(field.getField());");
|
|
201 |
}
|
|
202 |
|
|
203 |
public void visitMethod(Method method) {
|
|
204 |
MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
|
|
205 |
|
|
206 |
Type result_type = mg.getReturnType();
|
|
207 |
Type[] arg_types = mg.getArgumentTypes();
|
|
208 |
|
|
209 |
_out.println(" InstructionList il = new InstructionList();");
|
|
210 |
_out.println(" MethodGen method = new MethodGen(" +
|
|
211 |
printFlags(method.getAccessFlags()) +
|
|
212 |
", " + printType(result_type) +
|
|
213 |
", " + printArgumentTypes(arg_types) + ", " +
|
|
214 |
"new String[] { " +
|
|
215 |
Utility.printArray(mg.getArgumentNames(), false, true) +
|
|
216 |
" }, \"" + method.getName() + "\", \"" +
|
|
217 |
_clazz.getClassName() + "\", il, _cp);\n");
|
|
218 |
|
|
219 |
BCELFactory factory = new BCELFactory(mg, _out);
|
|
220 |
factory.start();
|
|
221 |
|
|
222 |
_out.println(" method.setMaxStack();");
|
|
223 |
_out.println(" method.setMaxLocals();");
|
|
224 |
_out.println(" _cg.addMethod(method.getMethod());");
|
|
225 |
_out.println(" il.dispose();");
|
|
226 |
}
|
|
227 |
|
|
228 |
static String printFlags(int flags) {
|
|
229 |
return printFlags(flags, false);
|
|
230 |
}
|
|
231 |
|
|
232 |
static String printFlags(int flags, boolean for_class) {
|
|
233 |
if(flags == 0)
|
|
234 |
return "0";
|
|
235 |
|
|
236 |
StringBuffer buf = new StringBuffer();
|
|
237 |
for(int i=0, pow=1; i <= Constants.MAX_ACC_FLAG; i++) {
|
|
238 |
if((flags & pow) != 0) {
|
|
239 |
if((pow == Constants.ACC_SYNCHRONIZED) && for_class)
|
|
240 |
buf.append("ACC_SUPER | ");
|
|
241 |
else
|
|
242 |
buf.append("ACC_" + Constants.ACCESS_NAMES[i].toUpperCase() + " | ");
|
|
243 |
}
|
|
244 |
|
|
245 |
pow <<= 1;
|
|
246 |
}
|
|
247 |
|
|
248 |
String str = buf.toString();
|
|
249 |
return str.substring(0, str.length() - 3);
|
|
250 |
}
|
|
251 |
|
|
252 |
static String printArgumentTypes(Type[] arg_types) {
|
|
253 |
if(arg_types.length == 0)
|
|
254 |
return "Type.NO_ARGS";
|
|
255 |
|
|
256 |
StringBuffer args = new StringBuffer();
|
|
257 |
|
|
258 |
for(int i=0; i < arg_types.length; i++) {
|
|
259 |
args.append(printType(arg_types[i]));
|
|
260 |
|
|
261 |
if(i < arg_types.length - 1)
|
|
262 |
args.append(", ");
|
|
263 |
}
|
|
264 |
|
|
265 |
return "new Type[] { " + args.toString() + " }";
|
|
266 |
}
|
|
267 |
|
|
268 |
static String printType(Type type) {
|
|
269 |
return printType(type.getSignature());
|
|
270 |
}
|
|
271 |
|
|
272 |
static String printType(String signature) {
|
|
273 |
Type type = Type.getType(signature);
|
|
274 |
byte t = type.getType();
|
|
275 |
|
|
276 |
if(t <= Constants.T_VOID) {
|
|
277 |
return "Type." + Constants.TYPE_NAMES[t].toUpperCase();
|
|
278 |
} else if(type.toString().equals("java.lang.String")) {
|
|
279 |
return "Type.STRING";
|
|
280 |
} else if(type.toString().equals("java.lang.Object")) {
|
|
281 |
return "Type.OBJECT";
|
|
282 |
} else if(type.toString().equals("java.lang.StringBuffer")) {
|
|
283 |
return "Type.STRINGBUFFER";
|
|
284 |
} else if(type instanceof ArrayType) {
|
|
285 |
ArrayType at = (ArrayType)type;
|
|
286 |
|
|
287 |
return "new ArrayType(" + printType(at.getBasicType()) +
|
|
288 |
", " + at.getDimensions() + ")";
|
|
289 |
} else {
|
|
290 |
return "new ObjectType(\"" + Utility.signatureToString(signature, false) +
|
|
291 |
"\")";
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
/** Default _main method
|
|
296 |
*/
|
|
297 |
public static void _main(String[] argv) throws Exception {
|
|
298 |
JavaClass java_class;
|
|
299 |
String name = argv[0];
|
|
300 |
|
|
301 |
if((java_class = Repository.lookupClass(name)) == null)
|
|
302 |
java_class = new ClassParser(name).parse(); // May throw IOException
|
|
303 |
|
|
304 |
BCELifier bcelifier = new BCELifier(java_class, System.out);
|
|
305 |
bcelifier.start();
|
|
306 |
}
|
|
307 |
}
|