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