-
Notifications
You must be signed in to change notification settings - Fork 115
/
TypeCache.java
204 lines (159 loc) · 5.99 KB
/
TypeCache.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
/*
* TypeCache.java
*
* Copyright (c) 2012 Mike Strobel
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0.
* A copy of the license can be found in the License.html file at the root of this distribution.
* By using this source code in any fashion, you are agreeing to be bound by the terms of the
* Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*/
package com.strobel.reflection;
import com.strobel.core.Comparer;
import com.strobel.core.HashUtilities;
import com.strobel.util.TypeUtils;
import java.util.LinkedHashMap;
/**
* @author strobelm
*/
@SuppressWarnings("unchecked")
final class TypeCache {
private final LinkedHashMap<Key, Type<?>> _map = new LinkedHashMap<>();
private final LinkedHashMap<String, Type<?>> _definitionMap = new LinkedHashMap<>();
private final LinkedHashMap<Type<?>, Type<?>> _arrayMap = new LinkedHashMap<>();
public Key key(final Type<?> type) {
return key(type, TypeList.empty());
}
public Key key(final Type<?> type, final TypeList typeArguments) {
return new Key(type.isGenericType() ? type.getGenericTypeDefinition() : type, typeArguments);
}
public Type find(final Key key) {
return _map.get(key);
}
public <T> Type<T[]> getArrayType(final Type<T> elementType) {
Type<T[]> arrayType = (Type<T[]>) _arrayMap.get(elementType);
if (arrayType != null) {
return arrayType;
}
arrayType = elementType.createArrayType();
add(arrayType);
return arrayType;
}
public <T> Type<T> getGenericType(final Type<T> type, final TypeList typeArguments) {
final Key key = key(
type.isGenericType() ? type.getGenericTypeDefinition() : type,
typeArguments
);
Type genericType = _map.get(key);
if (genericType == null) {
genericType = new GenericType(
type.getGenericTypeDefinition(),
typeArguments
);
final Type existing = _map.put(key, genericType);
if (existing != null) {
return existing;
}
}
return genericType;
}
public <T> Type<T> find(final Class<T> clazz) {
return (Type<T>) _definitionMap.get(TypeUtils.getInternalName(clazz));
}
public int size() {
return _map.size();
}
public void put(final Key key, final Type type) {
final String descriptor = key.descriptor;
if (!_definitionMap.containsKey(descriptor)) {
if (type.isGenericType() && !type.isGenericTypeDefinition()) {
_definitionMap.put(descriptor, type.getGenericTypeDefinition());
}
else {
_definitionMap.put(descriptor, type);
}
}
if (type.isPrimitive() && !_definitionMap.containsKey(type.getName())) {
_definitionMap.put(type.getName(), type);
}
_map.put(key, type);
if (type.isArray()) {
final Type elementType = type.getElementType();
if (!_arrayMap.containsKey(elementType)) {
_arrayMap.put(elementType, type);
}
}
}
public void add(final Type type) {
final TypeList typeArguments;
if (type.isGenericType()) {
typeArguments = type.getTypeBindings().getBoundTypes();
}
else {
typeArguments = TypeList.empty();
}
put(key(type, typeArguments), type);
}
final static class Key {
private final String descriptor;
private final TypeList typeArguments;
private final int hashCode;
public Key(final Type<?> simpleType) {
this(simpleType, null);
}
public Key(final Type<?> type, final TypeList typeArguments) {
this.descriptor = type.getInternalName();
this.typeArguments = typeArguments;
int h = this.descriptor.hashCode();
if (typeArguments != null && !typeArguments.isEmpty()) {
for (final Type<?> argument : typeArguments) {
h = HashUtilities.combineHashCodes(h, argument.hashCode());
}
}
this.hashCode = h;
}
@Override
public final int hashCode() {
return this.hashCode;
}
@Override
public final boolean equals(final Object o) {
if (o == this) {
return true;
}
if (o == null || o.getClass() != getClass()) {
return false;
}
final Key other = (Key) o;
if (!this.descriptor.equals(other.descriptor)) {
return false;
}
final TypeList typeArguments = this.typeArguments;
final TypeList otherArguments = other.typeArguments;
if (typeArguments == null || typeArguments.isEmpty()) {
return otherArguments == null || otherArguments.isEmpty();
}
if (otherArguments == null || otherArguments.size() != typeArguments.size()) {
return false;
}
for (int i = 0, n = typeArguments.size(); i < n; ++i) {
final Type argument = typeArguments.get(i);
final Type otherArgument = otherArguments.get(i);
if (!Comparer.equals(argument, otherArgument)) {
return false;
}
}
return true;
}
@Override
public final String toString() {
return "Key{" +
"descriptor=" + descriptor +
", typeArguments=" + typeArguments +
", hashCode=" + hashCode +
'}';
}
}
}