langtools/test/tools/javac/annotations/neg/8022765/VerifyErroneousAnnotationsAttributed.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:
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
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.
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     4
 *
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     8
 *
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    14
 *
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    18
 *
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    21
 * questions.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    22
 */
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    23
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    24
/*
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    25
 * @test
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    26
 * @bug 8029161 8029376
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27388
diff changeset
    27
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27388
diff changeset
    28
 *          jdk.compiler/com.sun.tools.javac.file
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    29
 */
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    30
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    31
import com.sun.source.tree.CompilationUnitTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    32
import com.sun.source.tree.IdentifierTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    33
import com.sun.source.tree.MemberSelectTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    34
import com.sun.source.util.JavacTask;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    35
import com.sun.source.util.TreePathScanner;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    36
import com.sun.source.util.Trees;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    37
import com.sun.tools.javac.api.JavacTool;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    38
import java.io.IOException;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    39
import java.net.URI;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    40
import java.net.URISyntaxException;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    41
import java.util.ArrayList;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    42
import java.util.Arrays;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    43
import java.util.List;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    44
import javax.lang.model.element.Element;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    45
import javax.lang.model.element.ElementKind;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    46
import javax.lang.model.type.TypeKind;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    47
import javax.tools.Diagnostic;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    48
import javax.tools.DiagnosticListener;
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    49
import javax.tools.JavaFileManager;
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    50
import javax.tools.JavaFileObject;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    51
import javax.tools.SimpleJavaFileObject;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    52
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    53
public class VerifyErroneousAnnotationsAttributed {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    54
    public static void main(String... args) throws IOException, URISyntaxException {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    55
        new VerifyErroneousAnnotationsAttributed().run();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    56
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    57
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    58
    void run() throws IOException {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    59
        try {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    60
            int failCount = 0;
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    61
            for (String ann : generateAnnotations()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    62
                String code = PATTERN.replace("PLACEHOLDER", ann);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    63
                try {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    64
                    validate(code);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    65
                } catch (Throwable t) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    66
                    System.out.println("Failed for: ");
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    67
                    System.out.println(code);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    68
                    t.printStackTrace(System.out);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    69
                    failCount++;
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    70
                }
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    71
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    72
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    73
            if (failCount > 0) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    74
                throw new IllegalStateException("failed sub-tests: " + failCount);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    75
            }
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    76
        } finally {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
    77
            fm.close();
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    78
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    79
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    80
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    81
    List<String> generateAnnotations() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    82
        List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    83
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    84
        result.addAll(Kind.ANNOTATION.generateValue(2, true));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    85
        result.addAll(Kind.ANNOTATION.generateValue(2, false));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    86
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    87
        return result;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    88
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    89
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    90
    enum Kind {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    91
        INT("i", "ValueInt") {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    92
            @Override public List<String> generateValue(int depth, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    93
                if (valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    94
                    return Arrays.asList("INT_VALUE");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    95
                } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    96
                    return Arrays.asList("BROKEN_INT_VALUE");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    97
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    98
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    99
        },
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   100
        ANNOTATION("a", "ValueAnnotation") {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   101
            @Override public List<String> generateValue(int depth, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   102
                String ad = "@Annotation" + depth + (valid ? "" : "Unknown");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   103
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   104
                if (depth <= 0) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   105
                    return Arrays.asList(ad);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   106
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   107
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   108
                List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   109
                final Kind[][] generateKindCombinations = new Kind[][] {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   110
                    new Kind[] {Kind.INT},
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   111
                    new Kind[] {Kind.ANNOTATION},
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   112
                    new Kind[] {Kind.INT, Kind.ANNOTATION}
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   113
                };
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   114
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   115
                for (boolean generateAssignment : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   116
                    for (boolean generateValid : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   117
                        for (Kind[] generateKinds : generateKindCombinations) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   118
                            if (generateKinds.length > 1 && generateValid && !generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   119
                                //skip: the same code is generated for generateValid == false.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   120
                                continue;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   121
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   122
                            List<String> attributes = generateAttributes(generateAssignment,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   123
                                    generateValid, depth, generateKinds);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   124
                            String annotation;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   125
                            if (generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   126
                                annotation = ad;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   127
                            } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   128
                                annotation = ad + generateKinds[0].annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   129
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   130
                            for (String attr : attributes) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   131
                                result.add(annotation + "(" + attr + ")");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   132
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   133
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   134
                    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   135
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   136
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   137
                return result;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   138
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   139
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   140
            List<String> generateAttributes(boolean generateAssignment, boolean valid, int depth,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   141
                                            Kind... kinds) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   142
                List<List<String>> combinations = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   143
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   144
                for (boolean subValid : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   145
                    for (Kind k : kinds) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   146
                        String prefix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   147
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   148
                        if (generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   149
                            if (valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   150
                                prefix = k.validAttributeName + "=";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   151
                            } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   152
                                prefix = "invalid" + k.validAttributeName + "=";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   153
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   154
                        } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   155
                            prefix = "";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   156
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   157
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   158
                        List<String> combination = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   159
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   160
                        combinations.add(combination);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   161
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   162
                        for (String val : k.generateValue(depth - 1, subValid)) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   163
                            combination.add(prefix + val);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   164
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   165
                    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   166
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   167
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   168
                List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   169
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   170
                combine(combinations, new StringBuilder(), result);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   171
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   172
                return result;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   173
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   174
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   175
            void combine(List<List<String>> combinations, StringBuilder current, List<String> to) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   176
                if (combinations.isEmpty()) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   177
                    to.add(current.toString());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   178
                    return ;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   179
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   180
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   181
                int currLen = current.length();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   182
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   183
                for (String str : combinations.get(0)) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   184
                    if (current.length() > 0) current.append(", ");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   185
                    current.append(str);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   186
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   187
                    combine(combinations.subList(1, combinations.size()), current, to);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   188
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   189
                    current.delete(currLen, current.length());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   190
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   191
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   192
        };
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   193
        String validAttributeName;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   194
        String annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   195
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   196
        private Kind(String validAttributeName, String annotationWithValueSuffix) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   197
            this.validAttributeName = validAttributeName;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   198
            this.annotationWithValueSuffix = annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   199
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   200
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   201
        public abstract List<String> generateValue(int depth, boolean valid);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   202
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   203
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   204
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   205
    private static final String PATTERN =
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   206
            "public class Test {\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   207
            "    public static final int INT_VALUE = 1;\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   208
            "    @interface Annotation0 {}\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   209
            "    @interface Annotation1 {int i() default 0; Annotation0 a() default @Annotation0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   210
            "    @interface Annotation2 {int i() default 0; Annotation1 a() default @Annotation1; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   211
            "    @interface Annotation1ValueInt {int value() default 0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   212
            "    @interface Annotation2ValueInt {int value() default 0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   213
            "    @interface Annotation1ValueAnnotation {Annotation0 a() default @Annotation0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   214
            "    @interface Annotation2ValueAnnotation {Annotation1 a() default @Annotation1; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   215
            "    PLACEHOLDER\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   216
            "    private void test() { }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   217
            "}";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   218
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   219
    static final class TestCase {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   220
        final String code;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   221
        final boolean valid;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   222
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   223
        public TestCase(String code, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   224
            this.code = code;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   225
            this.valid = valid;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   226
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   227
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   228
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   229
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   230
    final JavacTool tool = JavacTool.create();
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
   231
    final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   232
    final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   233
        @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   234
    };
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   235
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   236
    void validate(String code) throws IOException, URISyntaxException {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   237
        JavacTask task = tool.getTask(null,
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 22439
diff changeset
   238
                                      fm,
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   239
                                      devNull,
40835
6ab9ed1abc46 8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags
vromero
parents: 39601
diff changeset
   240
                                      Arrays.asList("--should-stop:at=FLOW"),
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   241
                                      null,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   242
                                      Arrays.asList(new MyFileObject(code)));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   243
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   244
        final Trees trees = Trees.instance(task);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   245
        final CompilationUnitTree cut = task.parse().iterator().next();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   246
        task.analyze();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   247
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   248
        //ensure all the annotation attributes are annotated meaningfully
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   249
        //all the attributes in the test file should contain either an identifier
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   250
        //or a select, so only checking those for a reasonable Element/Symbol.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   251
        new TreePathScanner<Void, Void>() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   252
            @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   253
            public Void visitIdentifier(IdentifierTree node, Void p) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   254
                verifyAttributedMeaningfully();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   255
                return super.visitIdentifier(node, p);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   256
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   257
            @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   258
            public Void visitMemberSelect(MemberSelectTree node, Void p) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   259
                verifyAttributedMeaningfully();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   260
                return super.visitMemberSelect(node, p);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   261
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   262
            private void verifyAttributedMeaningfully() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   263
                Element el = trees.getElement(getCurrentPath());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   264
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   265
                if (el == null || el.getKind() == ElementKind.OTHER ||
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   266
                        el.asType().getKind() == TypeKind.OTHER) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   267
                    throw new IllegalStateException("Not attributed properly: " +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   268
                            getCurrentPath().getParentPath().getLeaf());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   269
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   270
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   271
        }.scan(cut, null);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   272
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   273
    static class MyFileObject extends SimpleJavaFileObject {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   274
        private final String text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   275
        public MyFileObject(String text) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   276
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   277
            this.text = text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   278
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   279
        @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   280
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   281
            return text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   282
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   283
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   284
}