1: //: generics/Wildcards.java
2: // Exploring the meaning of wildcards.
3:
4: public class Wildcards {
5: // Raw argument:
6: static void rawArgs(Holder holder, Object arg) {
7: // holder.set(arg); // Warning:
8: // Unchecked call to set(T) as a
9: // member of the raw type Holder
10: // holder.set(new Wildcards()); // Same warning
11:
12: // Can't do this; don't have any 'T':
13: // T t = holder.get();
14:
15: // OK, but type information has been lost:
16: Object obj = holder.get();
17: }
18: // Similar to rawArgs(), but errors instead of warnings:
19: static void unboundedArg(Holder<?> holder, Object arg) {
20: // holder.set(arg); // Error:
21: // set(capture of ?) in Holder<capture of ?>
22: // cannot be applied to (Object)
23: // holder.set(new Wildcards()); // Same error
24:
25: // Can't do this; don't have any 'T':
26: // T t = holder.get();
27:
28: // OK, but type information has been lost:
29: Object obj = holder.get();
30: }
31: static <T> T exact1(Holder<T> holder) {
32: T t = holder.get();
33: return t;
34: }
35: static <T> T exact2(Holder<T> holder, T arg) {
36: holder.set(arg);
37: T t = holder.get();
38: return t;
39: }
40: static <T>
41: T wildSubtype(Holder<? extends T> holder, T arg) {
42: // holder.set(arg); // Error:
43: // set(capture of ? extends T) in
44: // Holder<capture of ? extends T>
45: // cannot be applied to (T)
46: T t = holder.get();
47: return t;
48: }
49: static <T>
50: void wildSupertype(Holder<? super T> holder, T arg) {
51: holder.set(arg);
52: // T t = holder.get(); // Error:
53: // Incompatible types: found Object, required T
54:
55: // OK, but type information has been lost:
56: Object obj = holder.get();
57: }
58: public static void main(String[] args) {
59: Holder raw = new Holder<Long>();
60: // Or:
61: raw = new Holder();
62: Holder<Long> qualified = new Holder<Long>();
63: Holder<?> unbounded = new Holder<Long>();
64: Holder<? extends Long> bounded = new Holder<Long>();
65: Long lng = 1L;
66:
67: rawArgs(raw, lng);
68: rawArgs(qualified, lng);
69: rawArgs(unbounded, lng);
70: rawArgs(bounded, lng);
71:
72: unboundedArg(raw, lng);
73: unboundedArg(qualified, lng);
74: unboundedArg(unbounded, lng);
75: unboundedArg(bounded, lng);
76:
77: // Object r1 = exact1(raw); // Warnings:
78: // Unchecked conversion from Holder to Holder<T>
79: // Unchecked method invocation: exact1(Holder<T>)
80: // is applied to (Holder)
81: Long r2 = exact1(qualified);
82: Object r3 = exact1(unbounded); // Must return Object
83: Long r4 = exact1(bounded);
84:
85: // Long r5 = exact2(raw, lng); // Warnings:
86: // Unchecked conversion from Holder to Holder<Long>
87: // Unchecked method invocation: exact2(Holder<T>,T)
88: // is applied to (Holder,Long)
89: Long r6 = exact2(qualified, lng);
90: // Long r7 = exact2(unbounded, lng); // Error:
91: // exact2(Holder<T>,T) cannot be applied to
92: // (Holder<capture of ?>,Long)
93: // Long r8 = exact2(bounded, lng); // Error:
94: // exact2(Holder<T>,T) cannot be applied
95: // to (Holder<capture of ? extends Long>,Long)
96:
97: // Long r9 = wildSubtype(raw, lng); // Warnings:
98: // Unchecked conversion from Holder
99: // to Holder<? extends Long>
100: // Unchecked method invocation:
101: // wildSubtype(Holder<? extends T>,T) is
102: // applied to (Holder,Long)
103: Long r10 = wildSubtype(qualified, lng);
104: // OK, but can only return Object:
105: Object r11 = wildSubtype(unbounded, lng);
106: Long r12 = wildSubtype(bounded, lng);
107:
108: // wildSupertype(raw, lng); // Warnings:
109: // Unchecked conversion from Holder
110: // to Holder<? super Long>
111: // Unchecked method invocation:
112: // wildSupertype(Holder<? super T>,T)
113: // is applied to (Holder,Long)
114: wildSupertype(qualified, lng);
115: // wildSupertype(unbounded, lng); // Error:
116: // wildSupertype(Holder<? super T>,T) cannot be
117: // applied to (Holder<capture of ?>,Long)
118: // wildSupertype(bounded, lng); // Error:
119: // wildSupertype(Holder<? super T>,T) cannot be
120: // applied to (Holder<capture of ? extends Long>,Long)
121: }
122: } ///:~