test/tools/javac/lambda/IdentifierTest.java

Tue, 14 May 2013 11:11:09 -0700

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 0
959103a6100f
permissions
-rw-r--r--

8012556: Implement lambda methods on interfaces as static
8006140: Javac NPE compiling Lambda expression on initialization expression of static field in interface
Summary: Lambdas occurring in static contexts or those not needing instance information should be generated into static methods. This has long been the case for classes. However, as a work-around to the lack of support for statics on interfaces, interface lambda methods have been generated into default methods. For lambdas in interface static contexts (fields and static methods) this causes an NPE in javac because there is no 'this'. MethodHandles now support static methods on interfaces. This changeset allows lambda methods to be generated as static interface methods. An existing bug in Hotspot (8013875) is exposed in a test when the "-esa" flag is used. This test and another test that already exposed this bug have been marked with @ignore.
Reviewed-by: mcimadamore

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

mercurial