forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDavaPrinter.java
More file actions
426 lines (377 loc) · 14.1 KB
/
DavaPrinter.java
File metadata and controls
426 lines (377 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package soot.dava;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2003 Jerome Miecznikowski
* Copyright (C) 2004 - 2005 Nomair A. Naeem
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.Body;
import soot.BooleanType;
import soot.ByteType;
import soot.CharType;
import soot.DoubleType;
import soot.FloatType;
import soot.G;
import soot.IntType;
import soot.LongType;
import soot.Modifier;
import soot.RefType;
import soot.Scene;
import soot.ShortType;
import soot.Singletons;
import soot.SootClass;
import soot.SootField;
import soot.SootMethod;
import soot.Type;
import soot.Unit;
import soot.UnitPrinter;
import soot.dava.internal.AST.ASTNode;
import soot.dava.toolkits.base.renamer.RemoveFullyQualifiedName;
import soot.options.Options;
import soot.tagkit.DoubleConstantValueTag;
import soot.tagkit.FloatConstantValueTag;
import soot.tagkit.IntegerConstantValueTag;
import soot.tagkit.LongConstantValueTag;
import soot.tagkit.StringConstantValueTag;
import soot.tagkit.Tag;
import soot.util.Chain;
import soot.util.IterableSet;
public class DavaPrinter {
public DavaPrinter(Singletons.Global g) {
}
public static DavaPrinter v() {
return G.v().soot_dava_DavaPrinter();
}
private void printStatementsInBody(Body body, java.io.PrintWriter out) {
if (Options.v().verbose()) {
System.out.println("Printing " + body.getMethod().getName());
}
Chain<Unit> units = body.getUnits();
if (units.size() != 1) {
throw new RuntimeException("DavaBody AST doesn't have single root.");
}
UnitPrinter up = new DavaUnitPrinter((DavaBody) body);
((ASTNode) units.getFirst()).toString(up);
out.print(up.toString());
}
public void printTo(SootClass cl, PrintWriter out) {
// IterableSet<String> packagesUsed = new IterableSet<String>();
IterableSet<String> importList = new IterableSet<String>();
{
final String curPackage = cl.getJavaPackageName();
if (!curPackage.isEmpty()) {
out.println("package " + curPackage + ';');
out.println();
}
if (cl.hasSuperclass()) {
SootClass superClass = cl.getSuperclass();
importList.add(superClass.toString());
// packagesUsed.add(superClass.getJavaPackageName());
}
for (SootClass sc : cl.getInterfaces()) {
String interfacePackage = sc.toString();
if (!importList.contains(interfacePackage)) {
importList.add(interfacePackage);
}
// if (!packagesUsed.contains(interfacePackage))
// packagesUsed.add(interfacePackage);
}
for (Iterator<SootMethod> methodIt = cl.methodIterator(); methodIt.hasNext();) {
SootMethod dm = methodIt.next();
if (dm.hasActiveBody()) {
// packagesUsed = packagesUsed.union(((DavaBody) dm.getActiveBody()).get_PackagesUsed());
importList = importList.union(((DavaBody) dm.getActiveBody()).getImportList());
}
for (SootClass sc : dm.getExceptions()) {
String thrownPackage = sc.toString();
if (!importList.contains(thrownPackage)) {
importList.add(thrownPackage);
}
// if (!packagesUsed.contains(thrownPackage))
// packagesUsed.add(thrownPackage);
}
for (Type t : dm.getParameterTypes()) {
if (t instanceof RefType) {
String paramPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(paramPackage)) {
importList.add(paramPackage);
}
// if (packagesUsed.contains(paramPackage) == false)
// packagesUsed.add(paramPackage);
}
}
Type t = dm.getReturnType();
if (t instanceof RefType) {
String returnPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(returnPackage)) {
importList.add(returnPackage);
}
// if (packagesUsed.contains(returnPackage) == false)
// packagesUsed.add(returnPackage);
}
}
for (SootField f : cl.getFields()) {
if (!f.isPhantom()) {
Type t = f.getType();
if (t instanceof RefType) {
String fieldPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(fieldPackage)) {
importList.add(fieldPackage);
}
}
}
}
List<String> toImport = new ArrayList<String>();
for (String temp : importList) {
/*
* dont import any file which has currentPackage.className dont import any file which starts with java.lang
*/
// System.out.println("temp is "+temp);
if (temp.contains("java.lang")) {
// problem is that we need to import sub packages java.lang.ref
// for instance if the type is java.lang.ref.WeakReference
String tempClassName = RemoveFullyQualifiedName.getClassName(temp);
if (temp.equals("java.lang." + tempClassName)) {
// System.out.println("temp was not printed as it belongs to java.lang");
continue;
}
}
if ((!curPackage.isEmpty() && temp.contains(curPackage)) || cl.toString().equals(temp)) {
continue;
}
// System.out.println("printing"+);
toImport.add(temp);
}
/*
* Check that we are not importing two classes with the same last name If yes then remove explicit import and import
* the whole package else output explicit import statement
*/
for (String temp : toImport) {
if (RemoveFullyQualifiedName.containsMultiple(toImport.iterator(), temp, null)) {
// there are atleast two imports with this className
// import package add *
if (temp.lastIndexOf('.') > -1) {
temp = temp.substring(0, temp.lastIndexOf('.'));
out.println("import " + temp + ".*;");
} else {
throw new DecompilationException("Cant find the DOT . for fullyqualified name");
}
} else {
if (temp.lastIndexOf('.') == -1) {
// dot not found this is a class belonging to this package so dont add
} else {
out.println("import " + temp + ';');
}
}
}
// out.println("import " + temp + ';');
out.println();
/*
* if (!packagesUsed.isEmpty()) out.println();
*
* packagesUsed.add("java.lang"); packagesUsed.add(curPackage);
*/
Dava.v().set_CurrentPackageContext(importList);
// Dava.v().set_CurrentPackageContext(packagesUsed);
Dava.v().set_CurrentPackage(curPackage);
}
// Print class name + modifiers
{
String classPrefix = Modifier.toString(cl.getModifiers()).trim();
if (!cl.isInterface()) {
classPrefix = (classPrefix + " class").trim();
}
out.print(classPrefix + " " + cl.getShortJavaStyleName());
}
// Print extension
if (cl.hasSuperclass()) {
String superClassName = cl.getSuperclass().getName();
if (!"java.lang.Object".equals(superClassName)) {
// Nomair Naeem 8th Feb 2006
// Also check if the super class name is not a fully qualified name,
// in which case if the package is imported no need for the long name.
out.print(" extends " + RemoveFullyQualifiedName.getReducedName(importList, superClassName, cl.getType()));
}
}
// Print interfaces
{
Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
if (interfaceIt.hasNext()) {
if (cl.isInterface()) {
out.print(" extends ");
} else {
out.print(" implements ");
}
out.print(interfaceIt.next().getName());
while (interfaceIt.hasNext()) {
out.print(", " + interfaceIt.next().getName());
}
}
}
out.println();
out.println("{");
// Print fields
for (SootField f : cl.getFields()) {
if (f.isPhantom()) {
continue;
}
Type fieldType = f.getType();
String qualifiers = (Modifier.toString(f.getModifiers()) + " "
+ RemoveFullyQualifiedName.getReducedName(importList, fieldType.toString(), fieldType)).trim();
String declaration;
if (qualifiers.isEmpty()) {
declaration = Scene.v().quotedNameOf(f.getName());
} else {
declaration = qualifiers + " " + Scene.v().quotedNameOf(f.getName());
}
if (f.isFinal() && f.isStatic()) {
printTags(f, declaration, out);
} else {
out.println(" " + declaration + ';');
}
}
// Print methods
{
Iterator<SootMethod> methodIt = cl.methodIterator();
if (methodIt.hasNext()) {
if (cl.getMethodCount() != 0) {
out.println();
}
do { // condition is already checked
SootMethod method = methodIt.next();
if (method.isPhantom()) {
continue;
}
if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) {
if (!method.hasActiveBody()) {
throw new RuntimeException("method " + method.getName() + " has no active body!");
} else {
printTo(method.getActiveBody(), out);
}
if (methodIt.hasNext()) {
out.println();
}
} else {
// if method is abstract then print the declaration
out.print(" ");
out.print(method.getDavaDeclaration());
out.println(";");
if (methodIt.hasNext()) {
out.println();
}
}
} while (methodIt.hasNext());
}
}
/*
* January 23rd, 2006 In trying to handle the suepr class problem we need to introduce an inner class Instead of creating
* a data structure for it we are right now just going to print it in the form of a string
*
* It would be interesting to later have an internal inner class structure so that we could decompile inner classes into
* inner classes
*/
if (G.v().SootClassNeedsDavaSuperHandlerClass.contains(cl)) {
out.println("\n private static class DavaSuperHandler{");
out.println(" java.util.Vector myVector = new java.util.Vector();");
out.println("\n public Object get(int pos){");
out.println(" return myVector.elementAt(pos);");
out.println(" }");
out.println("\n public void store(Object obj){");
out.println(" myVector.add(obj);");
out.println(" }");
out.println(" }");
}
out.println("}");
}
private void printTags(SootField f, String declaration, PrintWriter out) {
Type fieldType = f.getType();
if (fieldType instanceof DoubleType) {
DoubleConstantValueTag t = (DoubleConstantValueTag) f.getTag(DoubleConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = " + t.getDoubleValue() + ';');
return;
}
} else if (fieldType instanceof FloatType) {
FloatConstantValueTag t = (FloatConstantValueTag) f.getTag(FloatConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = " + t.getFloatValue() + "f;");
return;
}
} else if (fieldType instanceof LongType) {
LongConstantValueTag t = (LongConstantValueTag) f.getTag(LongConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = " + t.getLongValue() + "l;");
return;
}
} else if (fieldType instanceof CharType) {
IntegerConstantValueTag t = (IntegerConstantValueTag) f.getTag(IntegerConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = '" + ((char) t.getIntValue()) + "';");
return;
}
} else if (fieldType instanceof BooleanType) {
IntegerConstantValueTag t = (IntegerConstantValueTag) f.getTag(IntegerConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + (t.getIntValue() == 0 ? " = false;" : " = true;"));
return;
}
} else if (fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) {
IntegerConstantValueTag t = (IntegerConstantValueTag) f.getTag(IntegerConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = " + t.getIntValue() + ';');
return;
}
} else {
StringConstantValueTag t = (StringConstantValueTag) f.getTag(StringConstantValueTag.NAME);
if (t != null) {
out.println(" " + declaration + " = \"" + t.getStringValue() + "\";");
return;
}
}
// System.out.println("Couldn't find type of field: " + f.getDeclaration());
out.println(" " + declaration + ';');
}
/**
* Prints out the method corresponding to b Body, (declaration and body), in the textual format corresponding to the IR
* used to encode b body.
*
* @param out
* a PrintWriter instance to print to.
*/
private void printTo(Body b, PrintWriter out) {
b.validate();
out.println(" " + b.getMethod().getDavaDeclaration());
if (Options.v().print_tags_in_output()) {
for (Tag t : b.getMethod().getTags()) {
out.println(t);
}
}
out.println(" {");
/*
* The locals are now printed out from within the toString method of ASTMethodNode Nomair A Naeem 10-MARCH-2005
*/
// printLocalsInBody(b, out);
printStatementsInBody(b, out);
out.println(" }");
}
}