Conditional.java revision 0:9a66ca7c79fa
1184902Srwatson/* 2191273Srwatson * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. 3184902Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4184902Srwatson * 5184902Srwatson * This code is free software; you can redistribute it and/or modify it 6184902Srwatson * under the terms of the GNU General Public License version 2 only, as 7184902Srwatson * published by the Free Software Foundation. 8184902Srwatson * 9184902Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT 10184902Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11184902Srwatson * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12184902Srwatson * version 2 for more details (a copy is included in the LICENSE file that 13184902Srwatson * accompanied this code). 14184902Srwatson * 15184902Srwatson * You should have received a copy of the GNU General Public License version 16184902Srwatson * 2 along with this work; if not, write to the Free Software Foundation, 17184902Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18184902Srwatson * 19184902Srwatson * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20184902Srwatson * CA 95054 USA or visit www.sun.com if you need additional information or 21184902Srwatson * have any questions. 22184902Srwatson */ 23184902Srwatson 24184902Srwatson/* 25184902Srwatson * @test 26184902Srwatson * @bug 4881179 4883239 27184902Srwatson * @summary Rule for semantics of ?: in the presence of generics and generic class Class 28184902Srwatson * @author gafter 29244390Srwatson * 30184902Srwatson * @compile -source 1.5 Conditional.java 31184902Srwatson */ 32184902Srwatson 33184902Srwatsonpackage conditional; 34184902Srwatson 35184902Srwatsonimport java.io.Serializable; 36191273Srwatson 37191273Srwatsoninterface I {} 38191273Srwatsoninterface J {} 39191273Srwatsonclass A implements I, J {} 40191273Srwatsonclass B implements I, J {} 41191273Srwatsonclass C extends B {} 42184902Srwatson 43184902Srwatsonclass Conditional { 44184902Srwatson static boolean cond = String.class.getName().length() == 1; 45184902Srwatson public static void main(String[] args) { 46184902Srwatson Class c = cond ? A.class : B.class; 47184902Srwatson Class<?> d = cond ? A.class : B.class; 48184902Srwatson 49184902Srwatson Class<? extends B> e = cond ? B.class : C.class; 50184902Srwatson } 51184902Srwatson 52184902Srwatson void f(A a, B b) { 53184902Srwatson I i = cond ? a : b; 54184902Srwatson J j = cond ? a : b; 55184902Srwatson } 56184902Srwatson 57184902Srwatson // required for compatibility 58184902Srwatson Class g(Class a) { 59184902Srwatson return cond ? a : B.class; 60184902Srwatson } 61184902Srwatson 62184902Srwatson // required for compatibility 63184902Srwatson byte[] h(byte[] a, byte[] b) { 64184902Srwatson return cond ? a : b; 65184902Srwatson } 66184902Srwatson 67184902Srwatson // This one is hard because of the recursive F-bounds 68184902Srwatson // The naive result is the infinite type 69184902Srwatson // Class<? extends Number&Comparable<? extends Number&Comparable<? extends 70184902Srwatson // ... 71184902Srwatson Class<? extends Comparable<?>> c = 72184902Srwatson cond ? Integer.class : Float.class; 73184902Srwatson 74184902Srwatson Comparable<?> o = 75184902Srwatson cond ? true : 3; 76184902Srwatson 77184902Srwatson /* 78184902Srwatson 79184902Srwatson // See 4942040 80184902Srwatson void f(Cloneable a, int[] b) { 81184902Srwatson Cloneable x = cond ? a : b; 82184902Srwatson } 83184902Srwatson void f(Serializable a, int[] b) { 84184902Srwatson Serializable x = cond ? a : b; 85184902Srwatson } 86184902Srwatson 87184902Srwatson // See 4941882 88184902Srwatson void f(float[] a, int[] b) { 89184902Srwatson Serializable x = cond ? a : b; 90184902Srwatson } 91184902Srwatson */ 92184902Srwatson} 93184902Srwatson