langtools/test/tools/javac/annotations/neg/8022765/VerifyErroneousAnnotationsAttributed.java
author jlahoda
Fri, 10 Jan 2014 11:31:09 +0100
changeset 22439 4785d87cd2d6
child 27388 d694da45bd7a
permissions -rw-r--r--
8029376: Full attribution of unresolvable annotations 8029161: javac crashing when processing broken annotations Summary: Attributing values of annotation attributes regardless how broken the annotation is. Reviewed-by: jjg, jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     1
/*
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    27
 */
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    28
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    29
import com.sun.source.tree.CompilationUnitTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    30
import com.sun.source.tree.IdentifierTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    31
import com.sun.source.tree.MemberSelectTree;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    32
import com.sun.source.util.JavacTask;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    33
import com.sun.source.util.TreePathScanner;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    34
import com.sun.source.util.Trees;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTool;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    36
import java.io.IOException;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    37
import java.net.URI;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    38
import java.net.URISyntaxException;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    39
import java.util.ArrayList;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    40
import java.util.Arrays;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    41
import java.util.List;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    42
import javax.lang.model.element.Element;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    43
import javax.lang.model.element.ElementKind;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    44
import javax.lang.model.type.TypeKind;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    45
import javax.tools.Diagnostic;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    46
import javax.tools.DiagnosticListener;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    47
import javax.tools.JavaFileObject;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    48
import javax.tools.SimpleJavaFileObject;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    49
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    50
public class VerifyErroneousAnnotationsAttributed {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    51
    public static void main(String... args) throws IOException, URISyntaxException {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    52
        new VerifyErroneousAnnotationsAttributed().run();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    53
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    54
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    55
    void run() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    56
        int failCount = 0;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    57
        for (String ann : generateAnnotations()) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    58
            String code = PATTERN.replace("PLACEHOLDER", ann);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    59
            try {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    60
                validate(code);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    61
            } catch (Throwable t) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    62
                System.out.println("Failed for: ");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    63
                System.out.println(code);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    64
                t.printStackTrace(System.out);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    65
                failCount++;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    66
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    67
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    68
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    69
        if (failCount > 0) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    70
            throw new IllegalStateException("failed sub-tests: " + failCount);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    71
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    72
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    73
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    74
    List<String> generateAnnotations() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    75
        List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    76
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    77
        result.addAll(Kind.ANNOTATION.generateValue(2, true));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    78
        result.addAll(Kind.ANNOTATION.generateValue(2, false));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    79
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    80
        return result;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    81
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    82
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    83
    enum Kind {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    84
        INT("i", "ValueInt") {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    85
            @Override public List<String> generateValue(int depth, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    86
                if (valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    87
                    return Arrays.asList("INT_VALUE");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    88
                } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    89
                    return Arrays.asList("BROKEN_INT_VALUE");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    90
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    91
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    92
        },
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    93
        ANNOTATION("a", "ValueAnnotation") {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    94
            @Override public List<String> generateValue(int depth, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    95
                String ad = "@Annotation" + depth + (valid ? "" : "Unknown");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    96
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    97
                if (depth <= 0) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    98
                    return Arrays.asList(ad);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
    99
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   100
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   101
                List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   102
                final Kind[][] generateKindCombinations = new Kind[][] {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   103
                    new Kind[] {Kind.INT},
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   104
                    new Kind[] {Kind.ANNOTATION},
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   105
                    new Kind[] {Kind.INT, Kind.ANNOTATION}
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
                for (boolean generateAssignment : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   109
                    for (boolean generateValid : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   110
                        for (Kind[] generateKinds : generateKindCombinations) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   111
                            if (generateKinds.length > 1 && generateValid && !generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   112
                                //skip: the same code is generated for generateValid == false.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   113
                                continue;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   114
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   115
                            List<String> attributes = generateAttributes(generateAssignment,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   116
                                    generateValid, depth, generateKinds);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   117
                            String annotation;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   118
                            if (generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   119
                                annotation = ad;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   120
                            } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   121
                                annotation = ad + generateKinds[0].annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   122
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   123
                            for (String attr : attributes) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   124
                                result.add(annotation + "(" + attr + ")");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   125
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   126
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   127
                    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   128
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   129
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   130
                return result;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   131
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   132
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   133
            List<String> generateAttributes(boolean generateAssignment, boolean valid, int depth,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   134
                                            Kind... kinds) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   135
                List<List<String>> combinations = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   136
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   137
                for (boolean subValid : new boolean[] {false, true}) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   138
                    for (Kind k : kinds) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   139
                        String prefix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   140
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   141
                        if (generateAssignment) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   142
                            if (valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   143
                                prefix = k.validAttributeName + "=";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   144
                            } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   145
                                prefix = "invalid" + k.validAttributeName + "=";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   146
                            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   147
                        } else {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   148
                            prefix = "";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   149
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   150
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   151
                        List<String> combination = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   152
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   153
                        combinations.add(combination);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   154
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   155
                        for (String val : k.generateValue(depth - 1, subValid)) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   156
                            combination.add(prefix + val);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   157
                        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   158
                    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   159
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   160
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   161
                List<String> result = new ArrayList<>();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   162
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   163
                combine(combinations, new StringBuilder(), result);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   164
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   165
                return result;
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
            void combine(List<List<String>> combinations, StringBuilder current, List<String> to) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   169
                if (combinations.isEmpty()) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   170
                    to.add(current.toString());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   171
                    return ;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   172
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   173
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   174
                int currLen = current.length();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   175
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   176
                for (String str : combinations.get(0)) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   177
                    if (current.length() > 0) current.append(", ");
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   178
                    current.append(str);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   179
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   180
                    combine(combinations.subList(1, combinations.size()), current, to);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   181
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   182
                    current.delete(currLen, current.length());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   183
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   184
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   185
        };
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   186
        String validAttributeName;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   187
        String annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   188
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   189
        private Kind(String validAttributeName, String annotationWithValueSuffix) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   190
            this.validAttributeName = validAttributeName;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   191
            this.annotationWithValueSuffix = annotationWithValueSuffix;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   192
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   193
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   194
        public abstract List<String> generateValue(int depth, boolean valid);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   195
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   196
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   197
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   198
    private static final String PATTERN =
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   199
            "public class Test {\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   200
            "    public static final int INT_VALUE = 1;\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   201
            "    @interface Annotation0 {}\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   202
            "    @interface Annotation1 {int i() default 0; Annotation0 a() default @Annotation0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   203
            "    @interface Annotation2 {int i() default 0; Annotation1 a() default @Annotation1; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   204
            "    @interface Annotation1ValueInt {int value() default 0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   205
            "    @interface Annotation2ValueInt {int value() default 0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   206
            "    @interface Annotation1ValueAnnotation {Annotation0 a() default @Annotation0; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   207
            "    @interface Annotation2ValueAnnotation {Annotation1 a() default @Annotation1; }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   208
            "    PLACEHOLDER\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   209
            "    private void test() { }\n" +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   210
            "}";
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   211
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   212
    static final class TestCase {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   213
        final String code;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   214
        final boolean valid;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   215
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   216
        public TestCase(String code, boolean valid) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   217
            this.code = code;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   218
            this.valid = valid;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   219
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   220
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   221
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   222
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   223
    final JavacTool tool = JavacTool.create();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   224
    final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   225
        @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
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
    void validate(String code) throws IOException, URISyntaxException {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   229
        JavacTask task = tool.getTask(null,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   230
                                      null,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   231
                                      devNull,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   232
                                      Arrays.asList("-XDshouldStopPolicy=FLOW"),
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   233
                                      null,
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   234
                                      Arrays.asList(new MyFileObject(code)));
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   235
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   236
        final Trees trees = Trees.instance(task);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   237
        final CompilationUnitTree cut = task.parse().iterator().next();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   238
        task.analyze();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   239
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   240
        //ensure all the annotation attributes are annotated meaningfully
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   241
        //all the attributes in the test file should contain either an identifier
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   242
        //or a select, so only checking those for a reasonable Element/Symbol.
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   243
        new TreePathScanner<Void, Void>() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   244
            @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   245
            public Void visitIdentifier(IdentifierTree node, Void p) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   246
                verifyAttributedMeaningfully();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   247
                return super.visitIdentifier(node, p);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   248
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   249
            @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   250
            public Void visitMemberSelect(MemberSelectTree node, Void p) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   251
                verifyAttributedMeaningfully();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   252
                return super.visitMemberSelect(node, p);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   253
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   254
            private void verifyAttributedMeaningfully() {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   255
                Element el = trees.getElement(getCurrentPath());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   256
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   257
                if (el == null || el.getKind() == ElementKind.OTHER ||
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   258
                        el.asType().getKind() == TypeKind.OTHER) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   259
                    throw new IllegalStateException("Not attributed properly: " +
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   260
                            getCurrentPath().getParentPath().getLeaf());
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   261
                }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   262
            }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   263
        }.scan(cut, null);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   264
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   265
    static class MyFileObject extends SimpleJavaFileObject {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   266
        private final String text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   267
        public MyFileObject(String text) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   268
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   269
            this.text = text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   270
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   271
        @Override
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   272
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   273
            return text;
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   274
        }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   275
    }
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents:
diff changeset
   276
}