|
1 /* |
|
2 * @test /nodynamiccopyright/ |
|
3 * @bug 8007401 8007427 |
|
4 * @author sogoel |
|
5 * @summary Test generation of warnings when '_' is used an identifier |
|
6 * @compile/fail/ref=IdentifierTest.out -Werror -XDrawDiagnostics IdentifierTest.java |
|
7 */ |
|
8 |
|
9 import java.util.List; |
|
10 |
|
11 /* |
|
12 * This test checks for the generation of warnings when '_' is used as an |
|
13 * identifier in following cases: |
|
14 * package name, class name, class member names including constructor |
|
15 * cass members access using class object or this |
|
16 * loops: for, enhanced-for, while, do-while |
|
17 * arrays, |
|
18 * switch, |
|
19 * annotations, element=value pair |
|
20 * try-catch, |
|
21 * enum |
|
22 * break + identifier |
|
23 * continue + identifier |
|
24 * type-bounds |
|
25 * Above cases for identifier occurrences have been identified from JLS v3. |
|
26 * |
|
27 */ |
|
28 |
|
29 // Test class |
|
30 public class IdentifierTest { |
|
31 class _UnderscorePrefix {} |
|
32 class Underscore_Infix {} |
|
33 class UnderscorePostfix_ {} |
|
34 class __ {} |
|
35 |
|
36 static final int _prefix = 10; |
|
37 List<String> postfix_; |
|
38 |
|
39 // Test: class with name as '_' |
|
40 class _ { |
|
41 String in_fix; |
|
42 //Test: Constructor, "_", local variable, value |
|
43 public _() { |
|
44 String _ = "_"; |
|
45 in_fix = _; |
|
46 } |
|
47 |
|
48 public void testClassMembersAccess(String[] _args) { |
|
49 // Instance creation |
|
50 _ _ = new _(); |
|
51 //Method invocation |
|
52 _.testTryCatch(); |
|
53 //Field access |
|
54 _.in_fix = "__"; |
|
55 } |
|
56 |
|
57 // Test: try-catch |
|
58 public void testTryCatch() { |
|
59 try { |
|
60 int _ = 30/0; |
|
61 } catch (ArithmeticException _) { |
|
62 System.out.println("Got Arithmentic exception " + _); |
|
63 } |
|
64 } |
|
65 } |
|
66 |
|
67 // Test: class member access using class object '_', use of this. |
|
68 class TestMisc { |
|
69 int _; |
|
70 void _ () { |
|
71 this._ = 5; |
|
72 } |
|
73 |
|
74 public void testClassMemberAccess(String[] args) { |
|
75 // Instance creation |
|
76 TestMisc _ = new TestMisc(); |
|
77 //Field access |
|
78 _._ = 10; |
|
79 //Method access |
|
80 _._(); |
|
81 } |
|
82 } |
|
83 |
|
84 //Test: Type Bounds |
|
85 class TestTypeBounds { |
|
86 //Type bounds |
|
87 <_ extends Object> void test(_ t) {} |
|
88 } |
|
89 |
|
90 // Test: enum and switch case |
|
91 static class TestEnum { |
|
92 // Enum |
|
93 enum _ { |
|
94 _MONDAY, _TUESDAY, _WEDNESDAY, _THURSDAY, _FRIDAY, |
|
95 _SATURDAY, _SUNDAY; |
|
96 } |
|
97 |
|
98 void foo() { |
|
99 // switch-case |
|
100 for(_ _day : _.values()) { |
|
101 switch(_day) { |
|
102 case _SATURDAY: |
|
103 case _SUNDAY: |
|
104 System.out.println("Weekend is here!"); |
|
105 break; |
|
106 default: |
|
107 System.out.println("Weekday is here!"); |
|
108 break; |
|
109 } |
|
110 } |
|
111 } |
|
112 } |
|
113 |
|
114 // Test: Annotation |
|
115 static class TestAnno { |
|
116 // Annotation with name as _ |
|
117 @interface _ { |
|
118 String _name(); |
|
119 int _id(); |
|
120 } |
|
121 // Element-Value pair |
|
122 @_(_name ="m",_id=1) |
|
123 public void m(int arg) {} |
|
124 |
|
125 //Annotation with _ as one of the elements |
|
126 @interface MyAnno { |
|
127 int _(); |
|
128 } |
|
129 // Element Value pair |
|
130 @MyAnno(_='1') |
|
131 public void m2() {} |
|
132 } |
|
133 |
|
134 // Test: for loop, while loop, do-while loop, increment/decrement op, condition, print |
|
135 public void testLoop() { |
|
136 // for loop |
|
137 for(int _ = 0; _ < 5; ++_) { |
|
138 System.out.println("_=" + _ + " "); |
|
139 } |
|
140 |
|
141 // while loop |
|
142 int _ = 0; |
|
143 while(_ <= 5) { |
|
144 _++; |
|
145 } |
|
146 |
|
147 //do-while loop |
|
148 do { |
|
149 --_; |
|
150 } while(_ > 0); |
|
151 } |
|
152 |
|
153 // Test: Array and enhanced for loop |
|
154 public void testArraysEnhancedForLoop() { |
|
155 // Arrays |
|
156 String _[] = {"A","B","C","D"}; |
|
157 |
|
158 for(String _s : _ ) { |
|
159 System.out.println("_s="+_s); |
|
160 } |
|
161 } |
|
162 |
|
163 // Test: Labels in break, continue |
|
164 public void testLabels() { |
|
165 // break/continue with labels |
|
166 int j = 0; |
|
167 _: |
|
168 for (int i = 0; i <= 5; i++) { |
|
169 while( j > 4 ) { |
|
170 j++; |
|
171 continue _; |
|
172 } |
|
173 break _; |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 //interface |
|
179 interface _ { |
|
180 void mI(); |
|
181 } |
|
182 |