forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDexType.java
More file actions
229 lines (206 loc) · 5.71 KB
/
DexType.java
File metadata and controls
229 lines (206 loc) · 5.71 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
package soot.dexpler;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2012 Michael Markert, Frank Hartmann
*
* (c) 2012 University of Luxembourg - Interdisciplinary Centre for
* Security Reliability and Trust (SnT) - All rights reserved
* Alexandre Bartel
*
* %%
* 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 org.jf.dexlib2.iface.reference.TypeReference;
import org.jf.dexlib2.immutable.reference.ImmutableTypeReference;
import soot.BooleanType;
import soot.ByteType;
import soot.CharType;
import soot.DoubleType;
import soot.FloatType;
import soot.IntType;
import soot.LongType;
import soot.RefType;
import soot.ShortType;
import soot.Type;
import soot.UnknownType;
import soot.VoidType;
/**
* Wrapper for a dexlib TypeIdItem.
*
*/
public class DexType {
protected String name;
protected TypeReference type;
public DexType(TypeReference type) {
if (type == null) {
throw new RuntimeException("error: type ref is null!");
}
this.type = type;
this.name = type.getType();
}
public DexType(String type) {
if (type == null) {
throw new RuntimeException("error: type is null!");
}
this.type = new ImmutableTypeReference(type);
this.name = type;
}
public String getName() {
return name;
}
public boolean overwriteEquivalent(DexType field) {
return name.equals(field.getName());
}
public TypeReference getType() {
return type;
}
/**
* Return the appropriate Soot Type for this DexType.
*
* @return the Soot Type
*/
public Type toSoot() {
return toSoot(type.getType(), 0);
}
/**
* Return the appropriate Soot Type for the given TypeReference.
*
* @param type
* the TypeReference to convert
* @return the Soot Type
*/
public static Type toSoot(TypeReference type) {
return toSoot(type.getType(), 0);
}
public static Type toSoot(String type) {
return toSoot(type, 0);
}
/**
* Return if the given TypeIdItem is wide (i.e. occupies 2 registers).
*
* @param typeReference.getType()
* the TypeIdItem to analyze
* @return if type is wide
*/
public static boolean isWide(TypeReference typeReference) {
String t = typeReference.getType();
return isWide(t);
}
public static boolean isWide(String type) {
return type.startsWith("J") || type.startsWith("D");
}
/**
* Determine the soot type from a byte code type descriptor.
*
*/
private static Type toSoot(String typeDescriptor, int pos) {
Type type;
char typeDesignator = typeDescriptor.charAt(pos);
// see https://code.google.com/p/smali/wiki/TypesMethodsAndFields
switch (typeDesignator) {
case 'Z': // boolean
type = BooleanType.v();
break;
case 'B': // byte
type = ByteType.v();
break;
case 'S': // short
type = ShortType.v();
break;
case 'C': // char
type = CharType.v();
break;
case 'I': // int
type = IntType.v();
break;
case 'J': // long
type = LongType.v();
break;
case 'F': // float
type = FloatType.v();
break;
case 'D': // double
type = DoubleType.v();
break;
case 'L': // object
type = RefType.v(Util.dottedClassName(typeDescriptor));
break;
case 'V': // void
type = VoidType.v();
break;
case '[': // array
type = toSoot(typeDescriptor, pos + 1).makeArrayType();
break;
default:
type = UnknownType.v();
}
return type;
}
/**
* Seems that representation of Annotation type in Soot is not consistent with the normal type representation. Normal type
* representation would be a.b.c.ClassName Java bytecode representation is La/b/c/ClassName; Soot Annotation type
* representation (and Jasmin's) is a/b/c/ClassName.
*
* This method transforms the Java bytecode representation into the Soot annotation type representation.
*
* Ljava/lang/Class<Ljava/lang/Enum<*>;>; becomes java/lang/Class<java/lang/Enum<*>>
*
* @param type
* @param pos
* @return
*/
public static String toSootICAT(String type) {
type = type.replace(".", "/");
String r = "";
String[] split1 = type.split(";");
for (String s : split1) {
if (s.startsWith("L")) {
s = s.replaceFirst("L", "");
}
if (s.startsWith("<L")) {
s = s.replaceFirst("<L", "<");
}
r += s;
}
return r;
}
public static String toDalvikICAT(String type) {
type = type.replaceAll("<", "<L");
type = type.replaceAll(">", ">;");
type = "L" + type; // a class name cannot be a primitive
type = type.replaceAll("L\\*;", "*");
if (!type.endsWith(";")) {
type += ";";
}
return type;
}
/**
* Types read from annotations should be converted to Soot type. However, to maintain compatibility with Soot code most
* type will not be converted.
*
* @param type
* @return
*/
public static String toSootAT(String type) {
return type;
}
@Override
public String toString() {
return name;
}
}