--- /dev/null 2016-02-12 19:50:35.000000000 +0300 +++ new/test/runtime/valhalla/nestmates/InnerClassesTest.java 2016-02-12 19:50:35.000000000 +0300 @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016, 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. + * + * 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. + */ + +package runtime.valhalla.nestmates; + +import jdk.test.lib.Asserts; +import runtime.valhalla.nestmates.p2.InnerClassesHolder; + +/* + * @test InnerClassesTest + * @summary Verify an access to an enclosing class fields from all possible nested classes + * @library /testlibrary / + * @modules java.base + * @run main runtime.valhalla.nestmates.InnerClassesTest + */ +public class InnerClassesTest { + public static void main(String[] args) { + InnerClassesTest innerClassesTest = new InnerClassesTest(); + innerClassesTest.run(); + } + + public void run() { + InnerClassesHolder innerClassesHolder = new InnerClassesHolder(); + InnerClassesHolder.InnerClass innerClass = innerClassesHolder.new InnerClass(); + InnerClassesHolder.InnerCousin innerCousin = innerClassesHolder.new InnerCousin(innerClass); + + Asserts.assertEquals(innerClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PRIVATE), 1, "test inner private field"); + Asserts.assertEquals(innerClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PROTECTED), 2, "test inner protected field"); + Asserts.assertEquals(innerClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PPRIVATE), 3, "test inner pprivate field"); + Asserts.assertEquals(innerClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PUBLIC), 4, "test inner public field"); + + Asserts.assertEquals(innerCousin.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PRIVATE), 1, "test cousin private field"); + Asserts.assertEquals(innerCousin.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PROTECTED), 2, "test cousin protected field"); + Asserts.assertEquals(innerCousin.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PPRIVATE), 3, "test cousin pprivate field"); + Asserts.assertEquals(innerCousin.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PUBLIC), 4, "test cousin public field"); + + Asserts.assertEquals(innerClassesHolder.getValueFromAnonClass(InnerClassesHolder.MODIFIER_PRIVATE), 1, "test annon private"); + Asserts.assertEquals(innerClassesHolder.getValueFromAnonClass(InnerClassesHolder.MODIFIER_PROTECTED), 2, "test annon protected"); + Asserts.assertEquals(innerClassesHolder.getValueFromAnonClass(InnerClassesHolder.MODIFIER_PPRIVATE), 3, "test annon pprivate"); + Asserts.assertEquals(innerClassesHolder.getValueFromAnonClass(InnerClassesHolder.MODIFIER_PUBLIC), 4, "test annon public"); + + Asserts.assertEquals(innerClassesHolder.getValueFromLocalClass(InnerClassesHolder.MODIFIER_PRIVATE), 1, "test local private"); + Asserts.assertEquals(innerClassesHolder.getValueFromLocalClass(InnerClassesHolder.MODIFIER_PROTECTED), 2, "test local protected"); + Asserts.assertEquals(innerClassesHolder.getValueFromLocalClass(InnerClassesHolder.MODIFIER_PPRIVATE), 3, "test local pprivate"); + Asserts.assertEquals(innerClassesHolder.getValueFromLocalClass(InnerClassesHolder.MODIFIER_PUBLIC), 4, "test local public"); + + Asserts.assertEquals(InnerClassesHolder.StaticNestedClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PRIVATE), 1, "test static nested"); + Asserts.assertEquals(InnerClassesHolder.StaticNestedClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PROTECTED), 2, "test static nested"); + Asserts.assertEquals(InnerClassesHolder.StaticNestedClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PPRIVATE), 3, "test static nested"); + Asserts.assertEquals(InnerClassesHolder.StaticNestedClass.getEnclosingFieldValue(InnerClassesHolder.MODIFIER_PUBLIC), 4, "test static nested"); + } +} --- /dev/null 2016-02-12 19:50:35.000000000 +0300 +++ new/test/runtime/valhalla/nestmates/ParentChildTest.java 2016-02-12 19:50:35.000000000 +0300 @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, 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. + * + * 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. + */ + +package runtime.valhalla.nestmates; + +import jdk.test.lib.Asserts; +import runtime.valhalla.nestmates.p2.SubTop; + +/* + * @test ParentChildTest + * @summary Verify parent-child relationship in terms of inner classes and cousins + * @library /testlibrary / + * @modules java.base + * @run main runtime.valhalla.nestmates.ParentChildTest + */ +public class ParentChildTest { + public static void main(String[] args) { + ParentChildTest parentChildTest = new ParentChildTest(); + parentChildTest.run(); + } + + public void run() { + SubTop subTop = new SubTop(); + SubTop.Inner inner = subTop.new Inner(); + SubTop.Inner.InnerInner innerInner = inner.new InnerInner(); + SubTop.InnerCousin innerCousin = subTop.new InnerCousin(); + + Asserts.assertEquals(subTop.getSuperField(), 1, "top->parent"); + Asserts.assertEquals(inner.getSuperField(), 1, "inner->top->parent"); + Asserts.assertEquals(innerInner.getSuperField(), 1, "innerInner->inner->top->parent"); + Asserts.assertEquals(innerCousin.callCousinMethod(inner), 1, "cousin->inner->top->parent"); + } +} --- /dev/null 2016-02-12 19:50:36.000000000 +0300 +++ new/test/runtime/valhalla/nestmates/p1/Sup.java 2016-02-12 19:50:36.000000000 +0300 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016, 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. + * + * 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. + */ + +package runtime.valhalla.nestmates.p1; + +public class Sup { + protected int field = 1; + protected int getField() { + return field; + } +} --- /dev/null 2016-02-12 19:50:36.000000000 +0300 +++ new/test/runtime/valhalla/nestmates/p2/InnerClassesHolder.java 2016-02-12 19:50:36.000000000 +0300 @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2016, 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. + * + * 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. + */ + +package runtime.valhalla.nestmates.p2; + +// a class that contains all possible nested classes +public class InnerClassesHolder { + + // constants to be referenced from the tests + public static final String MODIFIER_PRIVATE = "private"; + public static final String MODIFIER_PROTECTED = "protected"; + public static final String MODIFIER_PPRIVATE = "pprivate"; + public static final String MODIFIER_PUBLIC = "public"; + + private int fieldPrivate = 1; + protected int fieldProtected = 2; + int fieldPPrivate = 3; + public int fieldPublic = 4; + + private static int staticFieldPrivate = 1; + protected static int staticFieldProtected = 2; + static int staticFieldPPrivate = 3; + public static int staticFieldPublic = 4; + + public static class StaticNestedClass { + + public static int getEnclosingFieldValue(String modifier) { + int returnValue = -1; + switch (modifier) { + case MODIFIER_PRIVATE: { + returnValue = staticFieldPrivate; + break; + } + case MODIFIER_PROTECTED: { + returnValue = staticFieldProtected; + break; + } + case MODIFIER_PPRIVATE: { + returnValue = staticFieldPPrivate; + break; + } + case MODIFIER_PUBLIC: { + returnValue = staticFieldPublic; + break; + } + } + return returnValue; + } + } + + public class InnerBase { + public int getEnclosingFieldValue(String modifier) { + int returnValue = -1; + switch (modifier) { + case MODIFIER_PRIVATE: { + returnValue = fieldPrivate; + break; + } + case MODIFIER_PROTECTED: { + returnValue = fieldProtected; + break; + } + case MODIFIER_PPRIVATE: { + returnValue = fieldPPrivate; + break; + } + case MODIFIER_PUBLIC: { + returnValue = fieldPublic; + break; + } + } + return returnValue; + } + } + + public class InnerClass extends InnerBase { + + } + + public class InnerCousin { + + private InnerClass innerClass; + + public InnerCousin(InnerClass innerClass) { + this.innerClass = innerClass; + } + + public int getEnclosingFieldValue(String modifier) { + return innerClass.getEnclosingFieldValue(modifier); + } + } + + public int getValueFromLocalClass(String modifier) { + class Local { + public int getEnclosingFieldValue(String modifier) { + int returnValue = -1; + switch (modifier) { + case MODIFIER_PRIVATE: { + returnValue = fieldPrivate; + break; + } + case MODIFIER_PROTECTED: { + returnValue = fieldProtected; + break; + } + case MODIFIER_PPRIVATE: { + returnValue = fieldPPrivate; + break; + } + case MODIFIER_PUBLIC: { + returnValue = fieldPublic; + break; + } + } + return returnValue; + } + } + + Local local = new Local(); + + return local.getEnclosingFieldValue(modifier); + } + + interface HelloWorld { + int getEnclosingFieldValue(String modifier); + } + + public int getValueFromAnonClass(String modifier) { + HelloWorld helloWorld = modifier1 -> { + int returnValue = -1; + switch (modifier1) { + case MODIFIER_PRIVATE: { + returnValue = fieldPrivate; + break; + } + case MODIFIER_PROTECTED: { + returnValue = fieldProtected; + break; + } + case MODIFIER_PPRIVATE: { + returnValue = fieldPPrivate; + break; + } + case MODIFIER_PUBLIC: { + returnValue = fieldPublic; + break; + } + } + return returnValue; + }; + return helloWorld.getEnclosingFieldValue(modifier); + } +} --- /dev/null 2016-02-12 19:50:37.000000000 +0300 +++ new/test/runtime/valhalla/nestmates/p2/SubTop.java 2016-02-12 19:50:36.000000000 +0300 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2016, 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. + * + * 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. + */ + +package runtime.valhalla.nestmates.p2; + +import runtime.valhalla.nestmates.p1.Sup; + +public class SubTop extends Sup { + + public int getSuperField() { + return field; + } + + public class Inner { + + public int getSuperField() { + return field; + } + + public class InnerInner { + + public int getSuperField() { + return field; + } + } + } + + public class InnerCousin { + + public int callCousinMethod(Inner inner) { + return inner.getSuperField(); + } + } +}