aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/HandlerLibrary.java
blob: 6f33003f9b452e0d14212a824540ecaaae76b3d9 (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
package lombok.javac;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;

import lombok.core.SpiLoadUtil;


public class HandlerLibrary {
	private final Map<String, AnnotationHandlerContainer<?>> annotationHandlers = new HashMap<String, AnnotationHandlerContainer<?>>();
//	private final Collection<JavacASTVisitor> visitorHandlers = new ArrayList<JavacASTVisitor>();
	
	private static class AnnotationHandlerContainer<T extends Annotation> {
		private JavacAnnotationHandler<T> handler;
		private Class<T> annotationClass;
		
		AnnotationHandlerContainer(JavacAnnotationHandler<T> handler, Class<T> annotationClass) {
			this.handler = handler;
			this.annotationClass = annotationClass;
		}
		
		@SuppressWarnings("unchecked")
		public void handle(JavacNode node, Object annInstance) {
			handler.handle(node, (T) annInstance);
		}
	}
	
	public static HandlerLibrary load(Messager messager) {
		HandlerLibrary library = new HandlerLibrary();
		loadAnnotationHandlers(messager, library);
		return library;
	}
	
	@SuppressWarnings("unchecked")
	private static void loadAnnotationHandlers(Messager messager, HandlerLibrary lib) {
		//No, that seemingly superfluous reference to JavacAnnotationHandler's classloader is not in fact superfluous!
		Iterator<JavacAnnotationHandler> it = ServiceLoader.load(JavacAnnotationHandler.class,
				JavacAnnotationHandler.class.getClassLoader()).iterator();
		while ( it.hasNext() ) {
			try {
				JavacAnnotationHandler<?> handler = it.next();
				Class<? extends Annotation> annotationClass =
					SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class);
				AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass);
				if ( lib.annotationHandlers.put(container.annotationClass.getName(), container) != null ) {
					messager.printMessage(Diagnostic.Kind.WARNING,
							"Duplicate handlers for annotation type: " + container.annotationClass.getName());
				}
			} catch ( ServiceConfigurationError e ) {
				messager.printMessage(Diagnostic.Kind.WARNING,
						"Can't load Lombok annotation handler for javac: " + e);
			}
		}
	}
	
	public void handleAnnotation(JavacNode node, TypeElement annotationType) {
		AnnotationHandlerContainer<?> container = annotationHandlers.get(annotationType.getQualifiedName().toString());
		if ( container == null ) return;
		try {
			container.handle(node, createAnnotation(container.annotationClass, annotationType.getQualifiedName(), node));
		} catch ( AnnotationValueDecodeFail e ) {
			node.addError(e.getMessage(), e.mirror, e.value);
		}
	}
	
	private Object createAnnotation(Class<? extends Annotation> target, Name annotationName, JavacNode node)
			throws AnnotationValueDecodeFail {
		AnnotationMirror mirror = fetchMirror(annotationName, node);
		if ( mirror == null ) throw new AssertionError("This can't be.");
		
		InvocationHandler invocations = new AnnotationMirrorInvocationHandler(target, mirror);
		return Proxy.newProxyInstance(target.getClassLoader(), new Class[] { target }, invocations);
	}
	
	private static class AnnotationValueDecodeFail extends Exception {
		private static final long serialVersionUID = 1L;
		
		AnnotationMirror mirror;
		AnnotationValue value;
		
		AnnotationValueDecodeFail(String msg, AnnotationMirror mirror, AnnotationValue value) {
			super(msg);
			this.mirror = mirror;
			this.value = value;
		}
	}
	
	private static class AnnotationMirrorInvocationHandler implements InvocationHandler {
		private final AnnotationMirror mirror;
		private final Map<String, Object> values = new HashMap<String, Object>();
		
		AnnotationMirrorInvocationHandler(Class<?> target, AnnotationMirror mirror) throws AnnotationValueDecodeFail {
			this.mirror = mirror;
			
			for ( Method m : target.getDeclaredMethods() ) {
				if ( !Modifier.isPublic(m.getModifiers()) ) continue;
				values.put(m.getName(), decode(m));
			}
		}
		
		private Object decode(Method m) throws AnnotationValueDecodeFail {
			for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
				mirror.getElementValues().entrySet() ) {
				
				if ( entry.getKey().getSimpleName().contentEquals(m.getName()) ) {
					AnnotationValue value = entry.getValue();
					return convert(m.getReturnType(), mirror, value, value.getValue());
				}
			}
			
			return m.getDefaultValue();
		}
		
		@Override public Object invoke(Object proxy, Method method, Object[] args) {
			return values.get(method.getName());
		}
		
		private Object convert(Class<?> expected, AnnotationMirror mirror, AnnotationValue value, Object v) throws AnnotationValueDecodeFail {
			if ( expected == int.class ) {
				if ( v instanceof Number ) return ((Number)v).intValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == long.class ) {
				if ( v instanceof Number ) return ((Number)v).longValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == short.class ) {
				if ( v instanceof Number ) return ((Number)v).shortValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == byte.class ) {
				if ( v instanceof Number ) return ((Number)v).byteValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == double.class ) {
				if ( v instanceof Number ) return ((Number)v).doubleValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == float.class ) {
				if ( v instanceof Number ) return ((Number)v).floatValue();
				else throw new AnnotationValueDecodeFail("Expected a numeric value here", mirror, value);
			} else if ( expected == char.class ) {
				if ( v instanceof Character ) return v;
				else throw new AnnotationValueDecodeFail("Expected a character here", mirror, value);
			} else if ( expected == boolean.class ) {
				if ( v instanceof Boolean ) return v;
				else throw new AnnotationValueDecodeFail("Expected a boolean here", mirror, value);
			} else if ( expected == String.class ) {
				if ( v instanceof String ) return v;
				else throw new AnnotationValueDecodeFail("Expected a String here", mirror, value);
			} else if ( expected == Class.class ) {
				if ( v instanceof TypeMirror ) {
					try {
						return Class.forName(v.toString());
					} catch ( ClassNotFoundException e ) {
						throw new AnnotationValueDecodeFail(
								"I can't find this class. Lombok only works well with types in the core java libraries.",
								mirror, value);
					}
				} else throw new AnnotationValueDecodeFail("Expected a class literal here", mirror, value);
			} else if ( Enum.class.isAssignableFrom(