forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodSubSignature.java
More file actions
154 lines (135 loc) · 4.34 KB
/
MethodSubSignature.java
File metadata and controls
154 lines (135 loc) · 4.34 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
package soot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2003 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 com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import soot.jimple.Stmt;
import soot.util.NumberedString;
/**
* Allows one-time parsing of method subsignatures. Note that these method sub signatures are resolved, i.e. we resolve the
* complete types upon construction.
*
* @author Marc Miltenberger
*/
public class MethodSubSignature {
public final String methodName;
public final Type returnType;
public final List<Type> parameterTypes;
public final NumberedString numberedSubSig;
private static final Pattern PATTERN_METHOD_SUBSIG
= Pattern.compile("(?<returnType>.*?) (?<methodName>.*?)\\((?<parameters>.*?)\\)");
public MethodSubSignature(SootMethodRef r) {
methodName = r.getName();
returnType = r.getReturnType();
parameterTypes = r.getParameterTypes();
numberedSubSig = r.getSubSignature();
}
public MethodSubSignature(NumberedString subsig) {
this.numberedSubSig = subsig;
Matcher m = PATTERN_METHOD_SUBSIG.matcher(subsig.toString());
if (!m.matches()) {
throw new IllegalArgumentException("Not a valid subsignature: " + subsig);
}
Scene sc = Scene.v();
methodName = m.group(2);
returnType = sc.getTypeUnsafe(m.group(1));
String parameters = m.group(3);
String[] spl = parameters.split(",");
parameterTypes = new ArrayList<>(spl.length);
if (parameters != null && !parameters.isEmpty()) {
for (String p : spl) {
parameterTypes.add(sc.getTypeUnsafe(p.trim()));
}
}
}
public MethodSubSignature(String methodName, Type returnType, List<Type> parameterTypes) {
this.methodName = methodName;
this.returnType = returnType;
this.parameterTypes = parameterTypes;
this.numberedSubSig = Scene.v().getSubSigNumberer()
.findOrAdd(returnType + " " + methodName + "(" + Joiner.on(',').join(parameterTypes) + ")");
}
/**
* Creates a new instance of the {@link MethodSubSignature} class based on a call site. The subsignature of the callee will
* be taken from the method referenced at the call site.
*
* @param callSite
* The call site
*/
public MethodSubSignature(Stmt callSite) {
this(callSite.getInvokeExpr().getMethodRef().getSubSignature());
}
public String getMethodName() {
return methodName;
}
public Type getReturnType() {
return returnType;
}
public List<Type> getParameterTypes() {
return parameterTypes;
}
public NumberedString getNumberedSubSig() {
return numberedSubSig;
}
/**
* Tries to find the exact method in a class. Returns null if cannot be found
*
* @param c
* the class
* @return the method (or null)
*/
public SootMethod getInClassUnsafe(SootClass c) {
return c.getMethodUnsafe(numberedSubSig);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((numberedSubSig == null) ? 0 : numberedSubSig.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
MethodSubSignature other = (MethodSubSignature) obj;
if (numberedSubSig == null) {
if (other.numberedSubSig != null) {
return false;
}
} else if (!numberedSubSig.equals(other.numberedSubSig)) {
return false;
}
return true;
}
@Override
public String toString() {
return numberedSubSig.toString();
}
}