View Javadoc
1   package gcUnrelatedTypes;
2   
3   import java.util.ArrayList;
4   
5   import edu.umd.cs.findbugs.annotations.ExpectWarning;
6   
7   public class ArrayListContains<T> {
8   
9   	static class Dummy {
10  
11  	}
12  
13  	static class DummyChild extends Dummy {
14  
15  	}
16  
17  	private ArrayList<?> wildcardF;
18  
19  	private ArrayList<Dummy> dummyF;
20  	private ArrayList<? extends Dummy> dummyEF;
21  	private ArrayList<? super Dummy> dummySF;
22  
23  	private ArrayList<DummyChild> childF;
24  	private ArrayList<? extends DummyChild> childEF;
25  	private ArrayList<? super DummyChild> childSF;
26  
27  	private ArrayList<T> genericF;		
28  	private ArrayList<? extends T> genericEF;
29  	private ArrayList<? super T> genericSF;		
30  
31  	@ExpectWarning("GC")
32  	public ArrayListContains(
33  			ArrayList<?> wildcardF, 
34  			ArrayList<Dummy> dummyF, 
35  			ArrayList<? extends Dummy> dummyEF, 
36  			ArrayList<? super Dummy> dummySF, 
37  			ArrayList<DummyChild> childF, 
38  			ArrayList<? extends DummyChild> childEF, 
39  			ArrayList<? super DummyChild> childSF, 
40  			ArrayList<T> genericF, 
41  			ArrayList<? extends T> genericEF, 
42  			ArrayList<? super T> genericSF) {
43  		this.wildcardF = wildcardF;
44  		this.dummyF = dummyF;
45  		this.dummyEF = dummyEF;
46  		this.dummySF = dummySF;
47  		this.childF = childF;
48  		this.childEF = childEF;
49  		this.childSF = childSF;
50  		this.genericF = genericF;
51  		this.genericEF = genericEF;
52  		this.genericSF = genericSF;
53  
54  		Dummy dummy = new Dummy();
55  		DummyChild dummyChild = new DummyChild();
56  		String s = "Mismatched Type";
57  
58  		wildcardF.contains(dummy); 				// No warning
59  		wildcardF.contains(dummyChild); 		// No warning
60  		wildcardF.contains(s); 					// No warning
61  
62  		dummyF.contains(dummy); 				// No warning
63  		dummyF.contains(dummyChild); 		 	// No warning
64  		dummyF.contains(s); 					// HIGH
65  
66  		dummyEF.contains(dummy); 				// No warning
67  		dummyEF.contains(dummyChild); 		 	// No warning
68  		dummyEF.contains(s); 					// HIGH
69  
70  		dummySF.contains(dummy); 				// No warning
71  		dummySF.contains(dummyChild); 		 	// No warning
72  		dummySF.contains(s); 					// HIGH
73  
74  		childF.contains(dummy); 				// No warning
75  		childF.contains(dummyChild); 		 	// No warning
76  		childF.contains(s); 					// HIGH
77  
78  		childEF.contains(dummy); 				// No warning
79  		childEF.contains(dummyChild); 		 	// No warning
80  		childEF.contains(s); 					// HIGH
81  
82  		childSF.contains(dummy); 				// No warning
83  		childSF.contains(dummyChild); 		 	// No warning
84  		childSF.contains(s); 					// HIGH
85  
86  		genericF.contains(dummy); 				// No warning
87  		genericF.contains(dummyChild); 		 	// No warning
88  		genericF.contains(s); 					// No warning
89  
90  		genericEF.contains(dummy); 				// No warning
91  		genericEF.contains(dummyChild); 		    // No warning
92  		genericEF.contains(s); 					// No warning
93  
94  		genericSF.contains(dummy); 				// No warning
95  		genericSF.contains(dummyChild); 		// No warning
96  		genericSF.contains(s); 					// No warning
97  
98  	}
99  
100 	@ExpectWarning("GC")
101 	public void testFields() {
102 		Dummy dummy = new Dummy();
103 		DummyChild dummyChild = new DummyChild();
104 		String s = "Mismatched Type";
105 
106 		wildcardF.contains(dummy); 				// No warning
107 		wildcardF.contains(dummyChild); 		// No warning
108 		wildcardF.contains(s); 					// No warning
109 
110 		dummyF.contains(dummy); 				// No warning
111 		dummyF.contains(dummyChild); 		 	// No warning
112 		dummyF.contains(s); 					// HIGH
113 
114 		dummyEF.contains(dummy); 				// No warning
115 		dummyEF.contains(dummyChild); 		 	// No warning
116 		dummyEF.contains(s); 					// HIGH
117 
118 		dummySF.contains(dummy); 				// No warning
119 		dummySF.contains(dummyChild); 		 	// No warning
120 		dummySF.contains(s); 					// HIGH
121 
122 		childF.contains(dummy); 				// No warning
123 		childF.contains(dummyChild); 		 	// No warning
124 		childF.contains(s); 					// HIGH
125 
126 		childEF.contains(dummy); 				// No warning
127 		childEF.contains(dummyChild); 		 	// No warning
128 		childEF.contains(s); 					// HIGH
129 
130 		childSF.contains(dummy); 				// No warning
131 		childSF.contains(dummyChild); 		 	// No warning
132 		childSF.contains(s); 					// HIGH
133 
134 		genericF.contains(dummy); 				// No warning
135 		genericF.contains(dummyChild); 		 	// No warning
136 		genericF.contains(s); 					// No warning
137 
138 		genericEF.contains(dummy); 				// No warning
139 		genericEF.contains(dummyChild); 		// No warning
140 		genericEF.contains(s); 					// No warning
141 
142 		genericSF.contains(dummy); 				// No warning
143 		genericSF.contains(dummyChild); 		// No warning
144 		genericSF.contains(s); 					// No warning
145 
146 		// what if <T extends Dummy> in class definition? 
147 		// Requires more analysis -- a future enhancement
148 		// Can we be sure that the T is the same as the one in the class/method definition
149 
150 	}
151 }