1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8177466 8189838
  27  * @summary Add compiler support for local variable type-inference
  28  * @modules jdk.compiler/com.sun.source.tree
  29  *          jdk.compiler/com.sun.source.util
  30  *          jdk.compiler/com.sun.tools.javac.api
  31  *          jdk.compiler/com.sun.tools.javac.code
  32  *          jdk.compiler/com.sun.tools.javac.util
  33  * @build LocalVariableInferenceTester
  34  * @run main LocalVariableInferenceTester NonDenotableTest.java
  35  */
  36 import java.util.List;
  37 
  38 class NonDenotableTest {
  39 
  40     static final String OBJECT = "java.lang.Object";
  41     static final String STRING = "java.lang.String";
  42     static final String ANON_OBJECT = "#ANON(java.lang.Object)";
  43     static final String ANON_RUNNABLE = "#ANON(java.lang.Object,java.lang.Runnable)";
  44     static final String LIST_EXT = "java.util.List<? extends java.lang.String>";
  45     static final String LIST_SUP = "java.util.List<? super java.lang.String>";
  46     static final String LIST_UNB = "java.util.List<?>";
  47     static final String COMP_UNB = "java.lang.Comparable<?>";
  48     static final String LIST_EXT_COMP_UNB = "java.util.List<? extends java.lang.Comparable<?>>";
  49     static final String LIST_SUP_COMP_UNB = "java.util.List<? super java.lang.Comparable<?>>";
  50     static final String INT_INTEGER_DOUBLE = "#INT(java.lang.Number,java.lang.Comparable<? extends java.lang.Number&java.lang.Comparable<?>>)";
  51     static final String SEL_INT_ENUM_SEL = "NonDenotableTest.Selector<? extends #INT(java.lang.Enum<?>,NonDenotableTest.Selector<?>)>";
  52 
  53     void testExtends() {
  54         @InferredType(LIST_EXT)
  55         var s = extString();
  56         for (@InferredType(LIST_EXT) var s2 = extString() ; ; ) { break; }
  57         for (@InferredType(LIST_EXT) var s2 : extStringArr()) { break; }
  58         for (@InferredType(LIST_EXT) var s2 : extStringIter()) { break; }
  59         for (@InferredType(STRING) var s2 : extString()) { break; }
  60     }
  61 
  62     void testExtendsFbound() {
  63         @InferredType(LIST_EXT_COMP_UNB)
  64         var s = extFbound();
  65         for (@InferredType(LIST_EXT_COMP_UNB) var s2 = extFbound() ; ; ) { break; }
  66         for (@InferredType(LIST_EXT_COMP_UNB) var s2 : extFboundArr()) { break; }
  67         for (@InferredType(LIST_EXT_COMP_UNB) var s2 : extFboundIter()) { break; }
  68         for (@InferredType(COMP_UNB) var s2 : extFbound()) { break; }
  69     }
  70 
  71     void testSuperFbound() {
  72         @InferredType(LIST_UNB)
  73         var s = supFbound();
  74         for (@InferredType(LIST_UNB) var s2 = supFbound() ; ; ) { break; }
  75         for (@InferredType(LIST_UNB) var s2 : supFboundArr()) { break; }
  76         for (@InferredType(LIST_UNB) var s2 : supFboundIter()) { break; }
  77         for (@InferredType(OBJECT) var s2 : supFbound()) { break; }
  78     }
  79 
  80     void testSuper() {
  81         @InferredType(LIST_SUP)
  82         var s = supString();
  83         for (@InferredType(LIST_SUP) var s2 = supString() ; ; ) { break; }
  84         for (@InferredType(LIST_SUP) var s2 : supStringArr()) { break; }
  85         for (@InferredType(LIST_SUP) var s2 : supStringIter()) { break; }
  86         for (@InferredType(OBJECT) var s2 : supString()) { break; }
  87     }
  88 
  89     void testUnbound() {
  90         @InferredType(LIST_UNB)
  91         var s = unbString();
  92         for (@InferredType(LIST_UNB) var s2 = unbString() ; ; ) { break; }
  93         for (@InferredType(LIST_UNB) var s2 : unbStringArr()) { break; }
  94         for (@InferredType(LIST_UNB) var s2 : unbStringIter()) { break; }
  95         for (@InferredType(OBJECT) var s2 : unbString()) { break; }
  96     }
  97 
  98     void testAnonymousClass() {
  99         @InferredType(ANON_OBJECT)
 100         var o = new Object() { };
 101         for (@InferredType(ANON_OBJECT) var s2 = new Object() { } ; ; ) { break; }
 102         for (@InferredType(ANON_OBJECT) var s2 : arrayOf(new Object() { })) { break; }
 103         for (@InferredType(ANON_OBJECT) var s2 : listOf(new Object() { })) { break; }
 104     }
 105 
 106     void testAnonymousInterface() {
 107         @InferredType(ANON_RUNNABLE)
 108         var r = new Runnable() { public void run() { } };
 109         for (@InferredType(ANON_RUNNABLE) var s2 = new Runnable() { public void run() { } } ; ; ) { break; }
 110         for (@InferredType(ANON_RUNNABLE) var s2 : arrayOf(new Runnable() { public void run() { } })) { break; }
 111         for (@InferredType(ANON_RUNNABLE) var s2 : listOf(new Runnable() { public void run() { } })) { break; }
 112     }
 113 
 114     void testIntersection() {
 115         @InferredType(INT_INTEGER_DOUBLE)
 116         var c = choose(1, 1L);
 117         for (@InferredType(INT_INTEGER_DOUBLE) var s2 = choose(1, 1L) ; ;) { break; }
 118         for (@InferredType(INT_INTEGER_DOUBLE) var s2 : arrayOf(choose(1, 1L))) { break; }
 119         for (@InferredType(INT_INTEGER_DOUBLE) var s2 : listOf(choose(1, 1L))) { break; }
 120     }
 121 
 122     void testIntersection(Selector<?> s) {
 123         @InferredType(SEL_INT_ENUM_SEL)
 124         var c = s;
 125         for (@InferredType(SEL_INT_ENUM_SEL) var s2 = s ; ;) { break; }
 126     }
 127 
 128     List<? extends String> extString() { return null; }
 129     List<? super String> supString() { return null; }
 130     List<?> unbString() { return null; }
 131 
 132     List<? extends String>[] extStringArr() { return null; }
 133     List<? super String>[] supStringArr() { return null; }
 134     List<?>[] unbStringArr() { return null; }
 135 
 136     Iterable<? extends List<? extends String>> extStringIter() { return null; }
 137     Iterable<? extends List<? super String>> supStringIter() { return null; }
 138     Iterable<? extends List<?>> unbStringIter() { return null; }
 139 
 140     <Z extends Comparable<Z>> List<? extends Z> extFbound() { return null; }
 141     <Z extends Comparable<Z>> List<? super Z> supFbound() { return null; }
 142 
 143     <Z extends Comparable<Z>> List<? extends Z>[] extFboundArr() { return null; }
 144     <Z extends Comparable<Z>> List<? super Z>[] supFboundArr() { return null; }
 145 
 146     <Z extends Comparable<Z>> Iterable<? extends List<? extends Z>> extFboundIter() { return null; }
 147     <Z extends Comparable<Z>> Iterable<? extends List<? super Z>> supFboundIter() { return null; }
 148 
 149     <Z> List<Z> listOf(Z z) { return null; }
 150     <Z> Z[] arrayOf(Z z) { return null; }
 151 
 152     <Z> Z choose(Z z1, Z z2) { return z1; }
 153 
 154     interface Selector<E extends Enum<E> & Selector<E>> {}
 155 }