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