-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScopeImpl.java
261 lines (237 loc) · 9.67 KB
/
ScopeImpl.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
import java.lang.reflect.Array;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.TypeParameter;
public class ScopeImpl implements Scope {
JavaFileiClassLoader loader;
ScopeImpl parent;
Node node;
iClassVirtual iclzv;
VarBlock vars;
public ScopeImpl(ScopeImpl parent, Node node, boolean new_var_block) {
this.parent = parent;
if (node == null)
throw new UnsupportedOperationException();
this.node = node;
vars = new_var_block ? (parent == null ? new VarBlock(null, this) : parent.vars.getChild(this)) : parent.vars;
}
public JavaFileiClassLoader getClassLoader() {
if (loader != null)
return loader;
if (parent == null)
throw new RuntimeException("wtf?");
return parent.getClassLoader();
}
public void setClz(iClassVirtual clz) {
iclzv = clz;
}
public static Scope newRootScope(Node node) {
if (node == null) {
node = new EmptyStmt();
}
return new ScopeImpl(null, node, true);
}
@Override
public VarBlock getVars() {
return vars;
}
LinkedHashMap<Node, Scope> childs = new LinkedHashMap<>();
public Scope getChild(Node node) {
if (node == this.node) {
return this;
}
if (node == null) {
node = new EmptyStmt();
}
boolean new_var_block = node instanceof BlockStmt;
// if (node instanceof Node)System.out.println(node.getClass().getSimpleName());
// if (!this.node.isAncestorOf(node))throw new UnsupportedOperationException();
return childs.computeIfAbsent(node, k -> new ScopeImpl(this, k, new_var_block));
}
public boolean hasParent() {
return parent != null;
}
public Scope getParent() {
return parent;
}
public iExecutor getExecutor() {
return new iExecutorImpl(this);
}
static boolean debuglog = false;
public void log(String format, Object... args) {
if (debuglog) {
/*
* String p = " ".repeat(depth);
* System.out.println(Arrays.stream(String.format(format,
* args).split("\n")).map(s -> p + s) .collect(Collectors.joining("\n")));
*/
System.out.printf(format + "\n", args);
// System.out.println(String.format(format, args) + ": " + this);
}
}
public String toString() {
return String.format("Scope(%s)%s", node, vars);
}
public iExecutable findExecutable(String name, List<iClass> types) throws NoSuchMethodException {
if (node instanceof ClassOrInterfaceDeclaration) {
if (name != null && iclzv != null)
try {
return iclzv.getMethod(name, types.toArray(iClass[]::new));
} catch (NoSuchMethodException t) {
log("[err] %s", t);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
if (hasParent())
return getParent().findExecutable(name, types);
throw new NoSuchMethodException(name);
}
@FunctionalInterface
public interface FindClassWTFZ {
iClass apply(String name) throws Throwable;
}
iClass findClassWTF2(String name, FindClassWTFZ f) throws Throwable {
Class<?> clz = Primitives.findClass(name);
if (clz != null)
return iClassWrapped.from(this, clz);
if (name.endsWith("[]")) {
clz = ((iClassWrapped) f.apply(name.substring(0, name.length() - 2))).x;
clz = Array.newInstance(clz, 0).getClass();
return new iClassArrayWrapped(this, iClassWrapped.from(this, clz));
}
if (name.endsWith(">")) {
return f.apply(name.substring(0, name.lastIndexOf('<')));
}
throw new ClassNotFoundException(name);
}
iClass findClassWTF(String name, FindClassWTFZ f) throws Throwable {
log("[findClassWTF] %s", name);
iClass c = findClassWTF2(name, f);
log("[findClassWTF2] %s=%s", name, f);
return c;
}
iClass findClassImports(String name) throws Throwable {
log("[findClassImports] %s", name);
if (!(node instanceof CompilationUnit))
return parent.findClassImports(name);
try {
return findClassWTF(name, this::findClassImports);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
CompilationUnit cu = (CompilationUnit) node;
iClass clz = cu.getImports().stream().map(i -> {
if (i.isStatic())
throw new UnsupportedOperationException();
try {
String n = i.getNameAsString();
log("[import] (%s) name=%s", i.toString().trim(), name);
if (i.isAsterisk()) {
//System.out.printf("(%s).startsWith(%s)=%s\n", name, i.getNameAsString(),name.startsWith(i.getNameAsString()));
//if (!name.startsWith(i.getNameAsString()))
if (cu.getImports().stream().allMatch(ii -> !name.startsWith(ii.getNameAsString())))
return findClassExt(n + "." + name);
} else if (n.endsWith("." + name))
return findClassExt(n);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
return null;
}).filter(c -> c != null).findFirst().orElseThrow(ClassNotFoundException::new);
log("[findClassImports2] %s=%s", name, clz);
return clz;
}
public iClass findClassExt(String name) throws ClassNotFoundException {
try {
return findClassWTF(name, this::findClassExt);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
try {
return findClassImports(name);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
try {
return getClassLoader().findClassExt(name);
} catch (ClassNotFoundException e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public iClass findClassLocal(String name) throws Throwable {
try {
return findClassWTF(name, this::findClassLocal);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
log("[findClassLocal] %s %s", name, node.getClass().getSimpleName());
if (node instanceof CompilationUnit || node instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration cid = node.stream()
.filter(n -> n instanceof ClassOrInterfaceDeclaration
&& ((ClassOrInterfaceDeclaration) n).getNameAsString().equals(name))
.map(n -> (ClassOrInterfaceDeclaration) n).findAny().orElse(null);
if (cid != null)
return new iClassVirtual(getChild(cid), cid);
//node.stream().filter(n -> n instanceof ClassOrInterfaceDeclaration).forEach(n -> System.out.println(n));
//throw new UnsupportedOperationException();
}
//JavaFileiClassLoader loader = getClassLoader();
if (node instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration d = ((ClassOrInterfaceDeclaration) node);
Optional<TypeParameter> tp = d.getTypeParameters().stream().filter(t -> t.getNameAsString().equals(name))
.findFirst();
if (tp.isPresent()) {
NodeList<ClassOrInterfaceType> tb = tp.get().getTypeBound();
if (tb.size() != 1)
throw new UnsupportedOperationException();
ClassOrInterfaceType t = tb.get(0);
return findClassLocal(t.getNameWithScope());
}
}
if (hasParent())
return getParent().findClass(name);
throw new ClassNotFoundException(name);
}
public iClass findClass(Type name) throws ClassNotFoundException {
return findClass(name.toString());
}
public iClass findClass(String name) throws ClassNotFoundException {
log("[findClass] %s", name);
try {
return findClassWTF(name, this::findClass);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
try {
return findClassLocal(name);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
try {
return findClassExt(name);
} catch (ClassNotFoundException e) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
throw new ClassNotFoundException(name);
}
}