langtools/test/tools/javac/annotations/typeAnnotations/newlocations/AfterMethodTypeParams.java
author vromero
Tue, 06 Sep 2016 17:04:43 -0700
changeset 40835 6ab9ed1abc46
parent 39601 5b37e511ae4b
permissions -rw-r--r--
8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     1
/*
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 30730
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     4
 *
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     8
 *
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    14
 *
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    18
 *
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    21
 * questions.
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    22
 */
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    23
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    24
/**
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    25
 * @test
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    26
 * @bug 8038788
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    27
 * @summary Verify proper handling of annotations after method's type parameters.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24062
diff changeset
    28
 * @modules jdk.compiler
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    29
 * @build AfterMethodTypeParams
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    30
 * @run main AfterMethodTypeParams
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    31
 */
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    32
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    33
import java.io.IOException;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    34
import java.io.StringWriter;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    35
import java.net.URI;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    36
import java.util.*;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    37
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    38
import javax.lang.model.element.Name;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    39
import javax.tools.*;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    40
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    41
import com.sun.source.tree.*;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    42
import com.sun.source.util.*;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    43
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    44
public class AfterMethodTypeParams {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    45
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    46
    public static void main(String... args) throws IOException {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    47
        new AfterMethodTypeParams().run();
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    48
    }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    49
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    50
    void run() throws IOException {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    51
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    52
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    53
        for (TestCase tc : testCases) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    54
            String test = TEMPLATE.replace("CONTENT", tc.snippet);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    55
            List<JavaFileObject> files = Arrays.asList(new MyFileObject(test));
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    56
            StringWriter out = new StringWriter();
40835
6ab9ed1abc46 8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags
vromero
parents: 39601
diff changeset
    57
            List<String> options = Arrays.asList("-XDrawDiagnostics", "--should-stop:at=FLOW");
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    58
            JavacTask task = (JavacTask) compiler.getTask(out, null, null, options, null, files);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    59
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    60
            new TreePathScanner<Void, Void>() {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    61
                boolean seenAnnotation;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    62
                @Override
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    63
                public Void visitAnnotation(AnnotationTree node, Void p) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    64
                    Name name = ((IdentifierTree) node.getAnnotationType()).getName();
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    65
                    seenAnnotation |= name.contentEquals("TA") || name.contentEquals("DA");
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    66
                    return null;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    67
                }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    68
                @Override
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    69
                public Void visitCompilationUnit(CompilationUnitTree node, Void p) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    70
                    super.visitCompilationUnit(node, p);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    71
                    if (!seenAnnotation)
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    72
                        error(test, "Annotation was missing");
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    73
                    return null;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    74
                }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    75
            }.scan(task.parse(), null);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    76
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    77
            task.analyze();
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    78
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    79
            if (!tc.error.equals(out.toString().trim())) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    80
                error(test, "Incorrect errors: " + out.toString());
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    81
            }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    82
        }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    83
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    84
        if (errors > 0) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    85
            throw new IllegalStateException("Errors found");
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    86
        }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    87
    }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    88
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    89
    int errors;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    90
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    91
    void error(String code, String error) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    92
        System.out.println("Error detected: " + error);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    93
        System.out.println("Code:");
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    94
        System.out.println(code);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    95
        errors++;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    96
    }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    97
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    98
    static String TEMPLATE =
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
    99
        "import java.lang.annotation.*;\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   100
        "public class Test {\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   101
        "    CONTENT\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   102
        "}\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   103
        "@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   104
        "@interface DA { }\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   105
        "@Target(ElementType.TYPE_USE)\n" +
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   106
        "@interface TA { }\n";
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   107
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   108
    static class MyFileObject extends SimpleJavaFileObject {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   109
        final String text;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   110
        public MyFileObject(String text) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   111
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   112
            this.text = text;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   113
        }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   114
        @Override
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   115
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   116
            return text;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   117
        }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   118
    }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   119
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   120
    static TestCase[] testCases = new TestCase[] {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   121
        new TestCase("<T> @DA int foo1() { return 0;}", ""),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   122
        new TestCase("<T> @DA void foo2() { }", ""),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   123
        new TestCase("<T> @TA int foo3() { return 0;}", ""),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   124
        new TestCase("<T> @TA void foo4() { }",
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   125
                "Test.java:3:9: compiler.err.annotation.type.not.applicable"),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   126
        new TestCase("<T> @DA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   127
        new TestCase("<T> @TA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"),
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   128
    };
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   129
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   130
    static class TestCase {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   131
        final String snippet;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   132
        final String error;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   133
        public TestCase(String snippet, String error) {
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   134
            this.snippet = snippet;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   135
            this.error = error;
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   136
        }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   137
    }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   138
}
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents:
diff changeset
   139