1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/IdentifierTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,182 @@ 1.4 +/* 1.5 + * @test /nodynamiccopyright/ 1.6 + * @bug 8007401 8007427 1.7 + * @author sogoel 1.8 + * @summary Test generation of warnings when '_' is used an identifier 1.9 + * @compile/fail/ref=IdentifierTest.out -Werror -XDrawDiagnostics IdentifierTest.java 1.10 + */ 1.11 + 1.12 +import java.util.List; 1.13 + 1.14 +/* 1.15 + * This test checks for the generation of warnings when '_' is used as an 1.16 + * identifier in following cases: 1.17 + * package name, class name, class member names including constructor 1.18 + * cass members access using class object or this 1.19 + * loops: for, enhanced-for, while, do-while 1.20 + * arrays, 1.21 + * switch, 1.22 + * annotations, element=value pair 1.23 + * try-catch, 1.24 + * enum 1.25 + * break + identifier 1.26 + * continue + identifier 1.27 + * type-bounds 1.28 + * Above cases for identifier occurrences have been identified from JLS v3. 1.29 + * 1.30 + */ 1.31 + 1.32 +// Test class 1.33 +public class IdentifierTest { 1.34 + class _UnderscorePrefix {} 1.35 + class Underscore_Infix {} 1.36 + class UnderscorePostfix_ {} 1.37 + class __ {} 1.38 + 1.39 + static final int _prefix = 10; 1.40 + List<String> postfix_; 1.41 + 1.42 + // Test: class with name as '_' 1.43 + class _ { 1.44 + String in_fix; 1.45 + //Test: Constructor, "_", local variable, value 1.46 + public _() { 1.47 + String _ = "_"; 1.48 + in_fix = _; 1.49 + } 1.50 + 1.51 + public void testClassMembersAccess(String[] _args) { 1.52 + // Instance creation 1.53 + _ _ = new _(); 1.54 + //Method invocation 1.55 + _.testTryCatch(); 1.56 + //Field access 1.57 + _.in_fix = "__"; 1.58 + } 1.59 + 1.60 + // Test: try-catch 1.61 + public void testTryCatch() { 1.62 + try { 1.63 + int _ = 30/0; 1.64 + } catch (ArithmeticException _) { 1.65 + System.out.println("Got Arithmentic exception " + _); 1.66 + } 1.67 + } 1.68 + } 1.69 + 1.70 + // Test: class member access using class object '_', use of this. 1.71 + class TestMisc { 1.72 + int _; 1.73 + void _ () { 1.74 + this._ = 5; 1.75 + } 1.76 + 1.77 + public void testClassMemberAccess(String[] args) { 1.78 + // Instance creation 1.79 + TestMisc _ = new TestMisc(); 1.80 + //Field access 1.81 + _._ = 10; 1.82 + //Method access 1.83 + _._(); 1.84 + } 1.85 + } 1.86 + 1.87 + //Test: Type Bounds 1.88 + class TestTypeBounds { 1.89 + //Type bounds 1.90 + <_ extends Object> void test(_ t) {} 1.91 + } 1.92 + 1.93 + // Test: enum and switch case 1.94 + static class TestEnum { 1.95 + // Enum 1.96 + enum _ { 1.97 + _MONDAY, _TUESDAY, _WEDNESDAY, _THURSDAY, _FRIDAY, 1.98 + _SATURDAY, _SUNDAY; 1.99 + } 1.100 + 1.101 + void foo() { 1.102 + // switch-case 1.103 + for(_ _day : _.values()) { 1.104 + switch(_day) { 1.105 + case _SATURDAY: 1.106 + case _SUNDAY: 1.107 + System.out.println("Weekend is here!"); 1.108 + break; 1.109 + default: 1.110 + System.out.println("Weekday is here!"); 1.111 + break; 1.112 + } 1.113 + } 1.114 + } 1.115 + } 1.116 + 1.117 + // Test: Annotation 1.118 + static class TestAnno { 1.119 + // Annotation with name as _ 1.120 + @interface _ { 1.121 + String _name(); 1.122 + int _id(); 1.123 + } 1.124 + // Element-Value pair 1.125 + @_(_name ="m",_id=1) 1.126 + public void m(int arg) {} 1.127 + 1.128 + //Annotation with _ as one of the elements 1.129 + @interface MyAnno { 1.130 + int _(); 1.131 + } 1.132 + // Element Value pair 1.133 + @MyAnno(_='1') 1.134 + public void m2() {} 1.135 + } 1.136 + 1.137 + // Test: for loop, while loop, do-while loop, increment/decrement op, condition, print 1.138 + public void testLoop() { 1.139 + // for loop 1.140 + for(int _ = 0; _ < 5; ++_) { 1.141 + System.out.println("_=" + _ + " "); 1.142 + } 1.143 + 1.144 + // while loop 1.145 + int _ = 0; 1.146 + while(_ <= 5) { 1.147 + _++; 1.148 + } 1.149 + 1.150 + //do-while loop 1.151 + do { 1.152 + --_; 1.153 + } while(_ > 0); 1.154 + } 1.155 + 1.156 + // Test: Array and enhanced for loop 1.157 + public void testArraysEnhancedForLoop() { 1.158 + // Arrays 1.159 + String _[] = {"A","B","C","D"}; 1.160 + 1.161 + for(String _s : _ ) { 1.162 + System.out.println("_s="+_s); 1.163 + } 1.164 + } 1.165 + 1.166 + // Test: Labels in break, continue 1.167 + public void testLabels() { 1.168 + // break/continue with labels 1.169 + int j = 0; 1.170 + _: 1.171 + for (int i = 0; i <= 5; i++) { 1.172 + while( j > 4 ) { 1.173 + j++; 1.174 + continue _; 1.175 + } 1.176 + break _; 1.177 + } 1.178 + } 1.179 +} 1.180 + 1.181 +//interface 1.182 +interface _ { 1.183 + void mI(); 1.184 +} 1.185 +