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