-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnownType.java
152 lines (128 loc) · 4.72 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
package bridge.asm;
import org.objectweb.asm.Type;
import java.util.*;
public class KnownType {
protected static final KnownType[] EMPTY = new KnownType[0];
private Object search;
public final Type type;
protected KnownType extended;
protected KnownType[] implemented;
protected boolean isInterface;
protected KnownType(Type anonymous) {
this.type = Objects.requireNonNull(anonymous);
}
KnownType(TypeMap types, Type type, Class<?> loaded) {
this.type = Objects.requireNonNull(type);
this.isInterface = loaded.isInterface();
types.map.put(type, this);
final Class<?> extended = loaded.getSuperclass();
if (extended != null) {
this.extended = types.get(extended);
} else if (loaded != Object.class && type.getSort() == Type.OBJECT) {
this.extended = types.get(Object.class);
}
final Class<?>[] interfaces = loaded.getInterfaces();
if (interfaces.length != 0) {
final KnownType[] implemented = new KnownType[interfaces.length];
for (int i = 0; i < interfaces.length; ++i) {
implemented[i] = types.get(interfaces[i]);
}
this.implemented = implemented;
} else {
this.implemented = EMPTY;
}
}
public int hashCode() {
return type.hashCode();
}
public boolean equals(Object type) {
if (this == type) {
return true;
} else if (type instanceof Type) {
return type.equals(this.type);
} else if (type instanceof KnownType) {
return ((KnownType) type).type.equals(this.type);
} else if (type instanceof Class) {
return Type.getType((Class<?>) type).equals(this.type);
} else {
return false;
}
}
public KnownType supertype() {
return extended;
}
public boolean extended(KnownType type) {
final KnownType extended;
return type != null && (
type == this || type.type.equals(this.type) || ((extended = this.extended) != null && extended.extended(type))
);
}
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 isInterface;
}
public boolean isArray() {
return false;
}
private static final int INDENT = 4;
private 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; i < implemented.length; ++i) {
implemented[i].toString(newline(builder.append(','), indent), indent);
}
newline(builder, indent -= INDENT).append('}');
}
if (this instanceof ArrayType) {
((ArrayType) this).element.toString(newline(builder, indent).append("return "), indent);
}
if (builder.charAt(builder.length() - 1) != '{')
newline(builder, indent - INDENT);
builder.append('}');
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
toString(builder, 0);
return builder.toString();
}
}