forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SootMethod.java
975 lines (850 loc) · 28 KB
/
SootMethod.java
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
package soot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 1999 Raja Vallee-Rai
* Copyright (C) 2004 Ondrej Lhotak
* %%
* 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.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.dava.DavaBody;
import soot.dava.toolkits.base.renamer.RemoveFullyQualifiedName;
import soot.dotnet.members.DotnetMethod;
import soot.options.Options;
import soot.tagkit.AbstractHost;
import soot.util.IterableSet;
import soot.util.NumberedString;
/**
* Soot representation of a Java method. Can be declared to belong to a {@link SootClass}. Does not contain the actual code,
* which belongs to a {@link Body}. The {@link #getActiveBody()} method points to the currently-active body.
*/
public class SootMethod extends AbstractHost implements ClassMember, MethodOrMethodContext, SootMethodInterface {
private static final Logger logger = LoggerFactory.getLogger(SootMethod.class);
public static final String constructorName = "<init>";
public static final String staticInitializerName = "<clinit>";
/**
* Name of the current method.
*/
protected String name;
/**
* An array of parameter types taken by this {@link SootMethod} object, in declaration order.
*/
protected Type[] parameterTypes;
/**
* The return type of this object.
*/
protected Type returnType;
/**
* True when some {@link SootClass} object declares this {@link SootMethod} object.
*/
protected boolean isDeclared;
/**
* Holds the class which declares this <code>SootClass</code> method.
*/
protected SootClass declaringClass;
/**
* Modifiers associated with this {@link SootMethod} (e.g. private, protected, etc.)
*/
protected int modifiers;
/**
* Is this method a phantom method?
*/
protected boolean isPhantom = false;
/**
* Declared exceptions thrown by this method. Created upon demand.
*/
protected List<SootClass> exceptions;
/**
* Active body associated with this method.
*/
protected volatile Body activeBody;
/**
* Tells this method how to find out where its body lives.
*/
protected volatile MethodSource ms;
protected volatile String sig;
protected volatile String subSig;
protected NumberedString subsignature;
/**
* Constructs a {@link SootMethod} with the given name, parameter types and return type.
*/
public SootMethod(String name, List<Type> parameterTypes, Type returnType) {
this(name, parameterTypes, returnType, 0, Collections.<SootClass>emptyList());
}
/**
* Constructs a {@link SootMethod} with the given name, parameter types, return type and modifiers.
*/
public SootMethod(String name, List<Type> parameterTypes, Type returnType, int modifiers) {
this(name, parameterTypes, returnType, modifiers, Collections.<SootClass>emptyList());
}
/**
* Constructs a {@link SootMethod} with the given name, parameter types, return type, and list of thrown exceptions.
*/
public SootMethod(String name, List<Type> parameterTypes, Type returnType, int modifiers,
List<SootClass> thrownExceptions) {
this.name = name;
if (parameterTypes != null && !parameterTypes.isEmpty()) {
this.parameterTypes = parameterTypes.toArray(new Type[parameterTypes.size()]);
}
this.returnType = returnType;
this.modifiers = modifiers;
if (thrownExceptions != null && !thrownExceptions.isEmpty()) {
this.exceptions = new ArrayList<SootClass>(thrownExceptions);
}
this.subsignature = Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
}
/**
* Returns a hash code for this method consistent with structural equality.
*/
public int equivHashCode() {
return returnType.hashCode() * 101 + modifiers * 17 + name.hashCode();
}
/**
* Returns the name of this method.
*/
@Override
public String getName() {
return name;
}
/**
* Sets the name of this method.
*/
public synchronized void setName(String name) {
boolean wasDeclared = isDeclared;
SootClass oldDeclaringClass = declaringClass;
if (wasDeclared) {
oldDeclaringClass.removeMethod(this);
}
this.name = name;
this.sig = null;
this.subSig = null;
this.subsignature = Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
if (wasDeclared) {
oldDeclaringClass.addMethod(this);
}
}
/**
* Sets the declaring class
*/
public synchronized void setDeclaringClass(SootClass declClass) {
// We could call setDeclared here, however, when SootClass adds a method, it checks isDeclared
// and throws an exception if set. So we currently cannot call setDeclared here.
this.declaringClass = declClass;
this.sig = null;
}
/**
* Returns the class which declares the current {@link SootMethod}.
*/
@Override
public SootClass getDeclaringClass() {
if (!isDeclared) {
throw new RuntimeException("not declared: " + getName());
}
return declaringClass;
}
public void setDeclared(boolean isDeclared) {
this.isDeclared = isDeclared;
}
/**
* Returns true when some {@link SootClass} object declares this {@link SootMethod} object.
*/
@Override
public boolean isDeclared() {
return isDeclared;
}
/**
* Returns true when this {@link SootMethod} object is phantom.
*/
@Override
public boolean isPhantom() {
return isPhantom;
}
/**
* Returns true if this method is not phantom, abstract or native, i.e. this method can have a body.
*/
public boolean isConcrete() {
return !isPhantom() && !isAbstract() && !isNative();
}
/**
* Sets the phantom flag on this method.
*/
@Override
public void setPhantom(boolean value) {
if (value) {
if (!Scene.v().allowsPhantomRefs()) {
throw new RuntimeException("Phantom refs not allowed");
}
if (!Options.v().allow_phantom_elms() && declaringClass != null && !declaringClass.isPhantom()) {
throw new RuntimeException("Declaring class would have to be phantom");
}
}
this.isPhantom = value;
}
/**
* Gets the modifiers of this method.
*
* @see soot.Modifier
*/
@Override
public int getModifiers() {
return modifiers;
}
/**
* Sets the modifiers of this method.
*
* @see soot.Modifier
*/
@Override
public void setModifiers(int modifiers) {
this.modifiers = modifiers;
}
/**
* Returns the return type of this method.
*/
@Override
public Type getReturnType() {
return returnType;
}
/**
* Sets the return type of this method.
*/
public synchronized void setReturnType(Type t) {
boolean wasDeclared = isDeclared;
SootClass oldDeclaringClass = declaringClass;
if (wasDeclared) {
oldDeclaringClass.removeMethod(this);
}
this.returnType = t;
this.sig = null;
this.subSig = null;
this.subsignature = Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
if (wasDeclared) {
oldDeclaringClass.addMethod(this);
}
}
/**
* Returns the number of parameters taken by this method.
*/
public int getParameterCount() {
return parameterTypes == null ? 0 : parameterTypes.length;
}
/**
* Gets the type of the <i>n</i>th parameter of this method.
*/
@Override
public Type getParameterType(int n) {
return parameterTypes[n];
}
/**
* Returns a read-only list of the parameter types of this method.
*/
@Override
public List<Type> getParameterTypes() {
return parameterTypes == null ? Collections.<Type>emptyList() : Arrays.asList(parameterTypes);
}
/**
* Changes the set of parameter types of this method.
*/
public synchronized void setParameterTypes(List<Type> l) {
boolean wasDeclared = isDeclared;
SootClass oldDeclaringClass = declaringClass;
if (wasDeclared) {
oldDeclaringClass.removeMethod(this);
}
this.parameterTypes = l.toArray(new Type[l.size()]);
this.sig = null;
this.subSig = null;
this.subsignature = Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
if (wasDeclared) {
oldDeclaringClass.addMethod(this);
}
}
/**
* Returns the {@link MethodSource} of the current {@link SootMethod}.
*/
public MethodSource getSource() {
return ms;
}
/**
* Sets the {@link MethodSource} of the current {@link SootMethod}.
*/
public synchronized void setSource(MethodSource ms) {
this.ms = ms;
}
/**
* Retrieves the active body for this method.
*/
@SuppressWarnings("deprecation")
public Body getActiveBody() {
// Retrieve the active body so thread changes do not affect the
// synchronization between if the body exists and the returned body.
// This is a quick check just in case the activeBody exists.
Body activeBody = this.activeBody;
if (activeBody != null) {
return activeBody;
}
// Synchronize because we are operating on two fields that may be updated
// separately otherwise.
synchronized (this) {
// Re-check the activeBody because things might have changed
activeBody = this.activeBody;
if (activeBody != null) {
return activeBody;
}
if (declaringClass != null) {
declaringClass.checkLevel(SootClass.BODIES);
}
if ((declaringClass != null && declaringClass.isPhantomClass()) || isPhantom()) {
throw new RuntimeException("cannot get active body for phantom method: " + getSignature());
}
// ignore empty body exceptions if we are just computing coffi metrics
if (!soot.jbco.Main.metrics) {
throw new RuntimeException("no active body present for method " + getSignature());
}
return null;
}
}
/**
* Sets the active body for this method.
*/
public synchronized void setActiveBody(Body body) {
if ((declaringClass != null) && declaringClass.isPhantomClass()) {
throw new RuntimeException("cannot set active body for phantom class! " + this);
}
// If someone sets a body for a phantom method, this method then is no
// longer phantom
setPhantom(false);
if (!isConcrete()) {
throw new RuntimeException("cannot set body for non-concrete method! " + this);
}
if (body != null) {
body.setMethod(this);
}
this.activeBody = body;
}
/**
* Returns the active body if present, else constructs an active body and returns that.
*
* If you called Scene.v().loadClassAndSupport() for a class yourself, it will not be an application class, so you cannot
* get retrieve its active body. Please call {@link SootClass#setApplicationClass()} on the relevant class.
*/
public Body retrieveActiveBody() {
return retrieveActiveBody((b) -> {
});
}
/**
* Returns the active body if present, else constructs an active body, calls the consumer and returns the body afterward.
*
* If you called Scene.v().loadClassAndSupport() for a class yourself, it will not be an application class, so you cannot
* get retrieve its active body. Please call {@link SootClass#setApplicationClass()} on the relevant class.
*
* @param consumer
* Consumer that takes in the body of the method. The consumer is only invoked if the current invocation
* constructs a new body and is guaranteed to terminate before the body is available to other threads.
* @return active body of the method
*/
public Body retrieveActiveBody(Consumer<Body> consumer) {
// Retrieve the active body so thread changes do not affect the
// synchronization between if the body exists and the returned body.
// This is a quick check just in case the activeBody exists.
Body activeBody = this.activeBody;
if (activeBody != null) {
return activeBody;
}
// Synchronize because we are operating on multiple fields that may be updated
// separately otherwise.
synchronized (this) {
// Re-check the activeBody because things might have changed
activeBody = this.activeBody;
if (activeBody != null) {
return activeBody;
}
if (declaringClass != null) {
declaringClass.checkLevel(SootClass.BODIES);
}
if ((declaringClass != null && declaringClass.isPhantomClass()) || isPhantom()) {
throw new RuntimeException("cannot get resident body for phantom method : " + this);
}
if (ms == null) {
throw new RuntimeException("No method source set for method " + this);
}
try {
// Method sources are not expected to be thread safe
activeBody = ms.getBody(this, "jb");
} catch (Exception e) {
logger.warn("Failed to build body for method " + this);
setPhantom(true);
return null;
}
// Call the consumer such that clients can update any data structures, caches, etc.
// atomically before the body is available to other threads.
consumer.accept(activeBody);
setActiveBody(activeBody);
// If configured, we drop the method source to save memory
if (Options.v().drop_bodies_after_load()) {
ms = null;
}
return activeBody;
}
}
/**
* Returns true if this method has an active body.
*/
public boolean hasActiveBody() {
return activeBody != null;
}
/**
* Releases the active body associated with this method.
*/
public synchronized void releaseActiveBody() {
this.activeBody = null;
}
/**
* Adds the given exception to the list of exceptions thrown by this method unless the exception is already in the list.
*/
public void addExceptionIfAbsent(SootClass e) {
if (!throwsException(e)) {
addException(e);
}
}
/**
* Adds the given exception to the list of exceptions thrown by this method.
*/
public void addException(SootClass e) {
logger.trace("Adding exception {}", e);
if (exceptions == null) {
exceptions = new ArrayList<SootClass>();
} else if (exceptions.contains(e)) {
throw new RuntimeException("already throws exception " + e.getName());
}
exceptions.add(e);
}
/**
* Removes the given exception from the list of exceptions thrown by this method.
*/
public void removeException(SootClass e) {
logger.trace("Removing exception {}", e);
if (exceptions == null || !exceptions.contains(e)) {
throw new RuntimeException("does not throw exception " + e.getName());
}
exceptions.remove(e);
}
/**
* Returns true if this method throws exception <code>e</code>.
*/
public boolean throwsException(SootClass e) {
return exceptions != null && exceptions.contains(e);
}
public void setExceptions(List<SootClass> exceptions) {
this.exceptions = (exceptions == null || exceptions.isEmpty()) ? null : new ArrayList<SootClass>(exceptions);
}
/**
* Returns a backed list of the exceptions thrown by this method.
*/
public List<SootClass> getExceptions() {
if (exceptions == null) {
exceptions = new ArrayList<SootClass>();
}
return exceptions;
}
public List<SootClass> getExceptionsUnsafe() {
return exceptions;
}
/**
* Convenience method returning true if this method is static.
*/
@Override
public boolean isStatic() {
return Modifier.isStatic(this.getModifiers());
}
/**
* Convenience method returning true if this method is private.
*/
@Override
public boolean isPrivate() {
return Modifier.isPrivate(this.getModifiers());
}
/**
* Convenience method returning true if this method is public.
*/
@Override
public boolean isPublic() {
return Modifier.isPublic(this.getModifiers());
}
/**
* Convenience method returning true if this method is protected.
*/
@Override
public boolean isProtected() {
return Modifier.isProtected(this.getModifiers());
}
/**
* Convenience method returning true if this method is abstract.
*/
public boolean isAbstract() {
return Modifier.isAbstract(this.getModifiers());
}
/**
* Convenience method returning true if this method is final.
*/
public boolean isFinal() {
return Modifier.isFinal(this.getModifiers());
}
/**
* Convenience method returning true if this method is native.
*/
public boolean isNative() {
return Modifier.isNative(this.getModifiers());
}
/**
* Convenience method returning true if this method is synchronized.
*/
public boolean isSynchronized() {
return Modifier.isSynchronized(this.getModifiers());
}
/**
*
* @return yes if this is the main method
*/
public boolean isMain() {
return isPublic() && isStatic()
&& Scene.v().getSubSigNumberer()
.findOrAdd(Options.v().src_prec() != Options.src_prec_dotnet ? "void main(java.lang.String[])"
: DotnetMethod.MAIN_METHOD_SIGNATURE)
.equals(subsignature);
}
/**
*
* @return yes, if this function is a constructor. Please not that <clinit> methods are not treated as constructors in this
* method.
*/
public boolean isConstructor() {
return constructorName.equals(name);
}
/**
*
* @return yes, if this function is a static initializer.
*/
public boolean isStaticInitializer() {
return staticInitializerName.equals(name);
}
/**
* @return yes, if this is a class initializer or main function.
*/
public boolean isEntryMethod() {
if (isStatic() && SootMethod.staticInitializerName.equals(subsignature.getString())) {
return true;
}
return isMain();
}
/**
* We rely on the JDK class recognition to decide if a method is JDK method.
*/
public boolean isJavaLibraryMethod() {
return getDeclaringClass().isJavaLibraryClass();
}
/**
* Returns the parameters part of the signature in the format in which it appears in bytecode.
*/
public String getBytecodeParms() {
StringBuilder buffer = new StringBuilder();
for (Type type : getParameterTypes()) {
buffer.append(AbstractJasminClass.jasminDescriptorOf(type));
}
return buffer.toString().intern();
}
/**
* Returns the signature of this method in the format in which it appears in bytecode (eg. [Ljava/lang/Object instead of
* java.lang.Object[]).
*/
public String getBytecodeSignature() {
StringBuilder buffer = new StringBuilder();
buffer.append('<');
buffer.append(Scene.v().quotedNameOf(getDeclaringClass().getName()));
buffer.append(": ");
buffer.append(getName());
buffer.append(AbstractJasminClass.jasminDescriptorOf(makeRef()));
buffer.append('>');
return buffer.toString().intern();
}
/**
* Returns the Soot signature of this method. Used to refer to methods unambiguously.
*/
@Override
public String getSignature() {
String sig = this.sig;
if (sig == null) {
synchronized (this) {
sig = this.sig;
if (sig == null) {
this.sig = sig = getSignature(getDeclaringClass(), getSubSignature());
}
}
}
return sig;
}
public static String getSignature(SootClass cl, String name, List<Type> params, Type returnType) {
return getSignature(cl, getSubSignatureImpl(name, params, returnType));
}
public static String getSignature(SootClass cl, String subSignature) {
StringBuilder buffer = new StringBuilder();
buffer.append('<');
buffer.append(Scene.v().quotedNameOf(cl.getName()));
buffer.append(": ");
buffer.append(subSignature);
buffer.append('>');
return buffer.toString();
}
/**
* Returns the Soot subsignature of this method. Used to refer to methods unambiguously.
*/
public String getSubSignature() {
String subSig = this.subSig;
if (subSig == null) {
synchronized (this) {
subSig = this.subSig;
if (subSig == null) {
this.subSig = subSig = getSubSignatureImpl(getName(), getParameterTypes(), getReturnType());
}
}
}
return subSig;
}
public static String getSubSignature(String name, List<Type> params, Type returnType) {
return getSubSignatureImpl(name, params, returnType);
}
private static String getSubSignatureImpl(String name, List<Type> params, Type returnType) {
StringBuilder buffer = new StringBuilder();
buffer.append(returnType.toQuotedString());
buffer.append(' ');
buffer.append(Scene.v().quotedNameOf(name));
buffer.append('(');
if (params != null) {
for (int i = 0, e = params.size(); i < e; i++) {
if (i > 0) {
buffer.append(',');
}
buffer.append(params.get(i).toQuotedString());
}
}
buffer.append(')');
return buffer.toString();
}
public NumberedString getNumberedSubSignature() {
return subsignature;
}
/**
* Returns the signature of this method.
*/
@Override
public String toString() {
return getSignature();
}
/*
* TODO: Nomair A. Naeem .... 8th Feb 2006 This is really messy coding So much for modularization!! Should some day look
* into creating the DavaDeclaration from within DavaBody
*/
public String getDavaDeclaration() {
if (staticInitializerName.equals(getName())) {
return "static";
}
StringBuilder buffer = new StringBuilder();
// modifiers
StringTokenizer st = new StringTokenizer(Modifier.toString(this.getModifiers()));
if (st.hasMoreTokens()) {
buffer.append(st.nextToken());
while (st.hasMoreTokens()) {
buffer.append(' ').append(st.nextToken());
}
}
if (buffer.length() != 0) {
buffer.append(' ');
}
// return type + name
if (constructorName.equals(getName())) {
buffer.append(getDeclaringClass().getShortJavaStyleName());
} else {
Type t = this.getReturnType();
String tempString = t.toString();
/*
* Added code to handle RuntimeExcepotion thrown by getActiveBody
*/
if (hasActiveBody()) {
DavaBody body = (DavaBody) getActiveBody();
IterableSet<String> importSet = body.getImportList();
if (!importSet.contains(tempString)) {
body.addToImportList(tempString);
}
tempString = RemoveFullyQualifiedName.getReducedName(importSet, tempString, t);
}
buffer.append(tempString).append(' ');
buffer.append(Scene.v().quotedNameOf(this.getName()));
}
// parameters
int count = 0;
buffer.append('(');
for (Iterator<Type> typeIt = this.getParameterTypes().iterator(); typeIt.hasNext();) {
Type t = typeIt.next();
String tempString = t.toString();
/*
* Nomair A. Naeem 7th Feb 2006 It is nice to remove the fully qualified type names of parameters if the package they
* belong to have been imported javax.swing.ImageIcon should be just ImageIcon if javax.swing is imported If not
* imported WHY NOT..import it!!
*/
if (hasActiveBody()) {
DavaBody body = (DavaBody) getActiveBody();
IterableSet<String> importSet = body.getImportList();
if (!importSet.contains(tempString)) {
body.addToImportList(tempString);
}
tempString = RemoveFullyQualifiedName.getReducedName(importSet, tempString, t);
}
buffer.append(tempString).append(' ');
buffer.append(' ');
if (hasActiveBody()) {
buffer.append(((DavaBody) getActiveBody()).get_ParamMap().get(count++));
} else {
if (t == BooleanType.v()) {
buffer.append('z').append(count++);
} else if (t == ByteType.v()) {
buffer.append('b').append(count++);
} else if (t == ShortType.v()) {
buffer.append('s').append(count++);
} else if (t == CharType.v()) {
buffer.append('c').append(count++);
} else if (t == IntType.v()) {
buffer.append('i').append(count++);
} else if (t == LongType.v()) {
buffer.append('l').append(count++);
} else if (t == DoubleType.v()) {
buffer.append('d').append(count++);
} else if (t == FloatType.v()) {
buffer.append('f').append(count++);
} else if (t == StmtAddressType.v()) {
buffer.append('a').append(count++);
} else if (t == ErroneousType.v()) {
buffer.append('e').append(count++);
} else if (t == NullType.v()) {
buffer.append('n').append(count++);
} else {
buffer.append('r').append(count++);
}
}
if (typeIt.hasNext()) {
buffer.append(", ");
}
}
buffer.append(')');
// Print exceptions
if (exceptions != null) {
Iterator<SootClass> exceptionIt = this.getExceptions().iterator();
if (exceptionIt.hasNext()) {
buffer.append(" throws ").append(exceptionIt.next().getName());
while (exceptionIt.hasNext()) {
buffer.append(", ").append(exceptionIt.next().getName());
}
}
}
return buffer.toString().intern();
}
/**
* Returns the declaration of this method, as used at the top of textual body representations (before the {}'s containing
* the code for representation.)
*/
public String getDeclaration() {
StringBuilder buffer = new StringBuilder();
// modifiers
StringTokenizer st = new StringTokenizer(Modifier.toString(this.getModifiers()));
if (st.hasMoreTokens()) {
buffer.append(st.nextToken());
while (st.hasMoreTokens()) {
buffer.append(' ').append(st.nextToken());
}
}
if (buffer.length() != 0) {
buffer.append(' ');
}
// return type + name
buffer.append(this.getReturnType().toQuotedString()).append(' ');
buffer.append(Scene.v().quotedNameOf(this.getName()));
// parameters
buffer.append('(');
for (Iterator<Type> typeIt = this.getParameterTypes().iterator(); typeIt.hasNext();) {
Type t = typeIt.next();
buffer.append(t.toQuotedString());
if (typeIt.hasNext()) {
buffer.append(", ");
}
}
buffer.append(')');
// Print exceptions
if (exceptions != null) {
Iterator<SootClass> exceptionIt = this.getExceptions().iterator();
if (exceptionIt.hasNext()) {
buffer.append(" throws ").append(Scene.v().quotedNameOf(exceptionIt.next().getName()));
while (exceptionIt.hasNext()) {
buffer.append(", ").append(Scene.v().quotedNameOf(exceptionIt.next().getName()));
}
}
}
return buffer.toString().intern();
}
@Override
public SootMethod method() {
return this;
}
@Override
public Context context() {
return null;
}
public SootMethodRef makeRef() {
return Scene.v().makeMethodRef(declaringClass, name, parameterTypes == null ? null : Arrays.asList(parameterTypes),
returnType, isStatic());
}
public boolean isValidResolve(SootMethodRef ref) {
return (this.isStatic() == ref.isStatic()) && Objects.equals(this.getDeclaringClass(), ref.getDeclaringClass())
&& Objects.equals(this.getName(), ref.getName()) && Objects.equals(this.getReturnType(), ref.getReturnType())
&& Objects.equals(this.getParameterTypes(), ref.getParameterTypes());
}
@Override
public int getJavaSourceStartLineNumber() {
super.getJavaSourceStartLineNumber();
// search statements for first line number
if (line == -1 && hasActiveBody()) {
for (Unit u : getActiveBody().getUnits()) {
int l = u.getJavaSourceStartLineNumber();
if (l > -1) {
// store l-1, as method header is usually one line before
// 1st statement
line = l - 1;
break;
}
}
}
return line;
}
}