8007427: Annotation element as '_' gives compiler error instead of a warning
authormcimadamore
Fri, 15 Feb 2013 16:31:19 +0000
changeset 16296 22c10640a521
parent 16295 8b76e466f3cb
child 16297 64d698b98faa
8007427: Annotation element as '_' gives compiler error instead of a warning 8007401: Write test to check for generation of warnings when '_' is used as an identifier Summary: Extended identifier production not used in annotation values Reviewed-by: jjg Contributed-by: sonali.goel@oracle.com
langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java
langtools/test/tools/javac/lambda/IdentifierTest.java
langtools/test/tools/javac/lambda/IdentifierTest.out
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Feb 15 16:30:31 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Feb 15 16:31:19 2013 +0000
@@ -2867,7 +2867,7 @@
      *                          | Identifier "=" AnnotationValue
      */
     JCExpression annotationFieldValue() {
-        if (token.kind == IDENTIFIER) {
+        if (LAX_IDENTIFIER.accepts(token.kind)) {
             mode = EXPR;
             JCExpression t1 = term1();
             if (t1.hasTag(IDENT) && token.kind == EQ) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/IdentifierTest.java	Fri Feb 15 16:31:19 2013 +0000
@@ -0,0 +1,182 @@
+/*
+ * @test   /nodynamiccopyright/
+ * @bug    8007401 8007427
+ * @author sogoel
+ * @summary Test generation of warnings when '_' is used an identifier
+ * @compile/fail/ref=IdentifierTest.out -Werror -XDrawDiagnostics IdentifierTest.java
+ */
+
+import java.util.List;
+
+/*
+ * This test checks for the generation of warnings when '_' is used as an
+ * identifier in following cases:
+ * package name, class name, class member names including constructor
+ * cass members access using class object or this
+ * loops: for, enhanced-for, while, do-while
+ * arrays,
+ * switch,
+ * annotations, element=value pair
+ * try-catch,
+ * enum
+ * break + identifier
+ * continue + identifier
+ * type-bounds
+ * Above cases for identifier occurrences have been identified from JLS v3.
+ *
+ */
+
+// Test class
+public class IdentifierTest {
+    class _UnderscorePrefix {}
+    class Underscore_Infix {}
+    class UnderscorePostfix_ {}
+    class __ {}
+
+    static final int _prefix = 10;
+    List<String> postfix_;
+
+    // Test: class with name as '_'
+    class _ {
+        String in_fix;
+        //Test: Constructor, "_", local variable, value
+        public _() {
+            String _ = "_";
+            in_fix = _;
+        }
+
+        public void testClassMembersAccess(String[] _args) {
+            // Instance creation
+            _ _ = new _();
+            //Method invocation
+            _.testTryCatch();
+            //Field access
+            _.in_fix = "__";
+        }
+
+        // Test: try-catch
+        public void testTryCatch() {
+            try {
+                int _ = 30/0;
+            } catch (ArithmeticException _) {
+                System.out.println("Got Arithmentic exception " + _);
+            }
+        }
+    }
+
+    // Test: class member access using class object '_', use of this.
+    class TestMisc {
+        int _;
+        void _ () {
+            this._ = 5;
+        }
+
+        public void testClassMemberAccess(String[] args) {
+            // Instance creation
+            TestMisc _ = new TestMisc();
+            //Field access
+            _._ = 10;
+           //Method access
+            _._();
+        }
+    }
+
+    //Test: Type Bounds
+    class TestTypeBounds {
+        //Type bounds
+        <_ extends Object> void test(_ t) {}
+    }
+
+    // Test: enum and switch case
+    static class TestEnum {
+        // Enum
+        enum _ {
+            _MONDAY, _TUESDAY, _WEDNESDAY, _THURSDAY, _FRIDAY,
+            _SATURDAY, _SUNDAY;
+        }
+
+        void foo() {
+            // switch-case
+            for(_ _day : _.values()) {
+                switch(_day) {
+                case _SATURDAY:
+                case _SUNDAY:
+                    System.out.println("Weekend is here!");
+                    break;
+                default:
+                    System.out.println("Weekday is here!");
+                    break;
+                }
+            }
+        }
+    }
+
+    // Test: Annotation
+    static class TestAnno {
+        // Annotation with name as _
+        @interface _ {
+            String _name();
+            int _id();
+        }
+        // Element-Value pair
+        @_(_name ="m",_id=1)
+        public void m(int arg) {}
+
+        //Annotation with _ as one of the elements
+        @interface MyAnno {
+            int _();
+        }
+        // Element Value pair
+        @MyAnno(_='1')
+        public void m2() {}
+    }
+
+    // Test: for loop, while loop, do-while loop, increment/decrement op, condition, print
+    public void testLoop() {
+        // for loop
+        for(int _ = 0; _ < 5; ++_) {
+            System.out.println("_=" + _ + " ");
+        }
+
+        // while loop
+        int _ = 0;
+        while(_ <= 5) {
+            _++;
+        }
+
+        //do-while loop
+        do {
+            --_;
+        } while(_ > 0);
+    }
+
+    // Test: Array and enhanced for loop
+    public void testArraysEnhancedForLoop() {
+        // Arrays
+        String _[] = {"A","B","C","D"};
+
+        for(String _s : _ ) {
+            System.out.println("_s="+_s);
+        }
+    }
+
+    // Test: Labels in break, continue
+    public void testLabels() {
+        // break/continue with labels
+        int j = 0;
+    _:
+        for (int i = 0; i <= 5; i++) {
+            while( j > 4 ) {
+                j++;
+                continue _;
+            }
+            break _;
+        }
+    }
+}
+
+//interface
+interface _ {
+    void mI();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/IdentifierTest.out	Fri Feb 15 16:31:19 2013 +0000
@@ -0,0 +1,47 @@
+IdentifierTest.java:40:11: compiler.warn.underscore.as.identifier
+IdentifierTest.java:43:16: compiler.warn.underscore.as.identifier
+IdentifierTest.java:44:20: compiler.warn.underscore.as.identifier
+IdentifierTest.java:45:22: compiler.warn.underscore.as.identifier
+IdentifierTest.java:50:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:50:15: compiler.warn.underscore.as.identifier
+IdentifierTest.java:50:23: compiler.warn.underscore.as.identifier
+IdentifierTest.java:52:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:54:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:60:21: compiler.warn.underscore.as.identifier
+IdentifierTest.java:61:42: compiler.warn.underscore.as.identifier
+IdentifierTest.java:62:67: compiler.warn.underscore.as.identifier
+IdentifierTest.java:69:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:70:14: compiler.warn.underscore.as.identifier
+IdentifierTest.java:71:18: compiler.warn.underscore.as.identifier
+IdentifierTest.java:76:22: compiler.warn.underscore.as.identifier
+IdentifierTest.java:78:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:78:15: compiler.warn.underscore.as.identifier
+IdentifierTest.java:80:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:80:15: compiler.warn.underscore.as.identifier
+IdentifierTest.java:87:10: compiler.warn.underscore.as.identifier
+IdentifierTest.java:87:38: compiler.warn.underscore.as.identifier
+IdentifierTest.java:93:14: compiler.warn.underscore.as.identifier
+IdentifierTest.java:100:17: compiler.warn.underscore.as.identifier
+IdentifierTest.java:100:26: compiler.warn.underscore.as.identifier
+IdentifierTest.java:117:20: compiler.warn.underscore.as.identifier
+IdentifierTest.java:122:10: compiler.warn.underscore.as.identifier
+IdentifierTest.java:127:17: compiler.warn.underscore.as.identifier
+IdentifierTest.java:130:17: compiler.warn.underscore.as.identifier
+IdentifierTest.java:137:17: compiler.warn.underscore.as.identifier
+IdentifierTest.java:137:24: compiler.warn.underscore.as.identifier
+IdentifierTest.java:137:33: compiler.warn.underscore.as.identifier
+IdentifierTest.java:138:39: compiler.warn.underscore.as.identifier
+IdentifierTest.java:142:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:143:15: compiler.warn.underscore.as.identifier
+IdentifierTest.java:144:13: compiler.warn.underscore.as.identifier
+IdentifierTest.java:149:15: compiler.warn.underscore.as.identifier
+IdentifierTest.java:150:17: compiler.warn.underscore.as.identifier
+IdentifierTest.java:156:16: compiler.warn.underscore.as.identifier
+IdentifierTest.java:158:25: compiler.warn.underscore.as.identifier
+IdentifierTest.java:167:5: compiler.warn.underscore.as.identifier
+IdentifierTest.java:171:26: compiler.warn.underscore.as.identifier
+IdentifierTest.java:173:19: compiler.warn.underscore.as.identifier
+IdentifierTest.java:179:11: compiler.warn.underscore.as.identifier
+- compiler.err.warnings.and.werror
+1 error
+44 warnings