aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers/HandleDelegate.java
blob: 367b2cff65b411288eae8c4f3df48468a929a39e (plain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright (C) 2010-2014 The Project Lombok Authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package lombok.javac.handlers;

import static com.sun.tools.javac.code.Flags.*;
import static lombok.core.handlers.HandlerUtil.handleExperimentalFlagUsage;
import static lombok.javac.handlers.JavacHandlerUtil.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

import org.mangosdk.spi.ProviderFor;

import com.sun.tools.javac.code.Attribute.Compound;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.TypeVar;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.model.JavacTypes;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;

import lombok.ConfigurationKeys;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
import lombok.core.HandlerPriority;
import lombok.experimental.Delegate;
import lombok.javac.FindTypeVarScanner;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.JavacResolution;
import lombok.javac.JavacResolution.TypeNotConvertibleException;
import lombok.javac.JavacTreeMaker;
import lombok.javac.ResolutionResetNeeded;

@ProviderFor(JavacAnnotationHandler.class)
@HandlerPriority(HandleDelegate.HANDLE_DELEGATE_PRIORITY) //2^16; to make sure that we also delegate generated methods.
@ResolutionResetNeeded
public class HandleDelegate extends JavacAnnotationHandler<Delegate> {
	
	private static final List<String> METHODS_IN_OBJECT = Collections.unmodifiableList(Arrays.asList(
			"hashCode()",
			"canEqual(java.lang.Object)",  //Not in j.l.Object, but it goes with hashCode and equals so if we ignore those two, we should ignore this one.
			"equals(java.lang.Object)",
			"wait()",
			"wait(long)",
			"wait(long, int)",
			"notify()",
			"notifyAll()",
			"toString()",
			"getClass()",
			"clone()",
			"finalize()"));
	
	private static final String LEGALITY_OF_DELEGATE = "@Delegate is legal only on instance fields or no-argument instance methods.";
	private static final String RECURSION_NOT_ALLOWED = "@Delegate does not support recursion (delegating to a type that itself has @Delegate members). Member \"%s\" is @Delegate in type \"%s\"";
	public static final int HANDLE_DELEGATE_PRIORITY = 65536;

	
	@Override public void handle(AnnotationValues<Delegate> annotation, JCAnnotation ast, JavacNode annotationNode) {
		handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.DELEGATE_FLAG_USAGE, "@Delegate");
		
		@SuppressWarnings("deprecation") Class<? extends Annotation> oldDelegate = lombok.Delegate.class;
		deleteAnnotationIfNeccessary(annotationNode, Delegate.class, oldDelegate);
		
		Type delegateType;
		Name delegateName = annotationNode.toName(annotationNode.up().getName());
		DelegateReceiver delegateReceiver;
		JavacResolution reso = new JavacResolution(annotationNode.getContext());
		JCTree member = annotationNode.up().get();
		if (annotationNode.up().getKind() == Kind.FIELD) {
			if ((((JCVariableDecl) member).mods.flags & Flags.STATIC) != 0) {
				annotationNode.addError(LEGALITY_OF_DELEGATE);
				return;
			}
			delegateReceiver = DelegateReceiver.FIELD;
			delegateType = member.type;
			if (delegateType == null) reso.resolveClassMember(annotationNode.up());
			delegateType = member.type;
		} else if (annotationNode.up().getKind() == Kind.METHOD) {
			if (!(member instanceof JCMethodDecl)) {
				annotationNode.addError(LEGALITY_OF_DELEGATE);
				return;
			}
			JCMethodDecl methodDecl = (JCMethodDecl) member;
			if (!methodDecl.params.isEmpty() || (methodDecl.mods.flags & Flags.STATIC) != 0) {
				annotationNode.addError(LEGALITY_OF_DELEGATE);
				return;
			}
			delegateReceiver = DelegateReceiver.METHOD;
			delegateType = methodDecl.restype.type;
			if (delegateType == null) reso.resolveClassMember(annotationNode.up());
			delegateType = methodDecl.restype.type;
		} else {
			// As the annotation is legal on fields and methods only, javac itself will take care of printing an error message for this.
			return;
		}
		
		List<Object> delegateTypes = annotation.getActualExpressions("types");
		List<Object> excludeTypes = annotation.getActualExpressions("excludes");
		List<Type> toDelegate = new ArrayList<Type>();
		List<Type> toExclude = new ArrayList<Type>();
		
		if (delegateTypes.isEmpty()) {
			if (delegateType != null) toDelegate.add(delegateType); 
		} else {
			for (Object dt : delegateTypes) {
				if (dt instanceof JCFieldAccess && ((JCFieldAccess)dt).name.toString().equals("class")) {
					Type type = ((JCFieldAccess)dt).selected.type;
					if (type == null) reso.resolveClassMember(annotationNode);
					type = ((JCFieldAccess)dt).selected.type;
					if (type != null) toDelegate.add(type);
				}
			}
		}
		
		for (Object et : excludeTypes) {
			if (et instanceof JCFieldAccess && ((JCFieldAccess)et).name.toString().equals("class")) {
				Type type = ((JCFieldAccess)et).selected.type;
				if (type == null) reso.resolveClassMember(annotationNode);
				type = ((JCFieldAccess)et).selected.type;
				if (type != null) toExclude.add(type);
			}
		}
		
		List<MethodSig> signaturesToDelegate = new ArrayList<MethodSig>();
		List<MethodSig> signaturesToExclude = new ArrayList<MethodSig>();
		Set<String> banList = new HashSet<String>();
		banList.addAll(METHODS_IN_OBJECT);
		/* To exclude all methods in the class itself, try this:
		for (Symbol member : ((JCClassDecl)typeNode.get()).sym.getEnclosedElements()) {
			if (member instanceof MethodSymbol) {
				MethodSymbol method = (MethodSymbol) member;
				banList.add(printSig((ExecutableType) method.asType(), method.name, annotationNode.getTypesUtil()));
			}
		}
		 */
		
		try {
			for (Type t : toExclude) {
				if (t instanceof ClassType) {
					ClassType ct = (ClassType) t;
					addMethodBindings(signaturesToExclude, ct, annotationNode.getTypesUtil(), banList);
				} else {
					annotationNode.addError("@Delegate can only use concrete class types, not wildcards, arrays, type variables, or primitives.");
					return;
				}
			}
			
			for (MethodSig sig : signaturesToExclude) {
				banList.add(printSig(sig.type, sig.name, annotationNode.getTypesUtil()));
			}
			
			for (Type t : toDelegate) {
				Type unannotatedType = Unannotated.unannotatedType(t);
				if (unannotatedType instanceof ClassType) {
					ClassType ct = (ClassType) unannotatedType;
					addMethodBindings(signaturesToDelegate, ct, annotationNode.getTypesUtil(), banList);
				} else {
					annotationNode.addError("@Delegate can only use concrete class types, not wildcards, arrays, type variables, or primitives.");
					return;
				}
			}
			
			for (MethodSig sig : signaturesToDelegate) generateAndAdd(sig, annotationNode, delegateName, delegateReceiver);
		} catch (DelegateRecursion e) {
			annotationNode.addError(String.format(RECURSION_NOT_ALLOWED, e.member, e.type));
		}
	}
	
	public void generateAndAdd(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) {
		List<JCMethodDecl> toAdd = new ArrayList<JCMethodDecl>();
		try {
			toAdd.add(createDelegateMethod(sig, annotation, delegateName, delegateReceiver));
		} catch (TypeNotConvertibleException e) {
			annotation.addError("Can't create delegate method for " + sig.name + ": " + e.getMessage());
			return;
		} catch (CantMakeDelegates e) {
			annotation.addError("There's a conflict in the names of type parameters. Fix it by renaming the following type parameters of your class: " + e.conflicted);
			return;
		}
		
		for (JCMethodDecl method : toAdd) {
			injectMethod(annotation.up().up(), method);
		}
	}
	
	public static class CantMakeDelegates extends Exception {
		Set<String> conflicted;
	}
	
	/**
	 * There's a rare but problematic case if a delegate method has its own type variables, and the delegated type does too, and the method uses both.
	 * If for example the delegated type has {@code <E>}, and the method has {@code <T>}, but in our class we have a {@code <T>} at the class level, then we have two different
	 * type variables both named {@code T}. We detect this situation and error out asking the programmer to rename their type variable.
	 * 
	 * @throws CantMakeDelegates If there's a conflict. Conflict list is in ex.conflicted.
	 */
	public void checkConflictOfTypeVarNames(MethodSig sig, JavacNode annotation) throws CantMakeDelegates {
		// As first step, we check if there's a conflict between the delegate method's type vars and our own class.
		
		if (sig.elem.getTypeParameters().isEmpty()) return;
		Set<String> usedInOurType = new HashSet<String>();
		
		JavacNode enclosingType = annotation;
		while (enclosingType != null) {
			if (enclosingType.getKind() == Kind.TYPE) {
				List<JCTypeParameter> typarams = ((JCClassDecl)enclosingType.get()).typarams;
				if (typarams != null) for (JCTypeParameter param : typarams) {
					if (param.name != null) usedInOurType.add(param.name.toString());
				}
			}
			enclosingType = enclosingType.up();
		}
		
		Set<String> usedInMethodSig = new HashSet<String>();
		for (TypeParameterElement param : sig.elem.getTypeParameters()) {
			usedInMethodSig.add(param.getSimpleName().toString());
		}
		
		usedInMethodSig.retainAll(usedInOurType);
		if (usedInMethodSig.isEmpty()) return;
		
		// We might be delegating a List<T>, and we are making method <T> toArray(). A conflict is possible.
		// But only if the toArray method also uses type vars from its class, otherwise we're only shadowing,
		// which is okay as we'll add a @SuppressWarnings.
		FindTypeVarScanner scanner = new FindTypeVarScanner();
		sig.elem.asType().accept(scanner, null);
		Set<String> names = new HashSet<String>(scanner.getTypeVariables());
		names.removeAll(usedInMethodSig);
		if (!names.isEmpty()) {
			// We have a confirmed conflict. We could dig deeper as this may still be a false alarm, but its already an exceedingly rare case.
			CantMakeDelegates cmd = new CantMakeDelegates();
			cmd.conflicted = usedInMethodSig;
			throw cmd;
		}
	}
	
	public JCMethodDecl createDelegateMethod(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) throws TypeNotConvertibleException, CantMakeDelegates {
		/* public <T, U, ...> ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... {
		 *      (return) delegate.<T, U>methodName(name1, name2);
		 *  }
		 */
		
		checkConflictOfTypeVarNames(sig, annotation);
		
		JavacTreeMaker maker = annotation.getTreeMaker();
		
		com.sun.tools.javac.util.List<JCAnnotation> annotations;
		if (sig.isDeprecated) {
			annotations = com.sun.tools.javac.util.List.of(maker.Annotation(
					genJavaLangTypeRef(annotation, "Deprecated"),
					com.sun.tools.javac.util.List.<JCExpression>nil()));
		} else {
			annotations = com.sun.tools.javac.util.List.nil();
		}
		
		JCModifiers mods = maker.Modifiers(PUBLIC, annotations);
		JCExpression returnType = JavacResolution.typeToJCTree((Type) sig.type.getReturnType(), annotation.getAst(), true);
		boolean useReturn = sig.type.getReturnType().getKind() != TypeKind.VOID;
		ListBuffer<JCVariableDecl> params = sig.type.getParameterTypes().isEmpty() ? null : new ListBuffer<JCVariableDecl>();
		ListBuffer<JCExpression> args = sig.type.getParameterTypes().isEmpty() ? null : new ListBuffer<JCExpression>();
		ListBuffer<JCExpression> thrown = sig.type.getThrownTypes().isEmpty() ? null : new ListBuffer<JCExpression>();
		ListBuffer<JCTypeParameter> typeParams = sig.type.getTypeVariables().isEmpty() ? null : new ListBuffer<JCTypeParameter>();
		ListBuffer<JCExpression> typeArgs = sig.type.getTypeVariables().isEmpty() ? null : new ListBuffer<JCExpression>();
		Types types = Types.instance(annotation.getContext());
		
		for (TypeMirror param : sig.type.getTypeVariables()) {
			Name name = ((TypeVar) param).tsym.name;
			
			ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>();
			for (Type type : types.getBounds((TypeVar) param)) {
				bounds.append(JavacResolution.typeToJCTree(type, annotation.getAst(), true));
			}
			
			typeParams.append(maker.TypeParameter(name, bounds.toList()));
			typeArgs.append(maker.Ident(name));
		}
		
		for (TypeMirror ex : sig.type.getThrownTypes()) {
			thrown.append(JavacResolution.typeToJCTree((Type) ex, annotation.getAst(), true));
		}
		
		int idx = 0;
		String[] paramNames = sig.getParameterNames();
		boolean varargs = sig.elem.isVarArgs();
		for (TypeMirror param : sig.type.getParameterTypes()) {
			long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, annotation.getContext());
			JCModifiers paramMods = maker.Modifiers(flags);
			Name name = annotation.toName(paramNames[idx++]);
			if (varargs && idx == paramNames.length) {
				paramMods.flags |= VARARGS;
			}
			params.append(maker.VarDef(paramMods, name, JavacResolution.typeToJCTree((Type) param, annotation.getAst(), true), null));
			args.append(maker.Ident(name));
		}
		
		JCExpression delegateCall = maker.Apply(toList(typeArgs), maker.Select(delegateReceiver.get(annotation, delegateName), sig.name), toList(args));
		JCStatement body = useReturn ? maker.Return(delegateCall) : maker.Exec(delegateCall);
		JCBlock bodyBlock = maker.Block(0, com.sun.tools.javac.util.List.of(body));
		
		return recursiveSetGeneratedBy(maker.MethodDef(mods, sig.name, returnType, toList(typeParams), toList(params), toList(thrown), bodyBlock, null), annotation.get(), annotation.getContext());
	}
	
	public static <T> com.sun.tools.javac.util.List<T> toList(ListBuffer<T> collection) {
		return collection == null ? com.sun.tools.javac.util.List.<T>nil() : collection.toList();
	}
	
	private static class DelegateRecursion extends Throwable {
		final String type, member;
		
		public DelegateRecursion(String type, String member) {
			this.type = type;
			this.member = member;
		}
	}
	
	public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) throws DelegateRecursion {
		TypeSymbol tsym = ct.asElement();
		if (tsym == null) return;
		
		for (Symbol member : tsym.getEnclosedElements()) {
			for (Compound am : member.getAnnotationMirrors()) {
				String name = null;
				try {
					name = am.type.tsym.flatName().toString();
				} catch (Exception ignore) {}
				
				if ("lombok.Delegate".equals(name) || "lombok.experimental.Delegate".equals(name)) {
					throw new DelegateRecursion(ct.tsym.name.toString(), member.name.toString());
				}
			}
			if (member.getKind() != ElementKind.METHOD) continue;
			if (member.isStatic()) continue;
			if (member.isConstructor()) continue;
			ExecutableElement exElem = (ExecutableElement)member;
			if (!exElem.getModifiers().contains(Modifier.PUBLIC)) continue;
			ExecutableType methodType = (ExecutableType) types.asMemberOf(ct, member);
			String sig = printSig(methodType, member.name, types);
			if (!banList.add(sig)) continue; //If add returns false, it was already in there
			boolean isDeprecated = (member.flags() & DEPRECATED) != 0;
			signatures.add(new MethodSig(member.name, methodType, isDeprecated, exElem));
		}
		
		if (ct.supertype_field instanceof ClassType) addMethodBindings(signatures, (ClassType) ct.supertype_field, types, banList);
		if (ct.interfaces_field != null) for (Type iface : ct.interfaces_field) {
			if (iface instanceof ClassType) addMethodBindings(signatures, (ClassType) iface, types, banList);
		}
	}
	
	public static class MethodSig {
		final Name name;
		final ExecutableType type;
		final boolean isDeprecated;
		final ExecutableElement elem;
		
		MethodSig(Name name, ExecutableType type, boolean isDeprecated, ExecutableElement elem) {
			this.name = name;
			this.type = type;
			this.isDeprecated = isDeprecated;
			this.elem = elem;
		}
		
		String[] getParameterNames() {
			List<? extends VariableElement> paramList = elem.getParameters();
			String[] paramNames = new String[paramList.size()];
			for (int i = 0; i < paramNames.length; i++) {
				paramNames[i] = paramList.get(i).getSimpleName().toString();
			}
			return paramNames;
		}
		
		@Override public String toString() {
			return (isDeprecated ? "@Deprecated " : "") + name + " " + type;
		}
	}
	
	public static String printSig(ExecutableType method, Name name, JavacTypes types) {
		StringBuilder sb = new StringBuilder();
		sb.append(name.toString()).append("(");
		boolean first = true;
		for (TypeMirror param : method.getParameterTypes()) {
			if (!first) sb.append(", ");
			first = false;
			sb.append(typeBindingToSignature(param, types));
		}
		return sb.append(")").toString();
	}
	
	public static String typeBindingToSignature(TypeMirror binding, JavacTypes types) {
		binding = types.erasure(binding);
		return binding.toString();
	}
	
	public enum DelegateReceiver {
		METHOD {
			public JCExpression get(final JavacNode node, final Name name) {
				com.sun.tools.javac.util.List<JCExpression> nilExprs = com.sun.tools.javac.util.List.nil();
				final JavacTreeMaker maker = node.getTreeMaker();
				return maker.Apply(nilExprs, maker.Select(maker.Ident(node.toName("this")), name), nilExprs);
			}
		},
		FIELD {
			public JCExpression get(final JavacNode node, final Name name) {
				final JavacTreeMaker maker = node.getTreeMaker();
				return maker.Select(maker.Ident(node.toName("this")), name);
			}
		};
		
		public abstract JCExpression get(final JavacNode node, final Name name);
	}
	
	private static class Unannotated {
		private static final Method unannotated;
		
		static {
			Method m = null;
			try {
				m = Type.class.getDeclaredMethod("unannotatedType");
			} catch (Exception e) {/* ignore */}
			unannotated = m;
		}
		
		static Type unannotatedType(Type t) {
			if (unannotated == null) return t;
			try {
				return (Type) unannotated.invoke(t);
			} catch (Exception e) {
				return t;
			}
		}
	}
}