aoqi@0: /* aoqi@0: * @test /nodynamiccopyright/ aoqi@0: * @bug 8007401 8007427 aoqi@0: * @author sogoel aoqi@0: * @summary Test generation of warnings when '_' is used an identifier aoqi@0: * @compile/fail/ref=IdentifierTest.out -Werror -XDrawDiagnostics IdentifierTest.java aoqi@0: */ aoqi@0: aoqi@0: import java.util.List; aoqi@0: aoqi@0: /* aoqi@0: * This test checks for the generation of warnings when '_' is used as an aoqi@0: * identifier in following cases: aoqi@0: * package name, class name, class member names including constructor aoqi@0: * cass members access using class object or this aoqi@0: * loops: for, enhanced-for, while, do-while aoqi@0: * arrays, aoqi@0: * switch, aoqi@0: * annotations, element=value pair aoqi@0: * try-catch, aoqi@0: * enum aoqi@0: * break + identifier aoqi@0: * continue + identifier aoqi@0: * type-bounds aoqi@0: * Above cases for identifier occurrences have been identified from JLS v3. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: // Test class aoqi@0: public class IdentifierTest { aoqi@0: class _UnderscorePrefix {} aoqi@0: class Underscore_Infix {} aoqi@0: class UnderscorePostfix_ {} aoqi@0: class __ {} aoqi@0: aoqi@0: static final int _prefix = 10; aoqi@0: List postfix_; aoqi@0: aoqi@0: // Test: class with name as '_' aoqi@0: class _ { aoqi@0: String in_fix; aoqi@0: //Test: Constructor, "_", local variable, value aoqi@0: public _() { aoqi@0: String _ = "_"; aoqi@0: in_fix = _; aoqi@0: } aoqi@0: aoqi@0: public void testClassMembersAccess(String[] _args) { aoqi@0: // Instance creation aoqi@0: _ _ = new _(); aoqi@0: //Method invocation aoqi@0: _.testTryCatch(); aoqi@0: //Field access aoqi@0: _.in_fix = "__"; aoqi@0: } aoqi@0: aoqi@0: // Test: try-catch aoqi@0: public void testTryCatch() { aoqi@0: try { aoqi@0: int _ = 30/0; aoqi@0: } catch (ArithmeticException _) { aoqi@0: System.out.println("Got Arithmentic exception " + _); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Test: class member access using class object '_', use of this. aoqi@0: class TestMisc { aoqi@0: int _; aoqi@0: void _ () { aoqi@0: this._ = 5; aoqi@0: } aoqi@0: aoqi@0: public void testClassMemberAccess(String[] args) { aoqi@0: // Instance creation aoqi@0: TestMisc _ = new TestMisc(); aoqi@0: //Field access aoqi@0: _._ = 10; aoqi@0: //Method access aoqi@0: _._(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //Test: Type Bounds aoqi@0: class TestTypeBounds { aoqi@0: //Type bounds aoqi@0: <_ extends Object> void test(_ t) {} aoqi@0: } aoqi@0: aoqi@0: // Test: enum and switch case aoqi@0: static class TestEnum { aoqi@0: // Enum aoqi@0: enum _ { aoqi@0: _MONDAY, _TUESDAY, _WEDNESDAY, _THURSDAY, _FRIDAY, aoqi@0: _SATURDAY, _SUNDAY; aoqi@0: } aoqi@0: aoqi@0: void foo() { aoqi@0: // switch-case aoqi@0: for(_ _day : _.values()) { aoqi@0: switch(_day) { aoqi@0: case _SATURDAY: aoqi@0: case _SUNDAY: aoqi@0: System.out.println("Weekend is here!"); aoqi@0: break; aoqi@0: default: aoqi@0: System.out.println("Weekday is here!"); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Test: Annotation aoqi@0: static class TestAnno { aoqi@0: // Annotation with name as _ aoqi@0: @interface _ { aoqi@0: String _name(); aoqi@0: int _id(); aoqi@0: } aoqi@0: // Element-Value pair aoqi@0: @_(_name ="m",_id=1) aoqi@0: public void m(int arg) {} aoqi@0: aoqi@0: //Annotation with _ as one of the elements aoqi@0: @interface MyAnno { aoqi@0: int _(); aoqi@0: } aoqi@0: // Element Value pair aoqi@0: @MyAnno(_='1') aoqi@0: public void m2() {} aoqi@0: } aoqi@0: aoqi@0: // Test: for loop, while loop, do-while loop, increment/decrement op, condition, print aoqi@0: public void testLoop() { aoqi@0: // for loop aoqi@0: for(int _ = 0; _ < 5; ++_) { aoqi@0: System.out.println("_=" + _ + " "); aoqi@0: } aoqi@0: aoqi@0: // while loop aoqi@0: int _ = 0; aoqi@0: while(_ <= 5) { aoqi@0: _++; aoqi@0: } aoqi@0: aoqi@0: //do-while loop aoqi@0: do { aoqi@0: --_; aoqi@0: } while(_ > 0); aoqi@0: } aoqi@0: aoqi@0: // Test: Array and enhanced for loop aoqi@0: public void testArraysEnhancedForLoop() { aoqi@0: // Arrays aoqi@0: String _[] = {"A","B","C","D"}; aoqi@0: aoqi@0: for(String _s : _ ) { aoqi@0: System.out.println("_s="+_s); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Test: Labels in break, continue aoqi@0: public void testLabels() { aoqi@0: // break/continue with labels aoqi@0: int j = 0; aoqi@0: _: aoqi@0: for (int i = 0; i <= 5; i++) { aoqi@0: while( j > 4 ) { aoqi@0: j++; aoqi@0: continue _; aoqi@0: } aoqi@0: break _; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //interface aoqi@0: interface _ { aoqi@0: void mI(); aoqi@0: } aoqi@0: