aboutsummaryrefslogtreecommitdiff
path: root/test/core/src/lombok/RunTestsViaDelombok.java
blob: adfb1fce6495c36c5f43935671f3b7470e8ca150 (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
/*
 * Copyright (C) 2009-2021 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;

import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAssign;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.TypeBoundKind;
import com.sun.tools.javac.tree.TreeScanner;

import lombok.delombok.Delombok;
import lombok.javac.CapturingDiagnosticListener;
import lombok.javac.CapturingDiagnosticListener.CompilerMessage;
import lombok.javac.Javac;

public class RunTestsViaDelombok extends AbstractRunTests {
	private Delombok delombok = new Delombok();
	
	@Override
	public boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, final File file, String encoding, Map<String, String> formatPreferences, int version, boolean checkPositions) throws Throwable {
		delombok.setVerbose(true);
		ChangedChecker cc = new ChangedChecker();
		delombok.setFeedback(cc.feedback);
		delombok.setForceProcess(true);
		delombok.setCharset(encoding == null ? "UTF-8" : encoding);
		delombok.setFormatPreferences(formatPreferences);
		
		delombok.setDiagnosticsListener(new CapturingDiagnosticListener(file, messages));
		
		if (checkPositions) delombok.addAdditionalAnnotationProcessor(new ValidatePositionProcessor(version));
		delombok.addAdditionalAnnotationProcessor(new ValidateTypesProcessor());
		delombok.addAdditionalAnnotationProcessor(new ValidateNoDuplicateTreeNodeProcessor());
		
		delombok.addFile(file.getAbsoluteFile().getParentFile(), file.getName());
		delombok.setSourcepath(file.getAbsoluteFile().getParent());
		String bcp = System.getProperty("delombok.bootclasspath");
		if (bcp != null) delombok.setBootclasspath(bcp);
		delombok.setWriter(result);
		Locale originalLocale = Locale.getDefault();
		try {
			Locale.setDefault(Locale.ENGLISH);
			delombok.delombok();
			return cc.isChanged();
		} finally {
			Locale.setDefault(originalLocale);
		}
	}
	
	public static class ValidatePositionProcessor extends TreeProcessor {
		private final int version;
		
		public ValidatePositionProcessor(int version) {
			this.version = version;
		}
		
		private String craftFailMsg(String problematicNode, Deque<JCTree> astContext) {
			StringBuilder msg = new StringBuilder(problematicNode).append(" position of node not set: ");
			for (JCTree t : astContext) {
				msg.append("\n  ").append(t.getClass().getSimpleName());
				String asStr = t.toString();
				if (asStr.length() < 80) msg.append(": ").append(asStr);
				else if (t instanceof JCClassDecl) msg.append(": ").append(((JCClassDecl) t).name);
				else if (t instanceof JCMethodDecl) msg.append(": ").append(((JCMethodDecl) t).name);
				else if (t instanceof JCVariableDecl) msg.append(": ").append(((JCVariableDecl) t).name);
			}
			return msg.append("\n-------").toString();
		}
		
		@Override void processCompilationUnit(final JCCompilationUnit unit) {
			final Deque<JCTree> astContext = new ArrayDeque<JCTree>();
			unit.accept(new TreeScanner() {
				@Override public void scan(JCTree tree) {
					if (tree == null) return;
					if (tree instanceof JCMethodDecl && (((JCMethodDecl) tree).mods.flags & Flags.GENERATEDCONSTR) != 0) return;
					astContext.push(tree);
					try {
						if (tree instanceof JCModifiers) return;
						
						if (!Javac.validateDocComment(unit, tree)) {
							fail("Start position of doc comment (" + Javac.getDocComment(unit, tree) + ") of " + tree + " not set");
						}
						
						boolean check = true;
						if (version < 8 && tree instanceof TypeBoundKind) {
							// TypeBoundKind works differently in java6, and as a consequence,
							// the position is not set properly.
							// Given status of j6/j7, not worth properly testing.
							check = false;
						}
						if (version < 8 && tree instanceof JCIdent) {
							// explicit `super()` invocations do not appear to have end pos in j6/7.
							if ("super".equals("" + ((JCIdent) tree).name)) check = false;
						}
						
						if (tree instanceof JCVariableDecl && (((JCVariableDecl) tree).mods.flags & Javac.GENERATED_MEMBER) != 0) return;
						
						if (check && tree.pos == -1) fail(craftFailMsg("Start", astContext));
						
						if (check && Javac.getEndPosition(tree, unit) == -1) {
							fail(craftFailMsg("End", astContext));
						}
					} finally {
						try {
							super.scan(tree);
						} finally {
							astContext.pop();
						}
					}
				}
				
				@Override public void visitMethodDef(JCMethodDecl tree) {
					super.visitMethodDef(tree);
				}
				
				@Override public void visitVarDef(JCVariableDecl tree) {
					if ((tree.mods.flags & Flags.ENUM) != 0) return;
					super.visitVarDef(tree);
				}
				
				@Override public void visitAnnotation(JCAnnotation tree) {
					scan(tree.annotationType);
					// Javac parser maps @Annotation("val") to @Annotation(value = "val") but does not add an end position for the new JCIdent...
					if (tree.args.length() == 1 && tree.args.head instanceof JCAssign && ((JCIdent)((JCAssign) tree.args.head).lhs).name.toString().equals("value")) {
						scan(((JCAssign) tree.args.head).rhs);
					} else {
						scan(tree.args);
					}
				}
			});
		}
	}
	
	public static class ValidateTypesProcessor extends TreeProcessor {
		@Override void processCompilationUnit(final JCCompilationUnit unit) {
			final Stack<JCTree> parents = new Stack<JCTree>();
			parents.add(unit);
			
			unit.accept(new TreeScanner() {
				private JCTree parent;
				@Override public void scan(JCTree tree) {
					parent = parents.peek();
					
					parents.push(tree);
					super.scan(tree);
					parents.pop();
				}
				
				@Override public void visitClassDef(JCClassDecl tree) {
					// Skip anonymous or local classes, they have no symbol
					if (!(parent instanceof JCClassDecl || parent instanceof JCCompilationUnit)) return;
					
					validateSymbol(tree, tree.sym);
					super.visitClassDef(tree);
				};

				@Override public void visitMethodDef(JCMethodDecl tree) {
					validateSymbol(tree, tree.sym);
					super.visitMethodDef(tree);
				}
				
				@Override public void visitVarDef(JCVariableDecl tree) {
					// Skip local variables
					if (!(parent instanceof JCClassDecl || parent instanceof JCMethodDecl)) return;
					
					validateSymbol(tree, tree.sym);
					super.visitVarDef(tree);
				}
				
				private void validateSymbol(JCTree tree, Symbol sym) {
					if (sym == null) {
						fail("Missing symbol for " + tree);
					}
					// Only classes have enclosed elements, skip everything else
					if (!sym.owner.getKind().isClass()) return;
					
					if (!sym.owner.getEnclosedElements().contains(sym)) {
						fail(tree + " not added to parent");
					}
				}
			});
		}
	}
	
	public static class ValidateNoDuplicateTreeNodeProcessor extends TreeProcessor {

		private String craftFailMsg(Collection<JCTree> astContext) {
			StringBuilder msg = new StringBuilder();
			for (JCTree t : astContext) {
				msg.append("\n  ").append(t.getClass().getSimpleName());
				String asStr = t.toString();
				if (asStr.length() < 80) msg.append(": ").append(asStr);
				else if (t instanceof JCClassDecl) msg.append(": ").append(((JCClassDecl) t).name);
				else if (t instanceof JCMethodDecl) msg.append(": ").append(((JCMethodDecl) t).name);
				else if (t instanceof JCVariableDecl) msg.append(": ").append(((JCVariableDecl) t).name);
			}
			return msg.append("\n-------").toString();
		}
		
		@Override
		void processCompilationUnit(JCCompilationUnit unit) {
			final Deque<JCTree> parents = new ArrayDeque<JCTree>();
			parents.add(unit);
			
			final Map<JCTree, List<JCTree>> knownTreeNode = new IdentityHashMap<JCTree, List<JCTree>>();
			
			unit.accept(new TreeScanner() {
				private JCTree parent;
				
				@Override
				public void scan(JCTree tree) {
					parent = parents.peek();
					
					if (tree == null) return;
					if (tree instanceof JCPrimitiveTypeTree) return;
					// javac generates duplicates for record members
					if (tree instanceof JCVariableDecl && (((JCVariableDecl) tree).mods.flags & Javac.GENERATED_MEMBER) != 0) return;
					
					List<JCTree> knownNodeContext = knownTreeNode.put(tree, new ArrayList<JCTree>(parents));
					if (knownNodeContext != null) {
						// javac generates two JCVariableDecl elements for 'int a, b;'
						if (parent instanceof JCVariableDecl) {
							if (tree instanceof JCModifiers) return;
							if (tree instanceof JCIdent) return;
						}
						
						fail("Node " + tree + " found twice:" + craftFailMsg(knownNodeContext) + craftFailMsg(parents));
					}
					
					parents.push(tree);
					super.scan(tree);
					parents.pop();
				}
				
				/**
				 * We always generate shallow copies for annotations
				 */
				@Override
				public void visitAnnotation(JCAnnotation tree) {
					return;
				}
			});
		}
		
	}
	
	public static abstract class TreeProcessor extends AbstractProcessor {
		private Trees trees;
		@Override public synchronized void init(ProcessingEnvironment processingEnv) {
			super.init(processingEnv);
			trees = Trees.instance(processingEnv);
		}
		
		@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
			for (Element element : roundEnv.getRootElements()) {
				JCCompilationUnit unit = toUnit(element);
				if (unit != null) {
					processCompilationUnit(unit);
				}
			}
			return false;
		}
		
		abstract void processCompilationUnit(JCCompilationUnit unit);
		
		@Override public Set<String> getSupportedAnnotationTypes() {
			return Collections.singleton("*");
		}
		
		@Override public SourceVersion getSupportedSourceVersion() {
			return SourceVersion.latest();
		}
		
		private JCCompilationUnit toUnit(Element element) {
			TreePath path = null;
			if (trees != null) {
				try {
					path = trees.getPath(element);
				} catch (NullPointerException ignore) {
					// Happens if a package-info.java doesn't contain a package declaration.
					// https://github.com/projectlombok/lombok/issues/2184
					// We can safely ignore those, since they do not need any processing
				}
			}
			if (path == null) return null;
			
			return (JCCompilationUnit) path.getCompilationUnit();
		}
	}

	static class ChangedChecker {
		private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		private final PrintStream feedback;
		
		ChangedChecker() throws UnsupportedEncodingException {
			feedback = new PrintStream(bytes, true, "UTF-8");
		}
		
		boolean isChanged() throws UnsupportedEncodingException {
			feedback.flush();
			return bytes.toString("UTF-8").endsWith("[delomboked]\n");
		}
	}
}