1 /*******************************************************************************
2  * Copyright (c) 2000, 2014 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
14  *								bug 354536 - compiling package-info.java still depends on the order of compilation units
15  *								bug 384870 - [compiler] @Deprecated annotation not detected if preceded by other annotation
16  *******************************************************************************/
17 package org.eclipse.jdt.core.tests.compiler.regression;
18 
19 import java.util.HashMap;
20 import java.util.Map;
21 
22 import junit.framework.Test;
23 
24 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
25 
26 @SuppressWarnings({ "unchecked", "rawtypes" })
27 public class Deprecated15Test extends AbstractRegressionTest {
Deprecated15Test(String name)28 public Deprecated15Test(String name) {
29 	super(name);
30 }
suite()31 public static Test suite() {
32 	return buildMinimalComplianceTestSuite(testClass(), F_1_5);
33 }
test001()34 public void test001() {
35 	Map options = getCompilerOptions();
36 	options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.WARNING);
37 	this.runNegativeTest(
38 		new String[] {
39 			"p/X.java",
40 			"package p;\n" +
41 			"/**\n" +
42 			" * @deprecated\n" +
43 			" */\n" +
44 			"public class X<T> {\n" +
45 			"}\n",
46 			"Y.java",
47 			"import p.X;\n" +
48 			"public class Y {\n" +
49 			"  Zork z;\n" +
50 			"  void foo() {\n" +
51 			"    X x;\n" +
52 			"    X[] xs = { x };\n" +
53 			"  }\n" +
54 			"  void bar() {\n" +
55 			"    p.X x;\n" +
56 			"    p.X[] xs = { x };\n" +
57 			"  }\n" +
58 			"}\n",
59 		},
60 		"----------\n" +
61 		"1. WARNING in Y.java (at line 1)\n" +
62 		"	import p.X;\n" +
63 		"	       ^^^\n" +
64 		"The type X<T> is deprecated\n" +
65 		"----------\n" +
66 		"2. ERROR in Y.java (at line 3)\n" +
67 		"	Zork z;\n" +
68 		"	^^^^\n" +
69 		"Zork cannot be resolved to a type\n" +
70 		"----------\n" +
71 		"3. WARNING in Y.java (at line 5)\n" +
72 		"	X x;\n" +
73 		"	^\n" +
74 		"The type X<T> is deprecated\n" +
75 		"----------\n" +
76 		"4. WARNING in Y.java (at line 5)\n" +
77 		"	X x;\n" +
78 		"	^\n" +
79 		"X is a raw type. References to generic type X<T> should be parameterized\n" +
80 		"----------\n" +
81 		"5. WARNING in Y.java (at line 6)\n" +
82 		"	X[] xs = { x };\n" +
83 		"	^\n" +
84 		"The type X<T> is deprecated\n" +
85 		"----------\n" +
86 		"6. WARNING in Y.java (at line 6)\n" +
87 		"	X[] xs = { x };\n" +
88 		"	^\n" +
89 		"X is a raw type. References to generic type X<T> should be parameterized\n" +
90 		"----------\n" +
91 		"7. WARNING in Y.java (at line 9)\n" +
92 		"	p.X x;\n" +
93 		"	^^^\n" +
94 		"X is a raw type. References to generic type X<T> should be parameterized\n" +
95 		"----------\n" +
96 		"8. WARNING in Y.java (at line 9)\n" +
97 		"	p.X x;\n" +
98 		"	  ^\n" +
99 		"The type X<T> is deprecated\n" +
100 		"----------\n" +
101 		"9. WARNING in Y.java (at line 10)\n" +
102 		"	p.X[] xs = { x };\n" +
103 		"	^^^\n" +
104 		"X is a raw type. References to generic type X<T> should be parameterized\n" +
105 		"----------\n" +
106 		"10. WARNING in Y.java (at line 10)\n" +
107 		"	p.X[] xs = { x };\n" +
108 		"	  ^\n" +
109 		"The type X<T> is deprecated\n" +
110 		"----------\n",
111 		null,
112 		true,
113 		options);
114 }
115 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=159709
116 // guard variant for DeprecatedTest#test015 using an annotation
test002()117 public void test002() {
118 	Map customOptions = new HashMap();
119 	customOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
120 	customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.IGNORE);
121 	this.runNegativeTest(
122 		true,
123 		new String[] {
124 			"p/M1.java",
125 			"package p;\n" +
126 			"public class M1 {\n" +
127 			"  void bar() {\n" +
128 			"    a.N1.N2.N3 m = null;\n" +
129 			"    m.foo();\n" +
130 			"  }\n" +
131 			"}\n",
132 			"a/N1.java",
133 			"package a;\n" +
134 			"public class N1 {\n" +
135 			"  @Deprecated\n" +
136 			"  public class N2 {" +
137 			"    public void foo() {}" +
138 			"    public class N3 {" +
139 			"      public void foo() {}" +
140 			"    }" +
141 			"  }" +
142 			"}\n",
143 		},
144 		null, customOptions,
145 		"----------\n" +
146 		"1. ERROR in p\\M1.java (at line 4)\n" +
147 		"	a.N1.N2.N3 m = null;\n" +
148 		"	     ^^\n" +
149 		"The type N1.N2 is deprecated\n" +
150 		"----------\n" +
151 		"2. ERROR in p\\M1.java (at line 4)\n" +
152 		"	a.N1.N2.N3 m = null;\n" +
153 		"	        ^^\n" +
154 		"The type N1.N2.N3 is deprecated\n" +
155 		"----------\n" +
156 		"3. ERROR in p\\M1.java (at line 5)\n" +
157 		"	m.foo();\n" +
158 		"	  ^^^^^\n" +
159 		"The method foo() from the type N1.N2.N3 is deprecated\n" +
160 		"----------\n",
161 		JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
162 }
163 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=161214
164 // shows that Member2 is properly tagged as deprecated (use the debugger, since
165 // we do not report deprecation in the unit where the deprecated type is
166 // declared anyway)
test003()167 public void test003() {
168 	Map customOptions = new HashMap();
169 	customOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
170 	this.runConformTest(
171 		new String[] {
172 			"X.java",
173 			"class X {\n" +
174 			"  void foo() {\n" +
175 			"    class Local {\n" +
176 			"      class Member1 {\n" +
177 			"        void bar() {\n" +
178 			"          Member2 m2; // Member2 is deprecated\n" +
179 			"        }\n" +
180 			"      }\n" +
181 			"      @Deprecated\n" +
182 			"      class Member2 {\n" +
183 			"      }\n" +
184 			"    }\n" +
185 			"  }\n" +
186 			"}\n"
187 		},
188 		"",
189 		null,
190 		true,
191 		null,
192 		customOptions,
193 		null);
194 }
195 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=191909
test004()196 public void test004() {
197 	Map customOptions = new HashMap();
198 	customOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
199 	this.runNegativeTest(
200 		true,
201 		new String[] {
202 			"test1/E01.java",
203 			"package test1;\n" +
204 			"public class E01 {\n" +
205 			"	@Deprecated\n" +
206 			"	public static int x = 5, y= 10;\n" +
207 			"}",
208 			"test1/E02.java",
209 			"package test1;\n" +
210 			"public class E02 {\n" +
211 			"	public void foo() {\n" +
212 			"		System.out.println(E01.x);\n" +
213 			"		System.out.println(E01.y);\n" +
214 			"	}\n" +
215 			"}"
216 		},
217 		null, customOptions,
218 		"----------\n" +
219 		"1. ERROR in test1\\E02.java (at line 4)\n" +
220 		"	System.out.println(E01.x);\n" +
221 		"	                       ^\n" +
222 		"The field E01.x is deprecated\n" +
223 		"----------\n" +
224 		"2. ERROR in test1\\E02.java (at line 5)\n" +
225 		"	System.out.println(E01.y);\n" +
226 		"	                       ^\n" +
227 		"The field E01.y is deprecated\n" +
228 		"----------\n",
229 		JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
230 }
231 // Bug 354536 - compiling package-info.java still depends on the order of compilation units
test005()232 public void test005() {
233 	Map customOptions = new HashMap();
234 	customOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
235 	this.runNegativeTest(
236 		true,
237 		new String[] {
238 			"p1/X.java",
239 			"package p1;\n" +
240 			"public class X {\n" +
241 			"    public static class Inner {" +
242 			"        public void foo() {}\n" +
243 			"    }\n" +
244 			"}\n",
245 			"p1/package-info.java",
246 			"@java.lang.Deprecated\n" +
247 			"package p1;\n",
248 			"p2/C.java",
249 			"package p2;\n" +
250 			"public class C {\n" +
251 			"    void bar(p1.X.Inner a) {\n" +
252 			"        a.foo();\n" +
253 			"    }\n" +
254 			"}\n",
255 		},
256 		null, customOptions,
257 		"----------\n" +
258 		"1. ERROR in p2\\C.java (at line 3)\n" +
259 		"	void bar(p1.X.Inner a) {\n" +
260 		"	            ^\n" +
261 		"The type X is deprecated\n" +
262 		"----------\n" +
263 		"2. ERROR in p2\\C.java (at line 3)\n" +
264 		"	void bar(p1.X.Inner a) {\n" +
265 		"	              ^^^^^\n" +
266 		"The type X.Inner is deprecated\n" +
267 		"----------\n" +
268 		"3. ERROR in p2\\C.java (at line 4)\n" +
269 		"	a.foo();\n" +
270 		"	  ^^^^^\n" +
271 		"The method foo() from the type X.Inner is deprecated\n" +
272 		"----------\n",
273 		JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
274 }
275 // https://bugs.eclipse.org/384870 - [compiler] @Deprecated annotation not detected if preceded by other annotation
test006()276 public void test006() {
277 	Map customOptions = new HashMap();
278 	customOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
279 	this.runNegativeTest(
280 		true,
281 		new String[] {
282 			"test1/E02.java",
283 			"package test1;\n" +
284 			"public class E02 {\n" +
285 			"	public void foo(E01 arg) {\n" +
286 			"		// nop\n" +
287 			"	}\n" +
288 			"}",
289 			"test1/E01.java",
290 			"package test1;\n" +
291 			"@SuppressWarnings(\"all\") @Deprecated\n" +
292 			"public class E01 {\n" +
293 			"	public static int x = 5;\n" +
294 			"}"
295 		},
296 		null, customOptions,
297 		"----------\n" +
298 		"1. ERROR in test1\\E02.java (at line 3)\n" +
299 		"	public void foo(E01 arg) {\n" +
300 		"	                ^^^\n" +
301 		"The type E01 is deprecated\n" +
302 		"----------\n",
303 		JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
304 }
testClass()305 public static Class testClass() {
306 	return Deprecated15Test.class;
307 }
308 }
309