Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 7196760
27 * @summary javac doesn't report Diagnostic end positions properly when
28 * an annotation processor is present
29 */
31 import com.sun.source.util.JavacTask;
32 import java.io.IOException;
33 import java.net.URI;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Set;
38 import javax.annotation.processing.*;
39 import javax.lang.model.*;
40 import javax.lang.model.element.*;
41 import javax.tools.JavaCompiler;
42 import javax.tools.JavaFileObject;
43 import javax.tools.SimpleJavaFileObject;
44 import javax.tools.Diagnostic;
45 import javax.tools.DiagnosticCollector;
46 import static javax.tools.JavaFileObject.Kind.SOURCE;
47 import javax.tools.ToolProvider;
49 @SupportedAnnotationTypes("*")
50 public class EndPositions extends AbstractProcessor {
51 public static void main(String... args) throws IOException {
52 class MyFileObject extends SimpleJavaFileObject {
53 MyFileObject() {
54 super(URI.create("myfo:///Test.java"), SOURCE);
55 }
56 @Override
57 public String getCharContent(boolean ignoreEncodingErrors) {
58 // 0 1 2 3
59 // 012345678901234567890123456789012345
60 return "class Test { String s = 1234; }";
61 }
62 }
63 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
64 List<JavaFileObject> compilationUnits =
65 Collections.<JavaFileObject>singletonList(new MyFileObject());
66 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
67 List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName());
68 JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits);
69 boolean valid = task.call();
70 if (valid)
71 throw new AssertionError("Expected one error, but found none.");
73 List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics();
74 if (errors.size() != 1)
75 throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors);
77 Diagnostic<?> error = errors.get(0);
78 if (error.getStartPosition() >= error.getEndPosition())
79 throw new AssertionError("Expected start to be less than end position: start [" +
80 error.getStartPosition() + "], end [" + error.getEndPosition() +"]" +
81 "; diagnostics code: " + error.getCode());
83 System.out.println("All is good!");
84 }
86 @Override
87 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
88 return true;
89 }
91 @Override
92 public SourceVersion getSupportedSourceVersion() {
93 return SourceVersion.latest();
94 }
95 }