langtools/test/tools/javac/api/EndPositions.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 15385 ee1eebe7e210
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
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
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13841
diff changeset
     2
 * Copyright (c) 2010, 2013, 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
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    29
 */
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
import com.sun.source.util.JavacTask;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    32
import java.io.IOException;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    33
import java.net.URI;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    34
import java.util.Arrays;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    35
import java.util.Collections;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    36
import java.util.List;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    37
import java.util.Set;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    38
import javax.annotation.processing.*;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    39
import javax.lang.model.*;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    40
import javax.lang.model.element.*;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    41
import javax.tools.JavaCompiler;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    43
import javax.tools.SimpleJavaFileObject;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    44
import javax.tools.Diagnostic;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    45
import javax.tools.DiagnosticCollector;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    46
import static javax.tools.JavaFileObject.Kind.SOURCE;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    47
import javax.tools.ToolProvider;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    48
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    49
@SupportedAnnotationTypes("*")
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    50
public class EndPositions extends AbstractProcessor {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    51
    public static void main(String... args) throws IOException {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    52
        class MyFileObject extends SimpleJavaFileObject {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    53
            MyFileObject() {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    54
                super(URI.create("myfo:///Test.java"), SOURCE);
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    55
            }
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    56
            @Override
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    57
            public String getCharContent(boolean ignoreEncodingErrors) {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    58
                //      0         1         2         3
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    59
                //      012345678901234567890123456789012345
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    60
                return "class Test { String s = 1234; }";
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    61
            }
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
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    64
        List<JavaFileObject> compilationUnits =
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    65
                Collections.<JavaFileObject>singletonList(new MyFileObject());
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    66
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    67
        List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName());
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    68
        JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits);
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    69
        boolean valid = task.call();
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    70
        if (valid)
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13841
diff changeset
    71
            throw new AssertionError("Expected one error, but found none.");
13841
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    72
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    73
        List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics();
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    74
        if (errors.size() != 1)
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13841
diff changeset
    75
            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
    76
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    77
        Diagnostic<?> error = errors.get(0);
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    78
        if (error.getStartPosition() >= error.getEndPosition())
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    79
            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
    80
                    error.getStartPosition() + "], end [" + error.getEndPosition() +"]" +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13841
diff changeset
    81
                    "; diagnostics code: " + error.getCode());
13841
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    82
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    83
        System.out.println("All is good!");
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    84
    }
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
    @Override
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    87
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    88
        return true;
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    89
    }
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
    @Override
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    92
    public SourceVersion getSupportedSourceVersion() {
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    93
        return SourceVersion.latest();
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    94
    }
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents:
diff changeset
    95
}