-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiClassWrapped.java
92 lines (74 loc) · 2.97 KB
/
iClassWrapped.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
import java.lang.reflect.Array;
import java.util.stream.Stream;
public class iClassWrapped extends iClass {
Class<?> x;
protected iClassWrapped(Scope parent, Class<?> x) {
super(parent, null);
this.x = x;
}
static iClassWrapped from(Scope parent, Class<?> x) {
if (x.isArray())
return new iClassArrayWrapped(parent, new iClassWrapped(parent, x));
return new iClassWrapped(parent, x);
}
public String getName() {
return x.getName();
}
@Override
public iObject cast(iObject obj) {
return new iObjectWrapped(getScope(), x.cast(obj.asWrapped().x));
}
public boolean isAssignableFrom(iClass cls) {
if (cls instanceof iClassWrapped)
return isAssignableFrom((iClassWrapped) cls);
throw new UnsupportedOperationException();
}
public boolean equals(Object obj) {
if (obj instanceof iClassWrapped)
return x.equals(((iClassWrapped) obj).x);
if (obj instanceof Class<?>)
return x.equals((Class<?>) obj);
throw new UnsupportedOperationException();
}
public boolean isAssignableFrom(iClassWrapped cls) {
return x.isAssignableFrom(cls.x);
}
public iClass getComponentType() {
Class<?> xx = x.getComponentType();
if (xx == null)
return null;
return iClassWrapped.from(getScope(), xx);
}
public iField getField(String name) throws Throwable {
return new iFieldWrapped(getScope(), x.getField(name));
}
public iMethod getMethod(String name, iClass... parameterTypes) throws Throwable {
return new iMethodWrapped(getScope(),
x.getMethod(name, Stream.of(parameterTypes).map(t -> ((iClassWrapped) t).x).toArray(Class[]::new)));
}
public iMethod[] getMethods() {
return Stream.of(x.getMethods()).map(m -> new iMethodWrapped(getScope(), m)).toArray(iMethod[]::new);
}
public iConstructor getConstructor(iClass... parameterTypes) throws Throwable {
return new iConstructorWrapped(getScope(),
x.getConstructor(Stream.of(parameterTypes).map(t -> ((iClassWrapped) t).x).toArray(Class[]::new)));
}
public iConstructor[] getConstructors() {
return Stream.of(x.getConstructors()).map(c -> new iConstructorWrapped(getScope(), c))
.toArray(iConstructor[]::new);
}
public iObject newArray(int[] dimensions) {
Object a = Array.newInstance(x, dimensions);
return new iArrayWrapped(getScope(), a);
}
public void setItem(iObject a, int i, iObject v) {
Array.set(a.asWrapped().x, i, v.asWrapped().x);
}
public iObject getItem(iObject a, int i) {
return new iObjectWrapped(getScope(), Array.get(a.asWrapped().x, i));
}
@Override
public String toString() {
return String.format("iClassWrapped(%s)", x.getName());
}
}