-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnownType.java
180 lines (153 loc) · 5.75 KB
/
KnownType.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
package bridge.asm;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import java.util.Locale;
import java.util.Objects;
public class KnownType {
static final KnownType[] EMPTY = new KnownType[0];
public final Type type;
private Object search;
int access;
Object data;
KnownType extended;
KnownType[] implemented;
KnownType(Type anonymous) {
this.type = Objects.requireNonNull(anonymous);
}
KnownType(TypeMap types, Type type, Class<?> loaded) {
types.map.put(this.type = Objects.requireNonNull(type), this);
access = loaded.getModifiers();
final Class<?> extended;
if ((extended = loaded.getSuperclass()) != null) {
this.extended = types.get(extended);
} else if (loaded != Object.class && !loaded.isPrimitive()) {
this.extended = types.get(Object.class);
}
final Class<?>[] interfaces;
if ((interfaces = loaded.getInterfaces()).length != 0) {
final KnownType[] implemented = this.implemented = new KnownType[interfaces.length];
for (int i = 0, length = interfaces.length; i != length;) {
implemented[i] = types.get(interfaces[i++]);
}
} else {
this.implemented = EMPTY;
}
}
public int hashCode() {
return type.hashCode();
}
public boolean equals(Object type) {
if (this == type) {
return true;
} else if (type instanceof KnownType) {
return ((KnownType) type).type.equals(this.type);
} else if (type instanceof Type) {
return type.equals(this.type);
} else if (type instanceof Class) {
return Type.getType((Class<?>) type).equals(this.type);
} else if (type instanceof CharSequence) {
return type.equals(this.type.getDescriptor());
} else {
return false;
}
}
public Object data() {
return data;
}
public int modifiers() {
return access;
}
public KnownType supertype() {
return extended;
}
public boolean extended(KnownType type) {
if (type != null) {
KnownType extended = this;
do {
if (type == extended || type.type.equals(extended.type)) return true;
} while ((extended = extended.extended) != null);
}
return false;
}
public KnownType[] interfaces() {
return implemented.clone();
}
public boolean implemented(KnownType type) {
return type != null && implemented(type, new Object());
}
private boolean implemented(KnownType type, Object search) {
final KnownType extended;
if (type == this || type.type.equals(this.type)) return true;
if ((extended = this.extended) != null && extended.search != search) {
if (extended.implemented(type, search)) return true;
extended.search = search;
}
for (KnownType implemented : this.implemented) {
if (implemented.search != search) {
if (implemented.implemented(type, search)) return true;
implemented.search = search;
}
}
return false;
}
public boolean isPrimitive() {
return type.getSort() < Type.ARRAY;
}
public boolean isInterface() {
return (access & Opcodes.ACC_INTERFACE) != 0;
}
public boolean isArray() {
return false;
}
private static final int INDENT = 4;
private static StringBuilder newline(StringBuilder builder, int indent) {
builder.append('\n');
while (indent != 0) {
builder.append(' ');
--indent;
}
return builder;
}
private void toString(StringBuilder builder, int indent) {
builder.append(type.getClassName()).append(" 0x").append(Integer.toHexString(super.hashCode()).toUpperCase(Locale.ROOT)).append(" {");
indent += INDENT;
KnownType extended = this.extended;
if (extended != null) {
extended.toString(newline(builder, indent).append("extends "), indent);
}
KnownType[] implemented = this.implemented;
if (implemented.length != 0) {
newline(builder, indent).append("implements {");
implemented[0].toString(newline(builder, indent += INDENT), indent);
for (int i = 1, length = implemented.length; i != length;) {
implemented[i++].toString(newline(builder.append(','), indent), indent);
}
newline(builder, indent -= INDENT).append('}');
}
if (isArray()) {
((ArrayType) this).element.toString(newline(builder, indent).append("return "), indent);
}
Object data = this.data;
if (data != null) {
String string = data.toString();
Class<?> type = data.getClass();
String hash = Integer.toHexString(data.hashCode());
newline(builder, indent).append("continue ").append(type.getTypeName()).append(" 0x").append(hash.toUpperCase(Locale.ROOT)).append(" {");
if (string != null && !string.equals(type.getName() + '@' + hash)) {
StringBuilder indentation = new StringBuilder();
newline(indentation, indent + INDENT);
newline(builder.append(indentation).append(string.replace("\n", indentation.toString())), indent);
}
builder.append('}');
}
if (builder.codePointBefore(builder.length()) != '{')
newline(builder, indent - INDENT);
builder.append('}');
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
toString(builder, 0);
return builder.toString();
}
}