IdentifierTest.java revision 2747:84a76798cff3
131567Ssef/* 2204977Simp * @test /nodynamiccopyright/ 331899Ssef * @bug 8007401 8007427 8061549 431899Ssef * @author sogoel 531899Ssef * @summary Test generation of warnings when '_' is used an identifier 631899Ssef * @compile/fail/ref=IdentifierTest8.out -source 8 -Xlint:-options -Werror -XDrawDiagnostics IdentifierTest.java 731899Ssef * @compile/fail/ref=IdentifierTest9.out -XDrawDiagnostics IdentifierTest.java 831899Ssef */ 931899Ssef 1031899Ssefimport java.util.List; 1131899Ssef 1231899Ssef/* 1331899Ssef * This test checks for the generation of warnings when '_' is used as an 1431899Ssef * identifier in following cases: 1531899Ssef * package name, class name, class member names including constructor 1631899Ssef * cass members access using class object or this 1731899Ssef * loops: for, enhanced-for, while, do-while 1831899Ssef * arrays, 1931899Ssef * switch, 2031899Ssef * annotations, element=value pair 2131899Ssef * try-catch, 2231899Ssef * enum 2331899Ssef * break + identifier 2431899Ssef * continue + identifier 2531899Ssef * type-bounds 2631899Ssef * Above cases for identifier occurrences have been identified from JLS v3. 2731899Ssef * 2831899Ssef */ 2931899Ssef 3031899Ssef// Test class 3131899Ssefpublic class IdentifierTest { 32290052Sjhb class _UnderscorePrefix {} 33290052Sjhb class Underscore_Infix {} 3432275Scharnier class UnderscorePostfix_ {} 35290052Sjhb class __ {} 3632275Scharnier 37168569Sdelphij static final int _prefix = 10; 3885301Sdes List<String> postfix_; 3985301Sdes 4085301Sdes // Test: class with name as '_' 4185301Sdes class _ { 4231567Ssef String in_fix; 4331567Ssef //Test: Constructor, "_", local variable, value 44101282Smdodd public _() { 4531567Ssef String _ = "_"; 4631567Ssef in_fix = _; 4731567Ssef } 48290052Sjhb 49290052Sjhb public void testClassMembersAccess(String[] _args) { 50240562Szont // Instance creation 51240005Szont _ _ = new _(); 52290052Sjhb //Method invocation 53240562Szont _.testTryCatch(); 5431567Ssef //Field access 55240562Szont _.in_fix = "__"; 56290052Sjhb } 57240562Szont 58240005Szont // Test: try-catch 59290052Sjhb public void testTryCatch() { 60240005Szont try { 6131567Ssef int _ = 30/0; 62240005Szont } catch (ArithmeticException _) { 63240005Szont System.out.println("Got Arithmentic exception " + _); 64240005Szont } 65240005Szont } 66240005Szont } 67240005Szont 68240005Szont // Test: class member access using class object '_', use of this. 69290052Sjhb class TestMisc { 70290052Sjhb int _; 71290052Sjhb void _ () { 72290052Sjhb this._ = 5; 73290052Sjhb } 74290052Sjhb 75290052Sjhb public void testClassMemberAccess(String[] args) { 76290052Sjhb // Instance creation 77290052Sjhb TestMisc _ = new TestMisc(); 78290052Sjhb //Field access 79290052Sjhb _._ = 10; 80290052Sjhb //Method access 81290052Sjhb _._(); 82240005Szont } 8331567Ssef } 84290052Sjhb 8531567Ssef //Test: Type Bounds 8631567Ssef class TestTypeBounds { 8731567Ssef //Type bounds 8831567Ssef <_ extends Object> void test(_ t) {} 8931567Ssef } 90228396Sed 91240005Szont // Test: enum and switch case 92240005Szont static class TestEnum { 93240005Szont // Enum 94240005Szont enum _ { 95240005Szont _MONDAY, _TUESDAY, _WEDNESDAY, _THURSDAY, _FRIDAY, 9631567Ssef _SATURDAY, _SUNDAY; 9731567Ssef } 9831567Ssef 99240005Szont void foo() { 10031567Ssef // switch-case 10131567Ssef for(_ _day : _.values()) { 102290052Sjhb switch(_day) { 103290052Sjhb case _SATURDAY: 104122348Smarcel case _SUNDAY: 105240005Szont System.out.println("Weekend is here!"); 106240562Szont break; 107290052Sjhb default: 10831567Ssef System.out.println("Weekday is here!"); 109240562Szont break; 110240562Szont } 111240005Szont } 112240005Szont } 113240005Szont } 11431567Ssef 115290052Sjhb // Test: Annotation 116290052Sjhb static class TestAnno { 117290052Sjhb // Annotation with name as _ 118101374Smdodd @interface _ { 119290052Sjhb String _name(); 120290052Sjhb int _id(); 121290052Sjhb } 122290052Sjhb // Element-Value pair 123290052Sjhb @_(_name ="m",_id=1) 124240005Szont public void m(int arg) {} 125240005Szont 126171055Sdelphij //Annotation with _ as one of the elements 127290052Sjhb @interface MyAnno { 128240005Szont int _(); 129290052Sjhb } 130290052Sjhb // Element Value pair 131171055Sdelphij @MyAnno(_='1') 132290052Sjhb public void m2() {} 133296010Sjhb } 134290052Sjhb 135290052Sjhb // Test: for loop, while loop, do-while loop, increment/decrement op, condition, print 136290052Sjhb public void testLoop() { 137290052Sjhb // for loop 138290052Sjhb for(int _ = 0; _ < 5; ++_) { 139101282Smdodd System.out.println("_=" + _ + " "); 140290052Sjhb } 141 142 // while loop 143 int _ = 0; 144 while(_ <= 5) { 145 _++; 146 } 147 148 //do-while loop 149 do { 150 --_; 151 } while(_ > 0); 152 } 153 154 // Test: Array and enhanced for loop 155 public void testArraysEnhancedForLoop() { 156 // Arrays 157 String _[] = {"A","B","C","D"}; 158 159 for(String _s : _ ) { 160 System.out.println("_s="+_s); 161 } 162 } 163 164 // Test: Labels in break, continue 165 public void testLabels() { 166 // break/continue with labels 167 int j = 0; 168 _: 169 for (int i = 0; i <= 5; i++) { 170 while( j > 4 ) { 171 j++; 172 continue _; 173 } 174 break _; 175 } 176 } 177} 178 179//interface 180interface _ { 181 void mI(); 182} 183 184