jjg@1323: /* jjg@1521: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1323: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1323: * jjg@1323: * This code is free software; you can redistribute it and/or modify it jjg@1323: * under the terms of the GNU General Public License version 2 only, as jjg@1323: * published by the Free Software Foundation. jjg@1323: * jjg@1323: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1323: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1323: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1323: * version 2 for more details (a copy is included in the LICENSE file that jjg@1323: * accompanied this code). jjg@1323: * jjg@1323: * You should have received a copy of the GNU General Public License version jjg@1323: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1323: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1323: * jjg@1323: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1323: * or visit www.oracle.com if you need additional information or have any jjg@1323: * questions. jjg@1323: */ jjg@1323: jjg@1323: /* jjg@1323: * @test jjg@1323: * @bug 7196760 jjg@1323: * @summary javac doesn't report Diagnostic end positions properly when jjg@1323: * an annotation processor is present jjg@1323: */ jjg@1323: jjg@1323: import com.sun.source.util.JavacTask; jjg@1323: import java.io.IOException; jjg@1323: import java.net.URI; jjg@1323: import java.util.Arrays; jjg@1323: import java.util.Collections; jjg@1323: import java.util.List; jjg@1323: import java.util.Set; jjg@1323: import javax.annotation.processing.*; jjg@1323: import javax.lang.model.*; jjg@1323: import javax.lang.model.element.*; jjg@1323: import javax.tools.JavaCompiler; jjg@1323: import javax.tools.JavaFileObject; jjg@1323: import javax.tools.SimpleJavaFileObject; jjg@1323: import javax.tools.Diagnostic; jjg@1323: import javax.tools.DiagnosticCollector; jjg@1323: import static javax.tools.JavaFileObject.Kind.SOURCE; jjg@1323: import javax.tools.ToolProvider; jjg@1323: jjg@1323: @SupportedAnnotationTypes("*") jjg@1323: public class EndPositions extends AbstractProcessor { jjg@1323: public static void main(String... args) throws IOException { jjg@1323: class MyFileObject extends SimpleJavaFileObject { jjg@1323: MyFileObject() { jjg@1323: super(URI.create("myfo:///Test.java"), SOURCE); jjg@1323: } jjg@1323: @Override jjg@1323: public String getCharContent(boolean ignoreEncodingErrors) { jjg@1323: // 0 1 2 3 jjg@1323: // 012345678901234567890123456789012345 jjg@1323: return "class Test { String s = 1234; }"; jjg@1323: } jjg@1323: } jjg@1323: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); jjg@1323: List compilationUnits = jjg@1323: Collections.singletonList(new MyFileObject()); jjg@1323: DiagnosticCollector diagnostics = new DiagnosticCollector(); jjg@1323: List options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); jjg@1323: JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); jjg@1323: boolean valid = task.call(); jjg@1323: if (valid) jjg@1521: throw new AssertionError("Expected one error, but found none."); jjg@1323: jjg@1323: List> errors = diagnostics.getDiagnostics(); jjg@1323: if (errors.size() != 1) jjg@1521: throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); jjg@1323: jjg@1323: Diagnostic error = errors.get(0); jjg@1323: if (error.getStartPosition() >= error.getEndPosition()) jjg@1323: throw new AssertionError("Expected start to be less than end position: start [" + jjg@1521: error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + jjg@1521: "; diagnostics code: " + error.getCode()); jjg@1323: jjg@1323: System.out.println("All is good!"); jjg@1323: } jjg@1323: jjg@1323: @Override jjg@1323: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@1323: return true; jjg@1323: } jjg@1323: jjg@1323: @Override jjg@1323: public SourceVersion getSupportedSourceVersion() { jjg@1323: return SourceVersion.latest(); jjg@1323: } jjg@1323: }