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

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

mercurial