test/tools/javac/api/EndPositions.java

Wed, 23 Jan 2013 20:57:40 +0000

author
vromero
date
Wed, 23 Jan 2013 20:57:40 +0000
changeset 1520
5c956be64b9e
parent 1323
1a7c11b22192
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8006694: temporarily workaround combo tests are causing time out in several platforms
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com

jjg@1323 1 /*
jjg@1323 2 * Copyright (c) 2010, 2012, 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.tree.ClassTree;
jjg@1323 32 import com.sun.source.tree.CompilationUnitTree;
jjg@1323 33 import com.sun.source.tree.Tree;
jjg@1323 34 import com.sun.source.util.JavacTask;
jjg@1323 35 import com.sun.source.util.Trees;
jjg@1323 36 import java.io.IOException;
jjg@1323 37 import java.net.URI;
jjg@1323 38 import java.util.Arrays;
jjg@1323 39 import java.util.Collections;
jjg@1323 40 import java.util.List;
jjg@1323 41 import java.util.Set;
jjg@1323 42 import javax.annotation.processing.*;
jjg@1323 43 import javax.lang.model.*;
jjg@1323 44 import javax.lang.model.element.*;
jjg@1323 45 import javax.tools.JavaCompiler;
jjg@1323 46 import javax.tools.JavaFileObject;
jjg@1323 47 import javax.tools.SimpleJavaFileObject;
jjg@1323 48 import javax.tools.Diagnostic;
jjg@1323 49 import javax.tools.DiagnosticCollector;
jjg@1323 50 import static javax.tools.JavaFileObject.Kind.SOURCE;
jjg@1323 51 import javax.tools.ToolProvider;
jjg@1323 52
jjg@1323 53 @SupportedAnnotationTypes("*")
jjg@1323 54 public class EndPositions extends AbstractProcessor {
jjg@1323 55 public static void main(String... args) throws IOException {
jjg@1323 56 class MyFileObject extends SimpleJavaFileObject {
jjg@1323 57 MyFileObject() {
jjg@1323 58 super(URI.create("myfo:///Test.java"), SOURCE);
jjg@1323 59 }
jjg@1323 60 @Override
jjg@1323 61 public String getCharContent(boolean ignoreEncodingErrors) {
jjg@1323 62 // 0 1 2 3
jjg@1323 63 // 012345678901234567890123456789012345
jjg@1323 64 return "class Test { String s = 1234; }";
jjg@1323 65 }
jjg@1323 66 }
jjg@1323 67 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
jjg@1323 68 List<JavaFileObject> compilationUnits =
jjg@1323 69 Collections.<JavaFileObject>singletonList(new MyFileObject());
jjg@1323 70 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
jjg@1323 71 List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName());
jjg@1323 72 JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits);
jjg@1323 73 boolean valid = task.call();
jjg@1323 74 if (valid)
jjg@1323 75 throw new AssertionError("Compilation succeeded unexpectedly");
jjg@1323 76
jjg@1323 77 List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics();
jjg@1323 78 if (errors.size() != 1)
jjg@1323 79 throw new AssertionError("Expected one error only, but found " + errors.size() + " errors");
jjg@1323 80
jjg@1323 81 Diagnostic<?> error = errors.get(0);
jjg@1323 82 if (error.getStartPosition() >= error.getEndPosition())
jjg@1323 83 throw new AssertionError("Expected start to be less than end position: start [" +
jjg@1323 84 error.getStartPosition() + "], end [" + error.getEndPosition() +"]");
jjg@1323 85
jjg@1323 86 System.out.println("All is good!");
jjg@1323 87 }
jjg@1323 88
jjg@1323 89 @Override
jjg@1323 90 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@1323 91 return true;
jjg@1323 92 }
jjg@1323 93
jjg@1323 94 @Override
jjg@1323 95 public SourceVersion getSupportedSourceVersion() {
jjg@1323 96 return SourceVersion.latest();
jjg@1323 97 }
jjg@1323 98 }

mercurial