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