langtools/test/tools/javac/varargs/warning/Warn4.java
author mcimadamore
Mon, 13 Dec 2010 15:11:00 -0800
changeset 7643 a067a0cda531
parent 5847 1908176fd6e3
child 8613 d8e9b6d6ccee
permissions -rw-r--r--
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings Reviewed-by: jjg, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     1
/*
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     4
 *
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     8
 *
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    14
 *
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    18
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    21
 * questions.
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    22
 */
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    23
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    24
/**
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    25
 * @test
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    26
 * @bug     6945418 6993978
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    27
 * @summary Project Coin: Simplified Varargs Method Invocation
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    28
 * @author  mcimadamore
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    29
 * @run main Warn4
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    30
 */
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    31
import com.sun.source.util.JavacTask;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    32
import java.net.URI;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    33
import java.util.Arrays;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    34
import java.util.Set;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    35
import java.util.HashSet;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    36
import javax.tools.Diagnostic;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    37
import javax.tools.JavaCompiler;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    39
import javax.tools.SimpleJavaFileObject;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    40
import javax.tools.ToolProvider;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    41
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    42
public class Warn4 {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    43
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    44
    final static Warning[] error = null;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    45
    final static Warning[] none = new Warning[] {};
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    46
    final static Warning[] vararg = new Warning[] { Warning.VARARGS };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    47
    final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    48
    final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    49
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    50
    enum Warning {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    51
        UNCHECKED("generic.array.creation"),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    52
        VARARGS("varargs.non.reifiable.type");
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    53
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    54
        String key;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    55
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    56
        Warning(String key) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    57
            this.key = key;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    58
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    59
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    60
        boolean isSuppressed(TrustMe trustMe, SourceLevel source, SuppressLevel suppressLevelClient,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    61
                SuppressLevel suppressLevelDecl, ModifierKind modKind) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    62
            switch(this) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    63
                case VARARGS:
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    64
                    return source == SourceLevel.JDK_6 ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    65
                            suppressLevelDecl == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    66
                            trustMe == TrustMe.TRUST;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    67
                case UNCHECKED:
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    68
                    return suppressLevelClient == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    69
                        (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE && source == SourceLevel.JDK_7);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    70
            }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    71
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    72
            SuppressLevel supLev = this == VARARGS ?
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    73
                suppressLevelDecl :
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    74
                suppressLevelClient;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    75
            return supLev == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    76
                    (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    77
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    78
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    79
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    80
    enum SourceLevel {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    81
        JDK_6("6"),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    82
        JDK_7("7");
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    83
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    84
        String sourceKey;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    85
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    86
        SourceLevel(String sourceKey) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    87
            this.sourceKey = sourceKey;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    88
        }
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    89
    }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    90
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    91
    enum TrustMe {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    92
        DONT_TRUST(""),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    93
        TRUST("@java.lang.SafeVarargs");
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    94
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    95
        String anno;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    96
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    97
        TrustMe(String anno) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    98
            this.anno = anno;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    99
        }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   100
    }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   101
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   102
    enum ModifierKind {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   103
        NONE(" "),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   104
        FINAL("final "),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   105
        STATIC("static ");
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   106
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   107
        String mod;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   108
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   109
        ModifierKind(String mod) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   110
            this.mod = mod;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   111
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   112
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   113
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   114
    enum SuppressLevel {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   115
        NONE(""),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   116
        UNCHECKED("unchecked");
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   117
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   118
        String lint;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   119
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   120
        SuppressLevel(String lint) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   121
            this.lint = lint;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   122
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   123
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   124
        String getSuppressAnno() {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   125
            return "@SuppressWarnings(\"" + lint + "\")";
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   126
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   127
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   128
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   129
    enum Signature {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   130
        UNBOUND("void #name(List<?>#arity arg) { #body }",
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   131
            new Warning[][] {none, none, none, none, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   132
        INVARIANT_TVAR("<Z> void #name(List<Z>#arity arg) { #body }",
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   133
            new Warning[][] {both, both, error, both, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   134
        TVAR("<Z> void #name(Z#arity arg) { #body }",
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   135
            new Warning[][] {both, both, both, both, vararg}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   136
        INVARIANT("void #name(List<String>#arity arg) { #body }",
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   137
            new Warning[][] {error, error, error, both, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   138
        UNPARAMETERIZED("void #name(String#arity arg) { #body }",
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   139
            new Warning[][] {error, error, error, error, none});
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   140
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   141
        String template;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   142
        Warning[][] warnings;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   143
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   144
        Signature(String template, Warning[][] warnings) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   145
            this.template = template;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   146
            this.warnings = warnings;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   147
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   148
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   149
        boolean isApplicableTo(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   150
            return warnings[other.ordinal()] != null;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   151
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   152
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   153
        boolean giveUnchecked(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   154
            return warnings[other.ordinal()] == unchecked ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   155
                    warnings[other.ordinal()] == both;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   156
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   157
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   158
        boolean giveVarargs(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   159
            return warnings[other.ordinal()] == vararg ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   160
                    warnings[other.ordinal()] == both;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   161
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   162
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   163
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   164
    public static void main(String... args) throws Exception {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   165
        for (SourceLevel sourceLevel : SourceLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   166
            for (TrustMe trustMe : TrustMe.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   167
                for (SuppressLevel suppressLevelClient : SuppressLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   168
                    for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   169
                        for (ModifierKind modKind : ModifierKind.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   170
                            for (Signature vararg_meth : Signature.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   171
                                for (Signature client_meth : Signature.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   172
                                    if (vararg_meth.isApplicableTo(client_meth)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   173
                                        test(sourceLevel,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   174
                                                trustMe,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   175
                                                suppressLevelClient,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   176
                                                suppressLevelDecl,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   177
                                                modKind,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   178
                                                vararg_meth,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   179
                                                client_meth);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   180
                                    }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   181
                                }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   182
                            }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   183
                        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   184
                    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   185
                }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   186
            }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   187
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   188
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   189
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   190
    static void test(SourceLevel sourceLevel, TrustMe trustMe, SuppressLevel suppressLevelClient,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   191
            SuppressLevel suppressLevelDecl, ModifierKind modKind, Signature vararg_meth, Signature client_meth) throws Exception {
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   192
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   193
        JavaSource source = new JavaSource(trustMe, suppressLevelClient, suppressLevelDecl, modKind, vararg_meth, client_meth);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   194
        DiagnosticChecker dc = new DiagnosticChecker();
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   195
        JavacTask ct = (JavacTask)tool.getTask(null, null, dc,
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   196
                Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   197
                null, Arrays.asList(source));
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   198
        ct.generate(); //to get mandatory notes
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   199
        check(dc.warnings, sourceLevel,
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   200
                new boolean[] {vararg_meth.giveUnchecked(client_meth),
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   201
                               vararg_meth.giveVarargs(client_meth)},
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   202
                source, trustMe, suppressLevelClient, suppressLevelDecl, modKind);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   203
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   204
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   205
    static void check(Set<Warning> warnings, SourceLevel sourceLevel, boolean[] warnArr, JavaSource source,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   206
            TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, ModifierKind modKind) {
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   207
        boolean badOutput = false;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   208
        for (Warning wkind : Warning.values()) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   209
            boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   210
                    suppressLevelClient, suppressLevelDecl, modKind);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   211
            System.out.println("SUPPRESSED = " + isSuppressed);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   212
            badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != warnings.contains(wkind);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   213
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   214
        if (badOutput) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   215
            throw new Error("invalid diagnostics for source:\n" +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   216
                    source.getCharContent(true) +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   217
                    "\nExpected unchecked warning: " + warnArr[0] +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   218
                    "\nExpected unsafe vararg warning: " + warnArr[1] +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   219
                    "\nWarnings: " + warnings +
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   220
                    "\nSource level: " + sourceLevel);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   221
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   222
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   223
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   224
    static class JavaSource extends SimpleJavaFileObject {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   225
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   226
        String source;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   227
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   228
        public JavaSource(TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   229
                ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   230
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   231
            String meth1 = vararg_meth.template.replace("#arity", "...");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   232
            meth1 = meth1.replace("#name", "m");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   233
            meth1 = meth1.replace("#body", "");
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   234
            meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + modKind.mod + meth1;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   235
            String meth2 = client_meth.template.replace("#arity", "");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   236
            meth2 = meth2.replace("#name", "test");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   237
            meth2 = meth2.replace("#body", "m(arg);");
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   238
            meth2 = suppressLevelClient.getSuppressAnno() + meth2;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   239
            source = "import java.util.List;\n" +
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   240
                     "class Test {\n" + meth1 +
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   241
                     "\n" + meth2 + "\n}\n";
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   242
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   243
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   244
        @Override
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   245
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   246
            return source;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   247
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   248
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   249
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   250
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   251
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   252
        Set<Warning> warnings = new HashSet<>();
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   253
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   254
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   255
            if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   256
                    diagnostic.getKind() == Diagnostic.Kind.WARNING) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   257
                if (diagnostic.getCode().contains(Warning.VARARGS.key)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   258
                    warnings.add(Warning.VARARGS);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   259
                } else if(diagnostic.getCode().contains(Warning.UNCHECKED.key)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   260
                    warnings.add(Warning.UNCHECKED);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   261
                }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   262
            }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   263
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   264
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   265
}