-
Notifications
You must be signed in to change notification settings - Fork 193
/
DelegateASTTransformation.java
459 lines (419 loc) · 23.4 KB
/
DelegateASTTransformation.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.codehaus.groovy.transform;
import groovy.lang.Delegate;
import groovy.lang.GroovyObject;
import groovy.lang.Lazy;
import groovy.lang.Reference;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.tools.BeanUtils;
import org.codehaus.groovy.ast.tools.GenericsUtils;
import org.codehaus.groovy.classgen.Verifier;
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
import static org.apache.groovy.util.BeanUtils.capitalize;
import static org.codehaus.groovy.ast.ClassHelper.make;
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllMethods;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getInterfacesAndSuperInterfaces;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName;
import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.ast.tools.GenericsUtils.addMethodGenerics;
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpecRecurse;
import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
import static org.codehaus.groovy.ast.tools.GenericsUtils.extractSuperClassGenerics;
/**
* Handles generation of code for the <code>@Delegate</code> annotation
*/
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
public class DelegateASTTransformation extends AbstractASTTransformation {
private static final Class MY_CLASS = Delegate.class;
private static final ClassNode MY_TYPE = make(MY_CLASS);
private static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage();
private static final ClassNode DEPRECATED_TYPE = make(Deprecated.class);
private static final ClassNode GROOVYOBJECT_TYPE = make(GroovyObject.class);
private static final ClassNode LAZY_TYPE = make(Lazy.class);
private static final String MEMBER_DEPRECATED = "deprecated";
private static final String MEMBER_INTERFACES = "interfaces";
private static final String MEMBER_INCLUDES = "includes";
private static final String MEMBER_EXCLUDES = "excludes";
private static final String MEMBER_INCLUDE_TYPES = "includeTypes";
private static final String MEMBER_EXCLUDE_TYPES = "excludeTypes";
private static final String MEMBER_PARAMETER_ANNOTATIONS = "parameterAnnotations";
private static final String MEMBER_METHOD_ANNOTATIONS = "methodAnnotations";
private static final String MEMBER_ALL_NAMES = "allNames";
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
DelegateDescription delegate = null;
if (parent instanceof FieldNode) {
FieldNode fieldNode = (FieldNode) parent;
delegate = new DelegateDescription();
delegate.delegate = fieldNode;
delegate.annotation = node;
delegate.name = fieldNode.getName();
delegate.type = fieldNode.getType();
delegate.owner = fieldNode.getOwner();
delegate.getOp = varX(fieldNode);
delegate.origin = "field";
} else if (parent instanceof MethodNode) {
MethodNode methodNode = (MethodNode) parent;
delegate = new DelegateDescription();
delegate.delegate = methodNode;
delegate.annotation = node;
delegate.name = methodNode.getName();
delegate.type = methodNode.getReturnType();
delegate.owner = methodNode.getDeclaringClass();
delegate.getOp = callThisX(delegate.name);
delegate.origin = "method";
if (methodNode.getParameters().length > 0) {
addError("You can only delegate to methods that take no parameters, but " +
delegate.name + " takes " + methodNode.getParameters().length +
" parameters.", parent);
return;
}
}
if (delegate != null) {
if (delegate.type.equals(ClassHelper.OBJECT_TYPE) || delegate.type.equals(GROOVYOBJECT_TYPE)) {
addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() +
". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent);
return;
}
if (delegate.type.equals(delegate.owner)) {
addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() +
". Delegation to own type not supported. Please use a different type.", parent);
return;
}
final List<MethodNode> delegateMethods = new java.util.LinkedList<>(getAllMethods(delegate.type));
// GRECLIPSE add -- GROOVY-4320, GROOVY-4516
for (java.util.ListIterator<MethodNode> it = delegateMethods.listIterator(); it.hasNext();) {
MethodNode next = it.next();
if (next.isPublic() && !next.isAbstract() && next.hasDefaultValue()) {
int n = 0;
for (Parameter p : next.getParameters())
if (p.hasInitialExpression()) n += 1;
for (int i = 1; i <= n; i += 1) { // from Verifier#addDefaultParameters
Parameter[] params = new Parameter[next.getParameters().length - i];
int index = 0, j = 1;
for (Parameter parameter : next.getParameters()) {
if (j > n - i && parameter.hasInitialExpression()) {
j += 1;
} else {
params[index++] = parameter;
if (parameter.hasInitialExpression()) j += 1;
}
}
if (delegateMethods.stream().noneMatch(mn -> mn.getName().equals(next.getName()) && org.codehaus.groovy.ast.tools.ParameterUtils.parametersEqual(mn.getParameters(), params))) {
MethodNode mn = new MethodNode(next.getName(), next.getModifiers(), next.getReturnType(), params, next.getExceptions(), null);
mn.setDeclaringClass(next.getDeclaringClass());
mn.setGenericsTypes(next.getGenericsTypes());
mn.addAnnotations(next.getAnnotations());
it.add(mn);
}
}
}
}
// GRECLIPSE end
for (ClassNode next : delegate.type.getAllInterfaces()) {
delegateMethods.addAll(getAllMethods(next));
}
final boolean skipInterfaces = memberHasValue(node, MEMBER_INTERFACES, false);
final boolean includeDeprecated = memberHasValue(node, MEMBER_DEPRECATED, true) || (delegate.type.isInterface() && !skipInterfaces);
final boolean allNames = memberHasValue(node, MEMBER_ALL_NAMES, true);
delegate.excludes = getMemberStringList(node, MEMBER_EXCLUDES);
delegate.includes = getMemberStringList(node, MEMBER_INCLUDES);
delegate.excludeTypes = getMemberClassList(node, MEMBER_EXCLUDE_TYPES);
delegate.includeTypes = getMemberClassList(node, MEMBER_INCLUDE_TYPES);
checkIncludeExcludeUndefinedAware(node, delegate.excludes, delegate.includes,
delegate.excludeTypes, delegate.includeTypes, MY_TYPE_NAME);
if (!checkPropertyOrMethodList(delegate.type, delegate.includes, "includes", node, MY_TYPE_NAME)) return;
if (!checkPropertyOrMethodList(delegate.type, delegate.excludes, "excludes", node, MY_TYPE_NAME)) return;
final List<MethodNode> ownerMethods = getAllMethods(delegate.owner);
for (MethodNode mn : delegateMethods) {
addDelegateMethod(delegate, ownerMethods, mn, includeDeprecated, allNames);
}
for (PropertyNode prop : getAllProperties(delegate.type)) {
if (prop.isStatic() || !prop.isPublic())
continue;
String name = prop.getName();
addGetterIfNeeded(delegate, prop, name, allNames);
addSetterIfNeeded(delegate, prop, name, allNames);
}
if (delegate.type.isArray()) {
boolean skipLength = delegate.excludes != null && (delegate.excludes.contains("length") || delegate.excludes.contains("getLength"));
if (!skipLength) {
addGeneratedMethod(delegate.owner, "getLength",
ACC_PUBLIC,
ClassHelper.int_TYPE,
Parameter.EMPTY_ARRAY,
null,
returnS(propX(delegate.getOp, "length")));
}
}
if (skipInterfaces) return;
final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(delegate.type);
final Set<ClassNode> ownerIfaces = delegate.owner.getAllInterfaces();
Map<String,ClassNode> genericsSpec = createGenericsSpec(delegate.owner);
genericsSpec = createGenericsSpec(delegate.type, genericsSpec);
for (ClassNode iface : allInterfaces) {
if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
final ClassNode[] ifaces = delegate.owner.getInterfaces();
final ClassNode[] newIfaces = new ClassNode[ifaces.length + 1];
for (int i = 0; i < ifaces.length; i++) {
newIfaces[i] = correctToGenericsSpecRecurse(genericsSpec, ifaces[i]);
}
newIfaces[ifaces.length] = correctToGenericsSpecRecurse(genericsSpec, iface);
delegate.owner.setInterfaces(newIfaces);
}
}
}
}
private boolean checkPropertyOrMethodList(ClassNode cNode, List<String> propertyNameList, String listName, AnnotationNode anno, String typeName) {
if (propertyNameList == null || propertyNameList.isEmpty()) {
return true;
}
final Set<String> pNames = new HashSet<>();
final Set<String> mNames = new HashSet<>();
for (PropertyNode pNode : BeanUtils.getAllProperties(cNode, false, false, false)) {
String name = pNode.getField().getName();
pNames.add(name);
// add getter/setters since Groovy compiler hasn't added property accessors yet
if ((pNode.getModifiers() & ACC_FINAL) == 0) {
mNames.add(getSetterName(name));
}
String capitalized = capitalize(name);
mNames.add("get" + capitalized);
boolean isPrimBool = pNode.getOriginType().equals(ClassHelper.boolean_TYPE);
if (isPrimBool) {
mNames.add("is" + capitalized);
}
}
for (MethodNode mNode : cNode.getAllDeclaredMethods()) {
mNames.add(mNode.getName());
}
boolean result = true;
for (String name : propertyNameList) {
if (!pNames.contains(name) && !mNames.contains(name)) {
addError("Error during " + typeName + " processing: '" + listName + "' property or method '" + name + "' does not exist.", anno);
result = false;
}
}
return result;
}
private static void addSetterIfNeeded(DelegateDescription delegate, PropertyNode prop, String name, boolean allNames) {
String setterName = "set" + Verifier.capitalize(name);
if ((prop.getModifiers() & ACC_FINAL) == 0
&& delegate.owner.getSetterMethod(setterName) == null && delegate.owner.getProperty(name) == null
&& !shouldSkipPropertyMethod(name, setterName, delegate.excludes, delegate.includes, allNames)) {
addGeneratedMethod(delegate.owner, setterName,
ACC_PUBLIC,
ClassHelper.VOID_TYPE,
params(new Parameter(GenericsUtils.nonGeneric(prop.getType()), "value")),
null,
assignS(propX(delegate.getOp, name), varX("value"))
);
}
}
private static void addGetterIfNeeded(DelegateDescription delegate, PropertyNode prop, String name, boolean allNames) {
boolean isPrimBool = prop.getOriginType().equals(ClassHelper.boolean_TYPE);
// do a little bit of pre-work since Groovy compiler hasn't added property accessors yet
boolean willHaveGetAccessor = true;
boolean willHaveIsAccessor = isPrimBool;
String suffix = Verifier.capitalize(name);
if (isPrimBool) {
ClassNode cNode = prop.getDeclaringClass();
if (cNode.getGetterMethod("is" + suffix) != null && cNode.getGetterMethod("get" + suffix) == null)
willHaveGetAccessor = false;
if (cNode.getGetterMethod("get" + suffix) != null && cNode.getGetterMethod("is" + suffix) == null)
willHaveIsAccessor = false;
}
Reference<Boolean> ownerWillHaveGetAccessor = new Reference<Boolean>();
Reference<Boolean> ownerWillHaveIsAccessor = new Reference<Boolean>();
extractAccessorInfo(delegate.owner, name, ownerWillHaveGetAccessor, ownerWillHaveIsAccessor);
for (String prefix : new String[]{"get", "is"}) {
String getterName = prefix + suffix;
if ((prefix.equals("get") && willHaveGetAccessor && !ownerWillHaveGetAccessor.get()
|| prefix.equals("is") && willHaveIsAccessor && !ownerWillHaveIsAccessor.get())
&& !shouldSkipPropertyMethod(name, getterName, delegate.excludes, delegate.includes, allNames)) {
addGeneratedMethod(delegate.owner, getterName,
ACC_PUBLIC,
GenericsUtils.nonGeneric(prop.getType()),
Parameter.EMPTY_ARRAY,
null,
returnS(propX(delegate.getOp, name)));
}
}
}
private static void extractAccessorInfo(ClassNode owner, String name, Reference<Boolean> willHaveGetAccessor, Reference<Boolean> willHaveIsAccessor) {
String suffix = Verifier.capitalize(name);
boolean hasGetAccessor = owner.getGetterMethod("get" + suffix) != null;
boolean hasIsAccessor = owner.getGetterMethod("is" + suffix) != null;
PropertyNode prop = owner.getProperty(name);
willHaveGetAccessor.set(hasGetAccessor || (prop != null && !hasIsAccessor));
willHaveIsAccessor.set(hasIsAccessor || (prop != null && !hasGetAccessor && prop.getOriginType().equals(ClassHelper.boolean_TYPE)));
}
private static boolean shouldSkipPropertyMethod(String propertyName, String methodName, List<String> excludes, List<String> includes, boolean allNames) {
return ((!allNames && deemedInternalName(propertyName))
|| excludes != null && (excludes.contains(propertyName) || excludes.contains(methodName))
|| (includes != null && !includes.isEmpty() && !includes.contains(propertyName) && !includes.contains(methodName)));
}
private void addDelegateMethod(DelegateDescription delegate, List<MethodNode> ownMethods, MethodNode candidate, boolean includeDeprecated, boolean allNames) {
if (!candidate.isPublic() || candidate.isStatic() || 0 != (candidate.getModifiers () & ACC_SYNTHETIC))
return;
if (!candidate.getAnnotations(DEPRECATED_TYPE).isEmpty() && !includeDeprecated)
return;
if (shouldSkip(candidate.getName(), delegate.excludes, delegate.includes, allNames)) return;
Map<String,ClassNode> genericsSpec = createGenericsSpec(delegate.owner);
genericsSpec = addMethodGenerics(candidate, genericsSpec);
extractSuperClassGenerics(delegate.type, candidate.getDeclaringClass(), genericsSpec);
if ((delegate.excludeTypes != null && !delegate.excludeTypes.isEmpty()) || delegate.includeTypes != null) {
MethodNode correctedMethodNode = correctToGenericsSpec(genericsSpec, candidate);
boolean checkReturn = delegate.type.getMethods().contains(candidate);
if (shouldSkipOnDescriptorUndefinedAware(checkReturn, genericsSpec, correctedMethodNode, delegate.excludeTypes, delegate.includeTypes))
return;
}
// ignore methods from GroovyObject
for (MethodNode mn : GROOVYOBJECT_TYPE.getMethods()) {
if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor())) {
return;
}
}
// ignore methods already in owner
for (MethodNode mn : delegate.owner.getMethods()) {
if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor())) {
return;
}
}
// give precedence to methods of self (but not abstract or static superclass methods)
// also allows abstract or static self methods to be selected for overriding but they are ignored later
MethodNode existingNode = null;
for (MethodNode mn : ownMethods) {
if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor()) && !mn.isAbstract() && !mn.isStatic()) {
existingNode = mn;
break;
}
}
if (existingNode == null || existingNode.getCode() == null) {
final ArgumentListExpression args = new ArgumentListExpression();
final Parameter[] params = candidate.getParameters();
final Parameter[] newParams = new Parameter[params.length];
List<String> currentMethodGenPlaceholders = genericPlaceholderNames(candidate);
for (int i = 0; i < newParams.length; i++) {
ClassNode newParamType = correctToGenericsSpecRecurse(genericsSpec, params[i].getType(), currentMethodGenPlaceholders);
Parameter newParam = new Parameter(newParamType, getParamName(params, i, delegate.name));
/* GRECLIPSE edit -- GROOVY-4320, GROOVY-4516
newParam.setInitialExpression(params[i].getInitialExpression());
*/
if (memberHasValue(delegate.annotation, MEMBER_PARAMETER_ANNOTATIONS, true)) {
newParam.addAnnotations(copyAnnotatedNodeAnnotations(params[i], MY_TYPE_NAME));
}
newParams[i] = newParam;
args.addExpression(varX(newParam));
}
boolean alsoLazy = !delegate.delegate.getAnnotations(LAZY_TYPE).isEmpty();
// addMethod will ignore attempts to override abstract or static methods with same signature on self
MethodCallExpression mce = callX(
// use propX when lazy, because lazy is only allowed on fields/properties
alsoLazy ? propX(varX("this"), delegate.name.substring(1)) : delegate.getOp,
candidate.getName(),
args);
mce.setImplicitThis(false); // GROOVY-9938
mce.setSourcePosition(delegate.delegate); // GROOVY-6542
ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, candidate.getReturnType(), currentMethodGenPlaceholders);
MethodNode newMethod = addGeneratedMethod(delegate.owner, candidate.getName(),
candidate.getModifiers() & (~ACC_ABSTRACT) & (~ACC_NATIVE),
returnType,
newParams,
candidate.getExceptions(),
// GRECLIPSE add
!candidate.isVoidMethod() ? returnS(mce) :
// GRECLIPSE end
stmt(mce));
newMethod.setGenericsTypes(candidate.getGenericsTypes());
if (memberHasValue(delegate.annotation, MEMBER_METHOD_ANNOTATIONS, true)) {
newMethod.addAnnotations(copyAnnotatedNodeAnnotations(candidate, MY_TYPE_NAME, false));
}
}
}
private static List<String> genericPlaceholderNames(MethodNode candidate) {
GenericsType[] candidateGenericsTypes = candidate.getGenericsTypes();
List<String> names = new ArrayList<String>();
if (candidateGenericsTypes != null) {
for (GenericsType gt : candidateGenericsTypes) {
names.add(gt.getName());
}
}
return names;
}
private static String getParamName(Parameter[] params, int i, String fieldName) {
String name = params[i].getName();
while(name.equals(fieldName) || clashesWithOtherParams(name, params, i)) {
name = "_" + name;
}
return name;
}
private static boolean clashesWithOtherParams(String name, Parameter[] params, int i) {
for (int j = 0; j < params.length; j++) {
if (i == j) continue;
if (params[j].getName().equals(name)) return true;
}
return false;
}
static class DelegateDescription {
AnnotationNode annotation;
AnnotatedNode delegate;
String name;
ClassNode type;
ClassNode owner;
Expression getOp;
String origin;
List<String> includes;
List<String> excludes;
List<ClassNode> includeTypes;
List<ClassNode> excludeTypes;
}
}