langtools/test/tools/javac/generics/Nonlinear.java
author sogoel
Fri, 15 May 2015 17:43:21 -0700
changeset 30724 0686b5ea7958
parent 5520 86e4b9a9da40
child 37636 6c6e6e25189d
permissions -rw-r--r--
8074514: Group 13d: golden files for tests in tools/javac/generics dir Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
30724
0686b5ea7958 8074514: Group 13d: golden files for tests in tools/javac/generics dir
sogoel
parents: 5520
diff changeset
     2
 * @test /nodynamiccopyright/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * @bug 4607420
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 * @summary A bug in the original JSR14 generics specification
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 *          created a loophole in the type system.
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 *
30724
0686b5ea7958 8074514: Group 13d: golden files for tests in tools/javac/generics dir
sogoel
parents: 5520
diff changeset
     7
 * @compile/fail/ref=Nonlinear.out -XDrawDiagnostics  Nonlinear.java
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
public class Nonlinear {
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
    // This is an example of lack of type safety for
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
    // the version of javac from jsr14_adding_generics-1_0-ea
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
    // It is a variant of the "classic" problem with polymorphic
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
    // references in SML, which resulted in the usual array of
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
    // fixes: notably value polymorphism.
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
    // This code compiles, but produces a ClassCastException
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
    // when executed, even though there are no explicit casts in
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
    // the program.
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
    public static void main (String [] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
        Integer x = new Integer (5);
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
        String y = castit (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
        System.out.println (y);
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
    static <A,B> A castit (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
        // This method casts any type to any other type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
        // Oh dear.  This shouldn't type check, but does
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
        // because build () returns a type Ref<*>
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
        // which is a subtype of RWRef<A,B>.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
        final RWRef<A,B> r = build ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
        r.set (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
        return r.get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    static <A> Ref<A> build () {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
        return new Ref<A> ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    // Another way of doing this is a variant of the crackit
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    // example discussed in the draft specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    // The original duplicate was:
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    // static <A> Pair <A,A> duplicate (A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    //     return new Pair<A,A> (x,x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    // }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    // which breaks the requirement that a type variable
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    // instantiated by * only occurs once in the result type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    //
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    // However, we can achieve the same result with a different
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    // type for duplicate, which uses its type variables linearly
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    // in the result:
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    static <A,B extends Ref<A>> Pair<Ref<A>,B> duplicate (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        return new Pair<Ref<A>,B> (x,x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    // the cheat here is that A and B are used linearly in the result
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    // type, but not in the polymorphic bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    // We can use that to give an alternative implementation of
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    // castit.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    static <A,B> A castit2 (B x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        Pair <Ref<A>, Ref<B>> p = duplicate (build ());
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        p.snd.set (x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        return p.fst.get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
}
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
interface RWRef<A,B> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public A get ();
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    public void set (B x);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
}
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
class Ref<A> implements RWRef <A,A> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    A contents;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    public void set (A x) { contents = x; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    public A get () { return contents; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
}
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
class Pair<A,B> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    final A fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    final B snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    Pair (A fst, B snd) { this.fst = fst; this.snd = snd; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
}