OpenJDK / amber / amber
changeset 57052:93af68a8ec11 records-and-sealed
allow static members in inner classes
author | vromero |
---|---|
date | Tue, 27 Aug 2019 17:18:26 -0400 |
parents | 7cf9d1e04cb1 |
children | c84f723610a4 c8e9983b2732 |
files | src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java test/langtools/tools/javac/records/RecordMemberTests.java |
diffstat | 2 files changed, 32 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 27 16:24:29 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 27 17:18:26 2019 -0400 @@ -166,6 +166,7 @@ allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source); sourceName = source.name; useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning"); + allowStaticMembersInInners = options.isSet("allowStaticMembersInInners"); statInfo = new ResultInfo(KindSelector.NIL, Type.noType); varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType); @@ -207,6 +208,11 @@ */ String sourceName; + /** Switch: allow static members in inner classes + * + */ + boolean allowStaticMembersInInners; + /** Check kind and type of given tree against protokind and prototype. * If check succeeds, store type in tree and return it. * If check fails, store errType in tree and return it. @@ -4843,17 +4849,19 @@ for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { // Attribute declaration attribStat(l.head, env); - // Check that declarations in inner classes are not static (JLS 8.1.2) - // Make an exception for static constants. - if (c.owner.kind != PCK && - ((c.flags() & STATIC) == 0 || c.name == names.empty) && - (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) { - Symbol sym = null; - if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym; - if (sym == null || - sym.kind != VAR || - ((VarSymbol) sym).getConstValue() == null) - log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c)); + if (!allowStaticMembersInInners) { + // Check that declarations in inner classes are not static (JLS 8.1.2) + // Make an exception for static constants. + if (c.owner.kind != PCK && + ((c.flags() & STATIC) == 0 || c.name == names.empty) && + (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) { + Symbol sym = null; + if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym; + if (sym == null || + sym.kind != VAR || + ((VarSymbol) sym).getConstValue() == null) + log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c)); + } } }
--- a/test/langtools/tools/javac/records/RecordMemberTests.java Tue Aug 27 16:24:29 2019 -0400 +++ b/test/langtools/tools/javac/records/RecordMemberTests.java Tue Aug 27 17:18:26 2019 -0400 @@ -38,6 +38,7 @@ * RecordMemberTests * * @test + * @compile -XDallowStaticMembersInInners RecordMemberTests.java * @run testng RecordMemberTests */ @Test @@ -139,14 +140,13 @@ assertEquals(o.sf(), "instance"); } - // @@@ Fails with error message about invalid static members -// class InstanceNestedRecordHelper { -// record R(int x) { } -// } -// -// public void testNestedRecordsStatic() { -// assertTrue((InstanceNestedRecordHelper.R.class.getModifiers() & Modifier.STATIC) != 0); -// } + class InstanceNestedRecordHelper { + record R(int x) { } + } + + public void testNestedRecordsStatic() { + assertTrue((InstanceNestedRecordHelper.R.class.getModifiers() & Modifier.STATIC) != 0); + } class LocalRecordHelper { Class<?> m(int x) { @@ -170,10 +170,10 @@ record R2(int x) { } } -// Runnable r = new Runnable() { -// record R3(int x) { } -// public void run() { } -// }; + Runnable r = new Runnable() { + record R3(int x) { } + public void run() { } + }; Class<?> m() { record R4(int x) { } @@ -202,7 +202,7 @@ } } - public void testNestedRecordsStatic() { + public void testNestedRecordsStatic2() { NestedRecordHelper n = new NestedRecordHelper(); for (Class<?> c : List.of(NestedRecordHelper.R1.class, NestedRecordHelper.Nested.R2.class, @@ -216,4 +216,3 @@ } } } -