< prev index next >

langtools/test/tools/javac/generics/Nonlinear.java

Print this page




   5  *          created a loophole in the type system.
   6  *
   7  * @compile/fail/ref=Nonlinear.out -XDrawDiagnostics  Nonlinear.java
   8  */
   9 
  10 
  11 public class Nonlinear {
  12 
  13     // This is an example of lack of type safety for
  14     // the version of javac from jsr14_adding_generics-1_0-ea
  15 
  16     // It is a variant of the "classic" problem with polymorphic
  17     // references in SML, which resulted in the usual array of
  18     // fixes: notably value polymorphism.
  19 
  20     // This code compiles, but produces a ClassCastException
  21     // when executed, even though there are no explicit casts in
  22     // the program.
  23 
  24     public static void main (String [] args) {
  25         Integer x = new Integer (5);
  26         String y = castit (x);
  27         System.out.println (y);
  28     }
  29 
  30     static <A,B> A castit (B x) {
  31         // This method casts any type to any other type.
  32         // Oh dear.  This shouldn't type check, but does
  33         // because build () returns a type Ref<*>
  34         // which is a subtype of RWRef<A,B>.
  35         final RWRef<A,B> r = build ();
  36         r.set (x);
  37         return r.get ();
  38     }
  39 
  40     static <A> Ref<A> build () {
  41         return new Ref<A> ();
  42     }
  43 
  44     // Another way of doing this is a variant of the crackit
  45     // example discussed in the draft specification.




   5  *          created a loophole in the type system.
   6  *
   7  * @compile/fail/ref=Nonlinear.out -XDrawDiagnostics  Nonlinear.java
   8  */
   9 
  10 
  11 public class Nonlinear {
  12 
  13     // This is an example of lack of type safety for
  14     // the version of javac from jsr14_adding_generics-1_0-ea
  15 
  16     // It is a variant of the "classic" problem with polymorphic
  17     // references in SML, which resulted in the usual array of
  18     // fixes: notably value polymorphism.
  19 
  20     // This code compiles, but produces a ClassCastException
  21     // when executed, even though there are no explicit casts in
  22     // the program.
  23 
  24     public static void main (String [] args) {
  25         Integer x = Integer.valueOf(5);
  26         String y = castit (x);
  27         System.out.println (y);
  28     }
  29 
  30     static <A,B> A castit (B x) {
  31         // This method casts any type to any other type.
  32         // Oh dear.  This shouldn't type check, but does
  33         // because build () returns a type Ref<*>
  34         // which is a subtype of RWRef<A,B>.
  35         final RWRef<A,B> r = build ();
  36         r.set (x);
  37         return r.get ();
  38     }
  39 
  40     static <A> Ref<A> build () {
  41         return new Ref<A> ();
  42     }
  43 
  44     // Another way of doing this is a variant of the crackit
  45     // example discussed in the draft specification.


< prev index next >