1 package net.sourceforge.pmd.ast;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.lang.java.ast.ParseException;
5 import net.sourceforge.pmd.testframework.ParserTst;
6
7 import org.junit.Test;
8
9
10 public class JDKVersionTest extends ParserTst {
11
12
13 @Test(expected = ParseException.class)
14 public void testEnumAsKeywordShouldFailWith14() throws Throwable {
15 parseJava15(JDK14_ENUM);
16 }
17
18 @Test
19 public void testEnumAsIdentifierShouldPassWith14() throws Throwable {
20 parseJava14(JDK14_ENUM);
21 }
22
23 @Test
24 public void testEnumAsKeywordShouldPassWith15() throws Throwable {
25 parseJava15(JDK15_ENUM);
26 }
27
28 @Test(expected = ParseException.class)
29 public void testEnumAsIdentifierShouldFailWith15() throws Throwable {
30 parseJava15(JDK14_ENUM);
31 }
32
33
34
35 @Test
36 public void testAssertAsKeywordVariantsSucceedWith1_4() {
37 parseJava14(ASSERT_TEST1);
38 parseJava14(ASSERT_TEST2);
39 parseJava14(ASSERT_TEST3);
40 parseJava14(ASSERT_TEST4);
41 }
42
43 @Test(expected = ParseException.class)
44 public void testAssertAsVariableDeclIdentifierFailsWith1_4() {
45 parseJava14(ASSERT_TEST5);
46 }
47
48 @Test(expected = ParseException.class)
49 public void testAssertAsMethodNameIdentifierFailsWith1_4() {
50 parseJava14(ASSERT_TEST7);
51 }
52
53 @Test
54 public void testAssertAsIdentifierSucceedsWith1_3() {
55 parseJava13(ASSERT_TEST5);
56 }
57
58 @Test(expected = ParseException.class)
59 public void testAssertAsKeywordFailsWith1_3() {
60 parseJava13(ASSERT_TEST6);
61 }
62
63
64 @Test
65 public void testVarargsShouldPassWith15() throws Throwable {
66 parseJava15(JDK15_VARARGS);
67 }
68
69 @Test(expected = ParseException.class)
70 public void testVarargsShouldFailWith14() throws Throwable {
71 parseJava14(JDK15_VARARGS);
72 }
73
74 @Test
75 public void testJDK15ForLoopSyntaxShouldPassWith15() throws Throwable {
76 parseJava15(JDK15_FORLOOP);
77 }
78
79 @Test
80 public void testJDK15ForLoopSyntaxWithModifiers() throws Throwable {
81 parseJava15(JDK15_FORLOOP_WITH_MODIFIER);
82 }
83
84 @Test(expected = ParseException.class)
85 public void testJDK15ForLoopShouldFailWith14() throws Throwable {
86 parseJava14(JDK15_FORLOOP);
87 }
88
89 @Test
90 public void testJDK15GenericsSyntaxShouldPassWith15() throws Throwable {
91 parseJava15(JDK15_GENERICS);
92 }
93
94 @Test
95 public void testVariousParserBugs() throws Throwable {
96 parseJava15(FIELDS_BUG);
97 parseJava15(GT_BUG);
98 parseJava15(ANNOTATIONS_BUG);
99 parseJava15(CONSTANT_FIELD_IN_ANNOTATION_BUG);
100 parseJava15(GENERIC_IN_FIELD);
101 }
102
103 @Test
104 public void testNestedClassInMethodBug() throws Throwable {
105 parseJava15(INNER_BUG);
106 parseJava15(INNER_BUG2);
107 }
108
109 @Test
110 public void testGenericsInMethodCall() throws Throwable {
111 parseJava15(GENERIC_IN_METHOD_CALL);
112 }
113
114 @Test
115 public void testGenericINAnnotation() throws Throwable {
116 parseJava15(GENERIC_IN_ANNOTATION);
117 }
118
119 @Test
120 public void testGenericReturnType() throws Throwable {
121 parseJava15(GENERIC_RETURN_TYPE);
122 }
123
124 @Test
125 public void testMultipleGenerics() throws Throwable {
126 parseJava15(FUNKY_GENERICS);
127 parseJava15(MULTIPLE_GENERICS);
128 }
129
130 @Test
131 public void testAnnotatedParams() throws Throwable {
132 parseJava15(ANNOTATED_PARAMS);
133 }
134
135 @Test
136 public void testAnnotatedLocals() throws Throwable {
137 parseJava15(ANNOTATED_LOCALS);
138 }
139
140 @Test
141 public void testAssertAsIdentifierSucceedsWith1_3_test2() {
142 parseJava13(ASSERT_TEST5_a);
143 }
144
145 @Test
146 public final void testBinaryAndUnderscoresInNumericalLiterals() throws Throwable {
147 parseJava17(JDK17_NUMERICAL_LITERALS);
148 }
149
150 @Test
151 public final void testStringInSwitch() throws Throwable {
152 parseJava17(JDK17_STRING_IN_SWITCH);
153 }
154
155 @Test
156 public final void testGenericDiamond() throws Throwable {
157 parseJava17(JDK17_GENERIC_DIAMOND);
158 }
159
160 @Test
161 public final void testTryWithResources() throws Throwable {
162 parseJava17(JDK17_TRY_WITH_RESOURCES);
163 }
164
165 @Test
166 public final void testTryWithResourcesSemi() throws Throwable {
167 parseJava17(JDK17_TRY_WITH_RESOURCES_SEMI);
168 }
169
170 @Test
171 public final void testTryWithResourcesMulti() throws Throwable {
172 parseJava17(JDK17_TRY_WITH_RESOURCES_MULTI);
173 }
174
175 @Test
176 public final void testTryWithResourcesWithAnnotations() throws Throwable {
177 parseJava17(JDK17_TRY_WITH_RESOURCES_WITH_ANNOTATIONS);
178 }
179
180 @Test
181 public final void testMulticatch() throws Throwable {
182 parseJava17(JDK17_MULTICATCH);
183 }
184
185 @Test
186 public final void testMulticatchWithAnnotations() throws Throwable {
187 parseJava17(JDK17_MULTICATCH_WITH_ANNOTATIONS);
188 }
189 private static final String ANNOTATED_LOCALS =
190 "public class Foo {" + PMD.EOL +
191 " void bar() {" + PMD.EOL +
192 " @SuppressWarnings(\"foo\") int y = 5;" + PMD.EOL +
193 " }" + PMD.EOL +
194 "}";
195
196 private static final String ANNOTATED_PARAMS =
197 "public class Foo {" + PMD.EOL +
198 " void bar(@SuppressWarnings(\"foo\") int x) {}" + PMD.EOL +
199 "}";
200
201 private static final String ASSERT_TEST1 =
202 "public class Foo {" + PMD.EOL +
203 " void bar() {" + PMD.EOL +
204 " assert x == 2;" + PMD.EOL +
205 " }" + PMD.EOL +
206 "}";
207
208 private static final String ASSERT_TEST2 =
209 "public class Foo {" + PMD.EOL +
210 " void bar() {" + PMD.EOL +
211 " assert (x == 2);" + PMD.EOL +
212 " }" + PMD.EOL +
213 "}";
214
215 private static final String ASSERT_TEST3 =
216 "public class Foo {" + PMD.EOL +
217 " void bar() {" + PMD.EOL +
218 " assert (x==2) : \"hi!\";" + PMD.EOL +
219 " }" + PMD.EOL +
220 "}";
221
222 private static final String ASSERT_TEST4 =
223 "public class Foo {" + PMD.EOL +
224 " void bar() {" + PMD.EOL +
225 " assert (x==2) : \"hi!\";" + PMD.EOL +
226 " }" + PMD.EOL +
227 "}";
228
229 private static final String ASSERT_TEST5 =
230 "public class Foo {" + PMD.EOL +
231 " int assert = 2;" + PMD.EOL +
232 "}";
233
234
235 private static final String ASSERT_TEST5_a =
236 "public class Foo {" + PMD.EOL +
237 " void bar() { assert(); }" + PMD.EOL +
238 "}";
239
240 private static final String ASSERT_TEST6 =
241 "public class Foo {" + PMD.EOL +
242 " void foo() {" + PMD.EOL +
243 " assert (x == 2) : \"hi!\";" + PMD.EOL +
244 " }" + PMD.EOL +
245 "}";
246
247 private static final String ASSERT_TEST7 =
248 "public class Foo {" + PMD.EOL +
249 " void assert() {}" + PMD.EOL +
250 "}";
251
252 private static final String JDK15_ENUM =
253 "public class Test {" + PMD.EOL +
254 " enum Season { winter, spring, summer, fall };" + PMD.EOL +
255 "}";
256
257 private static final String JDK14_ENUM =
258 "public class Test {" + PMD.EOL +
259 " int enum;" + PMD.EOL +
260 "}";
261
262 private static final String JDK15_VARARGS =
263 "public class Test {" + PMD.EOL +
264 " void bar(Object ... args) {}" + PMD.EOL +
265 "}";
266
267 private static final String JDK15_FORLOOP =
268 "public class Test {" + PMD.EOL +
269 " void foo(List list) {" + PMD.EOL +
270 " for (Integer i : list) {}" + PMD.EOL +
271 " }" + PMD.EOL +
272 "}";
273
274 private static final String JDK15_FORLOOP_WITH_MODIFIER =
275 "public class Test {" + PMD.EOL +
276 " void foo(List list) {" + PMD.EOL +
277 " for (final Integer i : list) {}" + PMD.EOL +
278 " }" + PMD.EOL +
279 "}";
280
281 private static final String JDK15_GENERICS =
282 "public class Test {" + PMD.EOL +
283 " ArrayList<Integer> list = new ArrayList<Integer>();" + PMD.EOL +
284 "}";
285
286 private static final String FIELDS_BUG =
287 "public class Test {" + PMD.EOL +
288 " private Foo bar;" + PMD.EOL +
289 "}";
290
291 private static final String GT_BUG =
292 "public class Test {" + PMD.EOL +
293 " int y = x > 32;" + PMD.EOL +
294 "}";
295
296 private static final String ANNOTATIONS_BUG =
297 "@Target(ElementType.METHOD)" + PMD.EOL +
298 "public @interface Foo {" + PMD.EOL +
299 "}";
300
301 private static final String CONSTANT_FIELD_IN_ANNOTATION_BUG =
302 "public @interface Foo {" + PMD.EOL +
303 " String CONST = \"foo\";" + PMD.EOL +
304 "}";
305
306 private static final String GENERIC_IN_FIELD =
307 "public class Foo {" + PMD.EOL +
308 " Class<Double> foo = (Class<Double>)clazz;" + PMD.EOL +
309 "}";
310
311 private static final String GENERIC_IN_ANNOTATION =
312 "public class Foo {" + PMD.EOL +
313 " public <A extends Annotation> A foo(Class<A> c) {" + PMD.EOL +
314 " return null;" + PMD.EOL +
315 " }" + PMD.EOL +
316 "}";
317
318 private static final String INNER_BUG =
319 "public class Test {" + PMD.EOL +
320 " void bar() {" + PMD.EOL +
321 " final class Inner {};" + PMD.EOL +
322 " Inner i = new Inner();" + PMD.EOL +
323 " }" + PMD.EOL +
324 "}";
325
326 private static final String INNER_BUG2 =
327 "public class Test {" + PMD.EOL +
328 " void bar() {" + PMD.EOL +
329 " class Inner {};" + PMD.EOL +
330 " Inner i = new Inner();" + PMD.EOL +
331 " }" + PMD.EOL +
332 "}";
333
334 private static final String GENERIC_IN_METHOD_CALL =
335 "public class Test {" + PMD.EOL +
336 " List<String> test() {" + PMD.EOL +
337 " return Collections.<String>emptyList();" + PMD.EOL +
338 " }" + PMD.EOL +
339 "}";
340
341 private static final String GENERIC_RETURN_TYPE =
342 "public class Test {" + PMD.EOL +
343 " public static <String> String test(String x) {" + PMD.EOL +
344 " return x;" + PMD.EOL +
345 " }" + PMD.EOL +
346 "}";
347
348
349 private static final String MULTIPLE_GENERICS =
350 "public class Foo<K,V> {" + PMD.EOL +
351 " public <A extends K, B extends V> Foo(Bar<A,B> t) {}" + PMD.EOL +
352 "}";
353
354
355 private static final String FUNKY_GENERICS =
356 "public class Foo {" + PMD.EOL +
357 " public <T extends E> Foo() {}" + PMD.EOL +
358 "}";
359
360 private static final String JDK17_NUMERICAL_LITERALS =
361 "public class Test {" + PMD.EOL +
362 " int i1 = 0b00011110;" + PMD.EOL +
363 " int i2 = 0B00011110;" + PMD.EOL +
364 " int i3 = 0xA;" + PMD.EOL +
365 " int i4 = 0x1___A_F;" + PMD.EOL +
366 " int i5 = 0b1;" + PMD.EOL +
367 " int i6 = 0b1___1_0;" + PMD.EOL +
368 " int i7 = 0;" + PMD.EOL +
369 " int i8 = 02;" + PMD.EOL +
370 " int i9 = 0_123;" + PMD.EOL +
371 " int i10 = 1;" + PMD.EOL +
372 " int i11 = 1___3;" + PMD.EOL +
373 " int i12 = 1_43_43598_7;" + PMD.EOL +
374 " " + PMD.EOL +
375 " long l1 = 0b00011110L;" + PMD.EOL +
376 " long l2 = 0B00011110l;" + PMD.EOL +
377 " long l3 = 0xAL;" + PMD.EOL +
378 " long l4 = 0x1___A_FL;" + PMD.EOL +
379 " long l5 = 0b1L;" + PMD.EOL +
380 " long l6 = 0b1___1_0L;" + PMD.EOL +
381 " long l7 = 0l;" + PMD.EOL +
382 " long l8 = 02L;" + PMD.EOL +
383 " long l9 = 0_123l;" + PMD.EOL +
384 " long l10 = 1l;" + PMD.EOL +
385 " long l11 = 1___3l;" + PMD.EOL +
386 " long l12 = 1_43_43598_7L;" + PMD.EOL +
387 " long l13 = 1_43_43598_7;" + PMD.EOL +
388 " " + PMD.EOL +
389 " float f1 = .1f;" + PMD.EOL +
390 " float f2 = 1.f;" + PMD.EOL +
391 " float f3 = 0f;" + PMD.EOL +
392 " float f4 = 1e0F;" + PMD.EOL +
393 " float f5 = 1e0f;" + PMD.EOL +
394 " float f6 = 12.345F;" + PMD.EOL +
395 " float f7 = .5____2_1f;" + PMD.EOL +
396 " float f8 = 1__42__3.f;" + PMD.EOL +
397 " float f9 = 0__2_4__324f;" + PMD.EOL +
398 " float f10 = 1_34e0F;" + PMD.EOL +
399 " float f11 = 1__1_2e0f;" + PMD.EOL +
400 " float f12 = 2_1___2.3__4_5F;" + PMD.EOL +
401 " float f13 = 1_34e0__4__3f;" + PMD.EOL +
402 " float f14 = 1__1_2e00__000_4f;" + PMD.EOL +
403 " float f15 = 2_1___2.3__4_5e00______0_5F;" + PMD.EOL +
404 " " + PMD.EOL +
405 " double d1 = .1d;" + PMD.EOL +
406 " double d2 = 1.D;" + PMD.EOL +
407 " double d3 = 0d;" + PMD.EOL +
408 " double d4 = 1e0D;" + PMD.EOL +
409 " double d5 = 1e0d;" + PMD.EOL +
410 " double d6 = 12.345D;" + PMD.EOL +
411 " double d7 = .5____2_1d;" + PMD.EOL +
412 " double d8 = 1__42__3.D;" + PMD.EOL +
413 " double d9 = 0__2_4__324d;" + PMD.EOL +
414 " double d10 = 1_34e0d;" + PMD.EOL +
415 " double d11 = 1__1_2e0d;" + PMD.EOL +
416 " double d12 = 2_1___2.3__4_5D;" + PMD.EOL +
417 " double d13 = 1_34e0__4__3d;" + PMD.EOL +
418 " double d14 = 1__1_2e00__000_4d;" + PMD.EOL +
419 " double d15 = 2_1___2.3__4_5e00______0_5D;" + PMD.EOL +
420 " double d16 = 0.12___34;" + PMD.EOL +
421 " " + PMD.EOL +
422 " float hf1 = 0x.1___AFp1;" + PMD.EOL +
423 " float hf2 = 0x.1___AFp0__0__0f;" + PMD.EOL +
424 " float hf3 = 0x2__3_34.4___AFP00_00f;" + PMD.EOL +
425 " " + PMD.EOL +
426 " double hd1 = 0x.1___AFp1;" + PMD.EOL +
427 " double hd2 = 0x.1___AFp0__0__0d;" + PMD.EOL +
428 " double hd3 = 0x2__3_34.4___AFP00_00d;" + PMD.EOL +
429 " " + PMD.EOL +
430 " int doc1 = 1234_5678;" + PMD.EOL +
431 " long doc2 = 1_2_3_4__5_6_7_8L;" + PMD.EOL +
432 " int doc3 = 0b0001_0010_0100_1000;" + PMD.EOL +
433 " double doc4 = 3.141_592_653_589_793d;" + PMD.EOL +
434 " double doc5 = 0x1.ffff_ffff_ffff_fP1_023;" + PMD.EOL +
435 "}" + PMD.EOL
436 ;
437
438 private static final String JDK17_STRING_IN_SWITCH =
439 "public class Test {" + PMD.EOL +
440 " public static void main(String[] args) {" + PMD.EOL +
441 " String mystr = \"value\" + \"2\";" + PMD.EOL +
442 " switch (mystr) {" + PMD.EOL +
443 " case \"value1\":" + PMD.EOL +
444 " break;" + PMD.EOL +
445 " case \"value2\":" + PMD.EOL +
446 " break;" + PMD.EOL +
447 " default:" + PMD.EOL +
448 " break;" + PMD.EOL +
449 " }" + PMD.EOL +
450 " }" + PMD.EOL +
451 "}" + PMD.EOL
452 ;
453
454 private static final String JDK17_GENERIC_DIAMOND =
455 "public class InputJava7Diamond {" + PMD.EOL +
456 " HashMap<String> map = new HashMap<>();" + PMD.EOL +
457 "}";
458
459 private static final String JDK17_TRY_WITH_RESOURCES =
460 "public class InputJava7TryWithResources {" + PMD.EOL +
461 " public static void main() {" + PMD.EOL +
462 " try (MyResource resource = new MyResource()) { }" + PMD.EOL +
463 " }" + PMD.EOL +
464 "}";
465
466 private static final String JDK17_TRY_WITH_RESOURCES_SEMI =
467 "public class InputJava7TryWithResources {" + PMD.EOL +
468 " public static void main() {" + PMD.EOL +
469 " try (MyResource resource = new MyResource();) { }" + PMD.EOL +
470 " }" + PMD.EOL +
471 "}";
472
473 private static final String JDK17_TRY_WITH_RESOURCES_MULTI =
474 "public class InputJava7TryWithResources {" + PMD.EOL +
475 " public static void main() {" + PMD.EOL +
476 " try (MyResource resource = new MyResource(); MyResource2 resource2 = new MyResource2()) { }" + PMD.EOL +
477 " }" + PMD.EOL +
478 "}";
479
480 private static final String JDK17_TRY_WITH_RESOURCES_WITH_ANNOTATIONS =
481 "public class InputJava7TryWithResources {" + PMD.EOL +
482 " public static void main() {" + PMD.EOL +
483 " try (@SuppressWarnings(\"all\") final MyResource resource = new MyResource()) { }" + PMD.EOL +
484 " }" + PMD.EOL +
485 "}";
486
487 private static final String JDK17_MULTICATCH =
488 "public class InputJava7Multicatch {" + PMD.EOL +
489 " public static void main() {" + PMD.EOL +
490 " try { }" + PMD.EOL +
491 " catch (FileNotFoundException | CustomException e) { }" + PMD.EOL +
492 " }" + PMD.EOL +
493 "}";
494
495 private static final String JDK17_MULTICATCH_WITH_ANNOTATIONS =
496 "public class InputJava7Multicatch {" + PMD.EOL +
497 " public static void main() {" + PMD.EOL +
498 " try { }" + PMD.EOL +
499 " catch (final @SuppressWarnings(\"all\") FileNotFoundException | CustomException e) { }" + PMD.EOL +
500 " }" + PMD.EOL +
501 "}";
502 public static junit.framework.Test suite() {
503 return new junit.framework.JUnit4TestAdapter(JDKVersionTest.class);
504 }
505 }