aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2007, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 4607420 aoqi@0: * @summary A bug in the original JSR14 generics specification aoqi@0: * created a loophole in the type system. aoqi@0: * aoqi@0: * @compile/fail Nonlinear.java aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: public class Nonlinear { aoqi@0: aoqi@0: // This is an example of lack of type safety for aoqi@0: // the version of javac from jsr14_adding_generics-1_0-ea aoqi@0: aoqi@0: // It is a variant of the "classic" problem with polymorphic aoqi@0: // references in SML, which resulted in the usual array of aoqi@0: // fixes: notably value polymorphism. aoqi@0: aoqi@0: // This code compiles, but produces a ClassCastException aoqi@0: // when executed, even though there are no explicit casts in aoqi@0: // the program. aoqi@0: aoqi@0: public static void main (String [] args) { aoqi@0: Integer x = new Integer (5); aoqi@0: String y = castit (x); aoqi@0: System.out.println (y); aoqi@0: } aoqi@0: aoqi@0: static A castit (B x) { aoqi@0: // This method casts any type to any other type. aoqi@0: // Oh dear. This shouldn't type check, but does aoqi@0: // because build () returns a type Ref<*> aoqi@0: // which is a subtype of RWRef. aoqi@0: final RWRef r = build (); aoqi@0: r.set (x); aoqi@0: return r.get (); aoqi@0: } aoqi@0: aoqi@0: static Ref build () { aoqi@0: return new Ref (); aoqi@0: } aoqi@0: aoqi@0: // Another way of doing this is a variant of the crackit aoqi@0: // example discussed in the draft specification. aoqi@0: // aoqi@0: // The original duplicate was: aoqi@0: // aoqi@0: // static Pair duplicate (A x) { aoqi@0: // return new Pair (x,x); aoqi@0: // } aoqi@0: // aoqi@0: // which breaks the requirement that a type variable aoqi@0: // instantiated by * only occurs once in the result type. aoqi@0: // aoqi@0: // However, we can achieve the same result with a different aoqi@0: // type for duplicate, which uses its type variables linearly aoqi@0: // in the result: aoqi@0: aoqi@0: static > Pair,B> duplicate (B x) { aoqi@0: return new Pair,B> (x,x); aoqi@0: } aoqi@0: aoqi@0: // the cheat here is that A and B are used linearly in the result aoqi@0: // type, but not in the polymorphic bounds. aoqi@0: aoqi@0: // We can use that to give an alternative implementation of aoqi@0: // castit. aoqi@0: aoqi@0: static A castit2 (B x) { aoqi@0: Pair , Ref> p = duplicate (build ()); aoqi@0: p.snd.set (x); aoqi@0: return p.fst.get (); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: } aoqi@0: aoqi@0: interface RWRef { aoqi@0: aoqi@0: public A get (); aoqi@0: public void set (B x); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: class Ref implements RWRef { aoqi@0: aoqi@0: A contents; aoqi@0: aoqi@0: public void set (A x) { contents = x; } aoqi@0: public A get () { return contents; } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: class Pair { aoqi@0: aoqi@0: final A fst; aoqi@0: final B snd; aoqi@0: aoqi@0: Pair (A fst, B snd) { this.fst = fst; this.snd = snd; } aoqi@0: aoqi@0: }