|
19 | 19 | import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;
|
20 | 20 |
|
21 | 21 | import com.google.common.collect.ImmutableList;
|
22 |
| -import com.google.common.collect.ImmutableSet; |
23 |
| -import com.google.errorprone.RefactoringCollection.RefactoringResult; |
24 |
| -import com.google.errorprone.scanner.ErrorProneScannerTransformer; |
25 | 22 | import com.google.errorprone.scanner.ScannerSupplier;
|
26 | 23 | import com.sun.source.util.JavacTask;
|
27 |
| -import com.sun.source.util.TaskEvent; |
28 |
| -import com.sun.source.util.TaskEvent.Kind; |
29 |
| -import com.sun.source.util.TaskListener; |
30 | 24 | import com.sun.tools.javac.api.BasicJavacTask;
|
31 | 25 | import com.sun.tools.javac.api.JavacTool;
|
32 | 26 | import com.sun.tools.javac.util.Context;
|
33 | 27 | import com.sun.tools.javac.util.JavacMessages;
|
34 |
| -import com.sun.tools.javac.util.Log; |
35 |
| -import com.sun.tools.javac.util.Log.WriterKind; |
36 | 28 | import com.sun.tools.javac.util.Options;
|
37 | 29 | import java.io.InputStream;
|
38 | 30 | import java.io.OutputStream;
|
39 |
| -import java.io.PrintWriter; |
40 | 31 | import java.io.Writer;
|
41 | 32 | import java.nio.charset.Charset;
|
42 | 33 | import java.util.EnumSet;
|
@@ -94,9 +85,9 @@ static void addTaskListener(
|
94 | 85 | setupMessageBundle(context);
|
95 | 86 | RefactoringCollection[] refactoringCollection = {null};
|
96 | 87 | javacTask.addTaskListener(
|
97 |
| - createAnalyzer(scannerSupplier, errorProneOptions, context, refactoringCollection)); |
| 88 | + ErrorProneAnalyzer.createAnalyzer(scannerSupplier, errorProneOptions, context, refactoringCollection)); |
98 | 89 | if (refactoringCollection[0] != null) {
|
99 |
| - javacTask.addTaskListener(new RefactoringTask(context, refactoringCollection[0])); |
| 90 | + javacTask.addTaskListener(new ErrorProneAnalyzer.RefactoringTask(context, refactoringCollection[0])); |
100 | 91 | }
|
101 | 92 | }
|
102 | 93 |
|
@@ -206,71 +197,4 @@ public static void setupMessageBundle(Context context) {
|
206 | 197 | ResourceBundle bundle = ResourceBundle.getBundle("com.google.errorprone.errors");
|
207 | 198 | JavacMessages.instance(context).add(l -> bundle);
|
208 | 199 | }
|
209 |
| - |
210 |
| - static ErrorProneAnalyzer createAnalyzer( |
211 |
| - ScannerSupplier scannerSupplier, |
212 |
| - ErrorProneOptions epOptions, |
213 |
| - Context context, |
214 |
| - RefactoringCollection[] refactoringCollection) { |
215 |
| - if (!epOptions.patchingOptions().doRefactor()) { |
216 |
| - return ErrorProneAnalyzer.createByScanningForPlugins(scannerSupplier, epOptions, context); |
217 |
| - } |
218 |
| - refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context); |
219 |
| - |
220 |
| - // Refaster refactorer or using builtin checks |
221 |
| - CodeTransformer codeTransformer = |
222 |
| - epOptions |
223 |
| - .patchingOptions() |
224 |
| - .customRefactorer() |
225 |
| - .or( |
226 |
| - () -> { |
227 |
| - ScannerSupplier toUse = ErrorPronePlugins.loadPlugins(scannerSupplier, context); |
228 |
| - ImmutableSet<String> namedCheckers = epOptions.patchingOptions().namedCheckers(); |
229 |
| - if (!namedCheckers.isEmpty()) { |
230 |
| - toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName())); |
231 |
| - } else { |
232 |
| - toUse = toUse.applyOverrides(epOptions); |
233 |
| - } |
234 |
| - return ErrorProneScannerTransformer.create(toUse.get()); |
235 |
| - }) |
236 |
| - .get(); |
237 |
| - |
238 |
| - return ErrorProneAnalyzer.createWithCustomDescriptionListener( |
239 |
| - codeTransformer, epOptions, context, refactoringCollection[0]); |
240 |
| - } |
241 |
| - |
242 |
| - static class RefactoringTask implements TaskListener { |
243 |
| - |
244 |
| - private final Context context; |
245 |
| - private final RefactoringCollection refactoringCollection; |
246 |
| - |
247 |
| - public RefactoringTask(Context context, RefactoringCollection refactoringCollection) { |
248 |
| - this.context = context; |
249 |
| - this.refactoringCollection = refactoringCollection; |
250 |
| - } |
251 |
| - |
252 |
| - @Override |
253 |
| - public void started(TaskEvent event) {} |
254 |
| - |
255 |
| - @Override |
256 |
| - public void finished(TaskEvent event) { |
257 |
| - if (event.getKind() != Kind.GENERATE) { |
258 |
| - return; |
259 |
| - } |
260 |
| - RefactoringResult refactoringResult; |
261 |
| - try { |
262 |
| - refactoringResult = refactoringCollection.applyChanges(event.getSourceFile().toUri()); |
263 |
| - } catch (Exception e) { |
264 |
| - PrintWriter out = Log.instance(context).getWriter(WriterKind.ERROR); |
265 |
| - out.println(e.getMessage()); |
266 |
| - out.flush(); |
267 |
| - return; |
268 |
| - } |
269 |
| - if (refactoringResult.type() == RefactoringCollection.RefactoringResultType.CHANGED) { |
270 |
| - PrintWriter out = Log.instance(context).getWriter(WriterKind.NOTICE); |
271 |
| - out.println(refactoringResult.message()); |
272 |
| - out.flush(); |
273 |
| - } |
274 |
| - } |
275 |
| - } |
276 | 200 | }
|
0 commit comments