forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJastAddInitialResolver.java
More file actions
170 lines (153 loc) · 5.69 KB
/
JastAddInitialResolver.java
File metadata and controls
170 lines (153 loc) · 5.69 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
package soot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2008 Eric Bodden
* Copyright (C) 2008 Torbjorn Ekman
* %%
* 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.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.JastAddJ.BodyDecl;
import soot.JastAddJ.CompilationUnit;
import soot.JastAddJ.ConstructorDecl;
import soot.JastAddJ.MethodDecl;
import soot.JastAddJ.Program;
import soot.JastAddJ.TypeDecl;
import soot.javaToJimple.IInitialResolver;
/**
* An {@link IInitialResolver} for the JastAdd frontend.
*
* @author Torbjorn Ekman
* @author Eric Bodden
*/
public class JastAddInitialResolver implements IInitialResolver {
private static final Logger logger = LoggerFactory.getLogger(JastAddInitialResolver.class);
public JastAddInitialResolver(soot.Singletons.Global g) {
}
public static JastAddInitialResolver v() {
return soot.G.v().soot_JastAddInitialResolver();
}
protected Map<String, CompilationUnit> classNameToCU = new HashMap<String, CompilationUnit>();
public void formAst(String fullPath, List<String> locations, String className) {
Program program = SootResolver.v().getProgram();
CompilationUnit u = program.getCachedOrLoadCompilationUnit(fullPath);
if (u != null && !u.isResolved) {
u.isResolved = true;
java.util.ArrayList<soot.JastAddJ.Problem> errors = new java.util.ArrayList<soot.JastAddJ.Problem>();
u.errorCheck(errors);
if (!errors.isEmpty()) {
for (soot.JastAddJ.Problem p : errors) {
logger.debug("" + p);
}
// die
throw new CompilationDeathException(CompilationDeathException.COMPILATION_ABORTED,
"there were errors during parsing and/or type checking (JastAdd frontend)");
}
u.transformation();
u.jimplify1phase1();
u.jimplify1phase2();
HashSet<SootClass> types = new HashSet<SootClass>();
for (TypeDecl typeDecl : u.getTypeDecls()) {
collectTypeDecl(typeDecl, types);
}
if (types.isEmpty()) {
classNameToCU.put(className, u);
} else {
for (SootClass sc : types) {
classNameToCU.put(sc.getName(), u);
}
}
}
}
@SuppressWarnings("unchecked")
private void collectTypeDecl(TypeDecl typeDecl, HashSet<SootClass> types) {
types.add(typeDecl.getSootClassDecl());
for (TypeDecl nestedType : (Collection<TypeDecl>) typeDecl.nestedTypes()) {
collectTypeDecl(nestedType, types);
}
}
@SuppressWarnings("unchecked")
private TypeDecl findNestedTypeDecl(TypeDecl typeDecl, SootClass sc) {
if (typeDecl.sootClass() == sc) {
return typeDecl;
}
for (TypeDecl nestedType : (Collection<TypeDecl>) typeDecl.nestedTypes()) {
TypeDecl t = findNestedTypeDecl(nestedType, sc);
if (t != null) {
return t;
}
}
return null;
}
public Dependencies resolveFromJavaFile(SootClass sootclass) {
CompilationUnit u = classNameToCU.get(sootclass.getName());
if (u == null) {
throw new RuntimeException("Error: couldn't find class: " + sootclass.getName() + " are the packages set properly?");
}
HashSet<SootClass> types = new HashSet<SootClass>();
for (TypeDecl typeDecl : u.getTypeDecls()) {
collectTypeDecl(typeDecl, types);
}
Dependencies deps = new Dependencies();
u.collectTypesToHierarchy(deps.typesToHierarchy);
u.collectTypesToSignatures(deps.typesToSignature);
for (SootClass sc : types) {
for (SootMethod m : sc.getMethods()) {
m.setSource(new MethodSource() {
public Body getBody(SootMethod m, String phaseName) {
SootClass sc = m.getDeclaringClass();
CompilationUnit u = classNameToCU.get(sc.getName());
for (TypeDecl typeDecl : u.getTypeDecls()) {
typeDecl = findNestedTypeDecl(typeDecl, sc);
if (typeDecl != null) {
if (typeDecl.clinit == m) {
typeDecl.jimplify2clinit();
return m.getActiveBody();
}
for (BodyDecl bodyDecl : typeDecl.getBodyDecls()) {
if (bodyDecl instanceof MethodDecl) {
MethodDecl methodDecl = (MethodDecl) bodyDecl;
if (m.equals(methodDecl.sootMethod)) {
methodDecl.jimplify2();
return m.getActiveBody();
}
} else if (bodyDecl instanceof ConstructorDecl) {
ConstructorDecl constrDecl = (ConstructorDecl) bodyDecl;
if (m.equals(constrDecl.sootMethod)) {
constrDecl.jimplify2();
return m.getActiveBody();
}
}
}
}
}
throw new RuntimeException(
"Could not find body for " + m.getSignature() + " in " + m.getDeclaringClass().getName());
}
});
}
}
return deps;
}
}