changeset 57054:c8e9983b2732 records-and-sealed

testing records in inner classes
author vromero
date Tue, 27 Aug 2019 18:10:25 -0400
parents 93af68a8ec11
children e05b8504a9a2 3df7cc39a142
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/records/RecordMemberTests.java test/langtools/tools/javac/records/RecordsInInnerClassesTest.java
diffstat 3 files changed, 99 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Tue Aug 27 17:18:26 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Tue Aug 27 18:10:25 2019 -0400
@@ -95,6 +95,7 @@
     private final Profile profile;
     private final boolean warnOnAnyAccessToMembers;
     private final boolean debug;
+    private final boolean allowStaticMembersInInners;
 
     // The set of lint options currently in effect. It is initialized
     // from the context, and then is set/reset as needed by Attr as it
@@ -135,6 +136,7 @@
         source = Source.instance(context);
         target = Target.instance(context);
         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");
+        allowStaticMembersInInners = options.isSet("allowStaticMembersInInners");
 
         Target target = Target.instance(context);
         syntheticNameChar = target.syntheticNameChar();
@@ -1190,7 +1192,7 @@
                 if (sym.owner.owner.kind == PCK ||
                     (sym.owner.flags_field & STATIC) != 0)
                     mask |= STATIC;
-                else if ((flags & ENUM) != 0) {
+                else if ((flags & ENUM) != 0 && !allowStaticMembersInInners) {
                     log.error(pos, Errors.EnumsMustBeStatic);
                 }
                 // Nested interfaces and enums are always STATIC (Spec ???)
--- a/test/langtools/tools/javac/records/RecordMemberTests.java	Tue Aug 27 17:18:26 2019 -0400
+++ b/test/langtools/tools/javac/records/RecordMemberTests.java	Tue Aug 27 18:10:25 2019 -0400
@@ -23,6 +23,13 @@
  * questions.
  */
 
+/**
+ * RecordMemberTests
+ *
+ * @test
+ * @run testng RecordMemberTests
+ */
+
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -34,13 +41,6 @@
 import org.testng.annotations.*;
 import static org.testng.Assert.*;
 
-/**
- * RecordMemberTests
- *
- * @test
- * @compile -XDallowStaticMembersInInners RecordMemberTests.java
- * @run testng RecordMemberTests
- */
 @Test
 public class RecordMemberTests {
     record R1(int i, int j) {}
@@ -140,14 +140,6 @@
         assertEquals(o.sf(), "instance");
     }
 
-    class InstanceNestedRecordHelper {
-        record R(int x) { }
-    }
-
-    public void testNestedRecordsStatic() {
-        assertTrue((InstanceNestedRecordHelper.R.class.getModifiers() & Modifier.STATIC) != 0);
-    }
-
     class LocalRecordHelper {
         Class<?> m(int x) {
             record R (int x) { }
@@ -170,11 +162,6 @@
             record R2(int x) { }
         }
 
-        Runnable r = new Runnable() {
-            record R3(int x) { }
-            public void run() { }
-        };
-
         Class<?> m() {
             record R4(int x) { }
             return R4.class;
@@ -202,7 +189,7 @@
         }
     }
 
-    public void testNestedRecordsStatic2() {
+    public void testNestedRecordsStatic() {
         NestedRecordHelper n = new NestedRecordHelper();
         for (Class<?> c : List.of(NestedRecordHelper.R1.class,
                                   NestedRecordHelper.Nested.R2.class,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/records/RecordsInInnerClassesTest.java	Tue Aug 27 18:10:25 2019 -0400
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * StaticMembersInInnerClassesTest
+ *
+ * @test
+ * @compile -XDallowStaticMembersInInners StaticMembersInInnerClassesTest.java
+ * @run testng StaticMembersInInnerClassesTest
+ */
+
+import org.testng.annotations.*;
+import java.lang.reflect.Modifier;
+import static org.testng.Assert.*;
+
+@Test
+public class StaticMembersInInnerClassesTest {
+    class InstanceNestedHelper {
+        interface I {
+            String foo();
+        }
+
+        record R(int x) implements I {
+            public String foo() { return "foo"; }
+        }
+
+        private R r;
+
+        InstanceNestedHelper(int x) {
+            this.r = new R(x);
+            this.e = E.B;
+        }
+
+        int x() {
+            return r.x();
+        }
+
+        static int ident(int i) {
+            return i;
+        }
+
+        String foo() {
+            return r.foo();
+        }
+
+        enum E { A, B }
+
+        E e;
+    }
+
+    public void testNestedRecordsStatic() {
+        assertTrue((InstanceNestedHelper.R.class.getModifiers() & Modifier.STATIC) != 0);
+        InstanceNestedHelper inner = new InstanceNestedHelper(1);
+        assertTrue(inner.x() == 1);
+        assertTrue(inner.ident(1) == 1);
+        assertTrue(InstanceNestedHelper.ident(1) == 1);
+        assertTrue(inner.foo().equals("foo"));
+        assertTrue(InstanceNestedHelper.E.A.toString().equals("A"));
+        assertTrue(inner.e.toString().equals("B"));
+    }
+
+    Runnable r = new Runnable() {
+        record R3(int x) {}
+        public void run() {}
+    };
+}