langtools/test/tools/javac/generics/Nonlinear.java
author darcy
Wed, 27 May 2009 22:34:43 -0700
changeset 2985 f43e1241e7fb
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6843761: Update langtools tests to remove unncessary -source and -target options Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 4607420
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary A bug in the original JSR14 generics specification
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 *          created a loophole in the type system.
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 *
2985
f43e1241e7fb 6843761: Update langtools tests to remove unncessary -source and -target options
darcy
parents: 10
diff changeset
    30
 * @compile/fail  Nonlinear.java
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
public class Nonlinear {
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
    // This is an example of lack of type safety for
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    // the version of javac from jsr14_adding_generics-1_0-ea
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
    // It is a variant of the "classic" problem with polymorphic
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    // references in SML, which resulted in the usual array of
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    // fixes: notably value polymorphism.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    // This code compiles, but produces a ClassCastException
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    // when executed, even though there are no explicit casts in
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    // the program.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public static void main (String [] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        Integer x = new Integer (5);
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        String y = castit (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        System.out.println (y);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    static <A,B> A castit (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        // This method casts any type to any other type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        // Oh dear.  This shouldn't type check, but does
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        // because build () returns a type Ref<*>
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        // which is a subtype of RWRef<A,B>.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        final RWRef<A,B> r = build ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        r.set (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        return r.get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    static <A> Ref<A> build () {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        return new Ref<A> ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    // Another way of doing this is a variant of the crackit
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    // example discussed in the draft specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    // The original duplicate was:
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    // static <A> Pair <A,A> duplicate (A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    //     return new Pair<A,A> (x,x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    // }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    // which breaks the requirement that a type variable
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    // instantiated by * only occurs once in the result type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    // However, we can achieve the same result with a different
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    // type for duplicate, which uses its type variables linearly
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    // in the result:
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    static <A,B extends Ref<A>> Pair<Ref<A>,B> duplicate (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return new Pair<Ref<A>,B> (x,x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    // the cheat here is that A and B are used linearly in the result
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    // type, but not in the polymorphic bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    // We can use that to give an alternative implementation of
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    // castit.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    static <A,B> A castit2 (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        Pair <Ref<A>, Ref<B>> p = duplicate (build ());
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        p.snd.set (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return p.fst.get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
interface RWRef<A,B> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public A get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public void set (B x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
class Ref<A> implements RWRef <A,A> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    A contents;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public void set (A x) { contents = x; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public A get () { return contents; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
class Pair<A,B> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    final A fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    final B snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    Pair (A fst, B snd) { this.fst = fst; this.snd = snd; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
}