-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathPlaceHistoryGeneratorContext.java
333 lines (287 loc) · 11.3 KB
/
PlaceHistoryGeneratorContext.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.place.rebind;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JParameterizedType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.place.shared.PlaceHistoryMapperWithFactory;
import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;
import com.google.gwt.place.shared.WithTokenizers;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeMap;
class PlaceHistoryGeneratorContext {
/**
* Creates a {@link PlaceHistoryGeneratorContext} for the given
* {@link PlaceHistoryMapper} sub-interface.
*
* @return a {@link PlaceHistoryGeneratorContext}, or <code>null</code> if the
* generator should not run (i.e. <code>interfaceName</code> is not an
* interface)
* @throws UnableToCompleteException if the type denoted by
* <code>interfaceName</code> cannot be found in
* <code>typeOracle</code>
*/
static PlaceHistoryGeneratorContext create(TreeLogger logger,
TypeOracle typeOracle, String interfaceName)
throws UnableToCompleteException {
JClassType stringType = requireType(typeOracle, String.class);
JClassType placeTokenizerType = requireType(typeOracle,
PlaceTokenizer.class);
JClassType placeHistoryMapperWithFactoryType = requireType(typeOracle,
PlaceHistoryMapperWithFactory.class);
JClassType factoryType;
JClassType interfaceType = typeOracle.findType(interfaceName);
if (interfaceType == null) {
logger.log(TreeLogger.ERROR, "Could not find requested typeName: "
+ interfaceName);
throw new UnableToCompleteException();
}
if (interfaceType.isInterface() == null) {
return null;
}
factoryType = findFactoryType(placeHistoryMapperWithFactoryType,
interfaceType);
String implName = interfaceType.getName().replace(".", "_") + "Impl";
return new PlaceHistoryGeneratorContext(logger, typeOracle, interfaceType,
factoryType, stringType, placeTokenizerType,
interfaceType.getPackage().getName(), implName);
}
private static JClassType findFactoryType(
JClassType placeHistoryMapperWithFactoryType, JClassType interfaceType) {
JClassType superInterfaces[] = interfaceType.getImplementedInterfaces();
for (JClassType superInterface : superInterfaces) {
JParameterizedType parameterizedType = superInterface.isParameterized();
if (parameterizedType != null
&& parameterizedType.getBaseType().equals(
placeHistoryMapperWithFactoryType)) {
return parameterizedType.getTypeArgs()[0];
}
}
return null;
}
private static JClassType requireType(TypeOracle typeOracle, Class<?> clazz) {
try {
return typeOracle.getType(clazz.getName());
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
}
final JClassType stringType;
final JClassType placeTokenizerType;
final TreeLogger logger;
final TypeOracle typeOracle;
final JClassType interfaceType;
final JClassType factoryType;
final String implName;
final String packageName;
/**
* All tokenizers, either as a {@link JMethod} for factory getters or as a
* {@link JClassType} for types that must be GWT.create()d, by prefix.
*/
private HashMap<String, Object> tokenizers;
/**
* All place types and the prefix of their associated tokenizer, ordered from
* most-derived to least-derived type (and falling back to the natural
* ordering of their names).
*/
private TreeMap<JClassType, String> placeTypes = new TreeMap<JClassType, String>(
new MostToLeastDerivedPlaceTypeComparator());
PlaceHistoryGeneratorContext(TreeLogger logger, TypeOracle typeOracle,
JClassType interfaceType, JClassType factoryType, JClassType stringType,
JClassType placeTokenizerType, String packageName, String implName) {
this.logger = logger;
this.typeOracle = typeOracle;
this.interfaceType = interfaceType;
this.factoryType = factoryType;
this.stringType = stringType;
this.placeTokenizerType = placeTokenizerType;
this.packageName = packageName;
this.implName = implName;
}
public Set<JClassType> getPlaceTypes() throws UnableToCompleteException {
ensureInitialized();
return placeTypes.keySet();
}
public String getPrefix(JClassType placeType)
throws UnableToCompleteException {
ensureInitialized();
return placeTypes.get(placeType);
}
public Set<String> getPrefixes() throws UnableToCompleteException {
ensureInitialized();
return tokenizers.keySet();
}
public JMethod getTokenizerGetter(String prefix)
throws UnableToCompleteException {
ensureInitialized();
Object tokenizerGetter = tokenizers.get(prefix);
if (tokenizerGetter instanceof JMethod) {
return (JMethod) tokenizerGetter;
}
return null;
}
public JClassType getTokenizerType(String prefix)
throws UnableToCompleteException {
ensureInitialized();
Object tokenizerType = tokenizers.get(prefix);
if (tokenizerType instanceof JClassType) {
return (JClassType) tokenizerType;
}
return null;
}
void ensureInitialized() throws UnableToCompleteException {
if (tokenizers == null) {
assert placeTypes.isEmpty();
tokenizers = new HashMap<String, Object>();
initTokenizerGetters();
initTokenizersWithoutGetters();
}
}
private void addPlaceTokenizer(Object tokenizerClassOrGetter, String prefix,
JClassType tokenizerType) throws UnableToCompleteException {
if (prefix.contains(":")) {
logger.log(TreeLogger.ERROR, String.format(
"Found place prefix \"%s\" containing separator char \":\", on %s",
prefix, getLogMessage(tokenizerClassOrGetter)));
throw new UnableToCompleteException();
}
if (tokenizers.containsKey(prefix)) {
logger.log(TreeLogger.ERROR, String.format(
"Found duplicate place prefix \"%s\" on %s, already seen on %s",
prefix, getLogMessage(tokenizerClassOrGetter),
getLogMessage(tokenizers.get(prefix))));
throw new UnableToCompleteException();
}
JClassType placeType = getPlaceTypeForTokenizerType(tokenizerType);
if (placeTypes.containsKey(placeType)) {
logger.log(
TreeLogger.ERROR,
String.format(
"Found duplicate tokenizer's place type \"%s\" on %s, already seen on %s",
placeType.getQualifiedSourceName(),
getLogMessage(tokenizerClassOrGetter),
getLogMessage(tokenizers.get(placeTypes.get(placeType)))));
throw new UnableToCompleteException();
}
tokenizers.put(prefix, tokenizerClassOrGetter);
placeTypes.put(placeType, prefix);
}
private String getLogMessage(Object methodOrClass) {
if (methodOrClass instanceof JMethod) {
JMethod method = (JMethod) methodOrClass;
return method.getEnclosingType().getQualifiedSourceName() + "#"
+ method.getName() + "()";
}
JClassType classType = (JClassType) methodOrClass;
return classType.getQualifiedSourceName();
}
private JClassType getPlaceTypeForTokenizerType(JClassType tokenizerType)
throws UnableToCompleteException {
JClassType rtn = placeTypeForInterfaces(tokenizerType.getFlattenedSupertypeHierarchy());
if (rtn == null) {
logger.log(TreeLogger.ERROR, "Found no Place type for "
+ tokenizerType.getQualifiedSourceName());
throw new UnableToCompleteException();
}
return rtn;
}
private String getPrefixForTokenizerGetter(JMethod method)
throws UnableToCompleteException {
Prefix annotation = method.getAnnotation(Prefix.class);
if (annotation != null) {
return annotation.value();
}
JClassType returnType = method.getReturnType().isClassOrInterface();
return getPrefixForTokenizerType(returnType);
}
private String getPrefixForTokenizerType(JClassType returnType)
throws UnableToCompleteException {
Prefix annotation;
annotation = returnType.getAnnotation(Prefix.class);
if (annotation != null) {
return annotation.value();
}
return getPlaceTypeForTokenizerType(returnType).getName();
}
private Set<JClassType> getWithTokenizerEntries() {
WithTokenizers annotation = interfaceType.getAnnotation(WithTokenizers.class);
if (annotation == null) {
return Collections.emptySet();
}
LinkedHashSet<JClassType> rtn = new LinkedHashSet<JClassType>();
for (Class<? extends PlaceTokenizer<?>> tokenizerClass : annotation.value()) {
JClassType tokenizerType = typeOracle.findType(tokenizerClass.getCanonicalName());
if (tokenizerType == null) {
logger.log(TreeLogger.ERROR, String.format(
"Error processing @%s, cannot find type %s",
WithTokenizers.class.getSimpleName(),
tokenizerClass.getCanonicalName()));
}
rtn.add(tokenizerType);
}
return rtn;
}
private void initTokenizerGetters() throws UnableToCompleteException {
if (factoryType != null) {
// TODO: include non-public methods that are nevertheless accessible
// to the interface (package-scoped);
// Add a isCallable(JClassType) method to JAbstractMethod?
for (JMethod method : factoryType.getInheritableMethods()) {
if (!method.isPublic()) {
continue;
}
if (method.getParameters().length > 0) {
continue;
}
JClassType returnType = method.getReturnType().isClassOrInterface();
if (returnType == null) {
continue;
}
if (!placeTokenizerType.isAssignableFrom(returnType)) {
continue;
}
addPlaceTokenizer(method, getPrefixForTokenizerGetter(method),
method.getReturnType().isClassOrInterface());
}
}
}
private void initTokenizersWithoutGetters() throws UnableToCompleteException {
for (JClassType tokenizerType : getWithTokenizerEntries()) {
addPlaceTokenizer(tokenizerType,
getPrefixForTokenizerType(tokenizerType), tokenizerType);
}
}
private JClassType placeTypeForInterfaces(Collection<? extends JClassType> interfaces) {
JClassType rtn = null;
for (JClassType i : interfaces) {
JParameterizedType parameterizedType = i.isParameterized();
if (parameterizedType != null
&& placeTokenizerType.equals(parameterizedType.getBaseType())) {
rtn = parameterizedType.getTypeArgs()[0];
}
}
return rtn;
}
}