langtools/test/tools/javac/annotations/neg/8022765/VerifyAnnotationsAttributed.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 27319 030080f03e4f
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     4
 *
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     8
 *
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    14
 *
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    18
 *
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    21
 * questions.
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    22
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 27319
diff changeset
    23
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    24
import com.sun.source.tree.CompilationUnitTree;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    25
import com.sun.source.tree.IdentifierTree;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    26
import com.sun.source.tree.MemberSelectTree;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    27
import com.sun.source.util.JavacTask;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    28
import com.sun.source.util.TreePathScanner;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    29
import com.sun.source.util.Trees;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    30
import com.sun.tools.javac.api.JavacTool;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    31
import com.sun.tools.javac.file.JavacFileManager;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    32
import java.io.File;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    33
import java.io.IOException;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    34
import java.net.URISyntaxException;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    35
import java.util.Collections;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    36
import javax.lang.model.element.Element;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    37
import javax.lang.model.element.ElementKind;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    38
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    39
public class VerifyAnnotationsAttributed {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    40
    public static void main(String... args) throws IOException, URISyntaxException {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    41
        if (args.length != 1) throw new IllegalStateException("Must provide class name!");
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    42
        File testSrc = new File(System.getProperty("test.src"));
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    43
        File testFile = new File(testSrc, args[0]);
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    44
        if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source");
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    45
        try (JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    46
            JavacTask task = JavacTool.create().getTask(null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    47
                                                        fm,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    48
                                                        null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    49
                                                        Collections.<String>emptyList(),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    50
                                                        null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    51
                                                        fm.getJavaFileObjects(testFile));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    52
            final Trees trees = Trees.instance(task);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    53
            final CompilationUnitTree cut = task.parse().iterator().next();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    54
            task.analyze();
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    55
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    56
            //ensure all the annotation attributes are annotated meaningfully
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    57
            //all the attributes in the test file should contain either an identifier
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    58
            //or a select, so only checking those for a reasonable Element/Symbol.
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    59
            new TreePathScanner<Void, Void>() {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    60
                @Override
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    61
                public Void visitIdentifier(IdentifierTree node, Void p) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    62
                    verifyAttributedMeaningfully();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    63
                    return super.visitIdentifier(node, p);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    64
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    65
                @Override
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    66
                public Void visitMemberSelect(MemberSelectTree node, Void p) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    67
                    verifyAttributedMeaningfully();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    68
                    return super.visitMemberSelect(node, p);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    69
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    70
                private void verifyAttributedMeaningfully() {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    71
                    Element el = trees.getElement(getCurrentPath());
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    72
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    73
                    if (el == null || el.getKind() == ElementKind.OTHER) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    74
                        throw new IllegalStateException("Not attributed properly: " + getCurrentPath().getParentPath().getLeaf());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    75
                    }
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    76
                }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    77
            }.scan(cut, null);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 20599
diff changeset
    78
        }
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    79
    }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents:
diff changeset
    80
}