langtools/test/tools/javac/varargs/warning/Warn4.java
author ohair
Wed, 06 Apr 2011 20:33:44 -0700
changeset 9087 e9e44877cd18
parent 8613 d8e9b6d6ccee
child 15040 99fd9483d3f0
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
     1
/*
9087
e9e44877cd18 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8613
diff changeset
     2
 * Copyright (c) 2010, 2011, 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;
8613
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
    32
import com.sun.tools.javac.api.JavacTool;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    33
import java.net.URI;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    34
import java.util.Arrays;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    35
import java.util.Set;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    36
import java.util.HashSet;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    37
import javax.tools.Diagnostic;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    38
import javax.tools.JavaCompiler;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    40
import javax.tools.SimpleJavaFileObject;
8613
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
    41
import javax.tools.StandardJavaFileManager;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    42
import javax.tools.ToolProvider;
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
public class Warn4 {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    45
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    46
    final static Warning[] error = null;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    47
    final static Warning[] none = new Warning[] {};
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    48
    final static Warning[] vararg = new Warning[] { Warning.VARARGS };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    49
    final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    50
    final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    51
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    52
    enum Warning {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    53
        UNCHECKED("generic.array.creation"),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    54
        VARARGS("varargs.non.reifiable.type");
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
        String key;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    57
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    58
        Warning(String key) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    59
            this.key = key;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    60
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    61
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    62
        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
    63
                SuppressLevel suppressLevelDecl, ModifierKind modKind) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    64
            switch(this) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    65
                case VARARGS:
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    66
                    return source == SourceLevel.JDK_6 ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    67
                            suppressLevelDecl == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    68
                            trustMe == TrustMe.TRUST;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    69
                case UNCHECKED:
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    70
                    return suppressLevelClient == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    71
                        (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
    72
            }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    73
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    74
            SuppressLevel supLev = this == VARARGS ?
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    75
                suppressLevelDecl :
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    76
                suppressLevelClient;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    77
            return supLev == SuppressLevel.UNCHECKED ||
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    78
                    (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    79
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    80
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    81
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    82
    enum SourceLevel {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    83
        JDK_6("6"),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    84
        JDK_7("7");
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
        String sourceKey;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    87
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    88
        SourceLevel(String sourceKey) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    89
            this.sourceKey = sourceKey;
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
    }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
    92
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    93
    enum TrustMe {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    94
        DONT_TRUST(""),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    95
        TRUST("@java.lang.SafeVarargs");
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
        String anno;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    98
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
    99
        TrustMe(String anno) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   100
            this.anno = anno;
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
    }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   103
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   104
    enum ModifierKind {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   105
        NONE(" "),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   106
        FINAL("final "),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   107
        STATIC("static ");
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
        String mod;
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   110
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   111
        ModifierKind(String mod) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   112
            this.mod = mod;
5846
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
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   115
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   116
    enum SuppressLevel {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   117
        NONE(""),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   118
        UNCHECKED("unchecked");
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
        String lint;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   121
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   122
        SuppressLevel(String lint) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   123
            this.lint = lint;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   124
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   125
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   126
        String getSuppressAnno() {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   127
            return "@SuppressWarnings(\"" + lint + "\")";
5846
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
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   130
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   131
    enum Signature {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   132
        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
   133
            new Warning[][] {none, none, none, none, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   134
        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
   135
            new Warning[][] {both, both, error, both, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   136
        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
   137
            new Warning[][] {both, both, both, both, vararg}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   138
        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
   139
            new Warning[][] {error, error, error, both, error}),
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   140
        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
   141
            new Warning[][] {error, error, error, error, none});
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   142
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   143
        String template;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   144
        Warning[][] warnings;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   145
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   146
        Signature(String template, Warning[][] warnings) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   147
            this.template = template;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   148
            this.warnings = warnings;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   149
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   150
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   151
        boolean isApplicableTo(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   152
            return warnings[other.ordinal()] != null;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   153
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   154
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   155
        boolean giveUnchecked(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   156
            return warnings[other.ordinal()] == unchecked ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   157
                    warnings[other.ordinal()] == both;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   158
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   159
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   160
        boolean giveVarargs(Signature other) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   161
            return warnings[other.ordinal()] == vararg ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   162
                    warnings[other.ordinal()] == both;
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
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   165
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   166
    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
   167
        for (SourceLevel sourceLevel : SourceLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   168
            for (TrustMe trustMe : TrustMe.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   169
                for (SuppressLevel suppressLevelClient : SuppressLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   170
                    for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   171
                        for (ModifierKind modKind : ModifierKind.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   172
                            for (Signature vararg_meth : Signature.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   173
                                for (Signature client_meth : Signature.values()) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   174
                                    if (vararg_meth.isApplicableTo(client_meth)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   175
                                        test(sourceLevel,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   176
                                                trustMe,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   177
                                                suppressLevelClient,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   178
                                                suppressLevelDecl,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   179
                                                modKind,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   180
                                                vararg_meth,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   181
                                                client_meth);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   182
                                    }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   183
                                }
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   184
                            }
5846
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
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   190
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   191
8613
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
   192
    // Create a single file manager and reuse it for each compile to save time.
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
   193
    static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
   194
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   195
    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
   196
            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
   197
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   198
        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
   199
        DiagnosticChecker dc = new DiagnosticChecker();
8613
d8e9b6d6ccee 7018753: tools/javac/varargs/warning/Warn5.java times out on slow machines
jjh
parents: 7643
diff changeset
   200
        JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   201
                Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey),
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   202
                null, Arrays.asList(source));
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   203
        ct.generate(); //to get mandatory notes
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   204
        check(dc.warnings, sourceLevel,
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   205
                new boolean[] {vararg_meth.giveUnchecked(client_meth),
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   206
                               vararg_meth.giveVarargs(client_meth)},
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   207
                source, trustMe, suppressLevelClient, suppressLevelDecl, modKind);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   208
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   209
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   210
    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
   211
            TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, ModifierKind modKind) {
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   212
        boolean badOutput = false;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   213
        for (Warning wkind : Warning.values()) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   214
            boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel,
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   215
                    suppressLevelClient, suppressLevelDecl, modKind);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   216
            System.out.println("SUPPRESSED = " + isSuppressed);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   217
            badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != warnings.contains(wkind);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   218
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   219
        if (badOutput) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   220
            throw new Error("invalid diagnostics for source:\n" +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   221
                    source.getCharContent(true) +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   222
                    "\nExpected unchecked warning: " + warnArr[0] +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   223
                    "\nExpected unsafe vararg warning: " + warnArr[1] +
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   224
                    "\nWarnings: " + warnings +
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   225
                    "\nSource level: " + sourceLevel);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   226
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   227
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   228
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   229
    static class JavaSource extends SimpleJavaFileObject {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   230
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   231
        String source;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   232
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   233
        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
   234
                ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   235
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   236
            String meth1 = vararg_meth.template.replace("#arity", "...");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   237
            meth1 = meth1.replace("#name", "m");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   238
            meth1 = meth1.replace("#body", "");
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   239
            meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + modKind.mod + meth1;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   240
            String meth2 = client_meth.template.replace("#arity", "");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   241
            meth2 = meth2.replace("#name", "test");
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   242
            meth2 = meth2.replace("#body", "m(arg);");
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   243
            meth2 = suppressLevelClient.getSuppressAnno() + meth2;
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   244
            source = "import java.util.List;\n" +
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   245
                     "class Test {\n" + meth1 +
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   246
                     "\n" + meth2 + "\n}\n";
5846
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
        @Override
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   250
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   251
            return source;
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   252
        }
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
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   255
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   256
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   257
        Set<Warning> warnings = new HashSet<>();
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   258
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   259
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   260
            if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING ||
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   261
                    diagnostic.getKind() == Diagnostic.Kind.WARNING) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   262
                if (diagnostic.getCode().contains(Warning.VARARGS.key)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   263
                    warnings.add(Warning.VARARGS);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   264
                } 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
   265
                    warnings.add(Warning.UNCHECKED);
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   266
                }
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   267
            }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   268
        }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   269
    }
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
diff changeset
   270
}