1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8046171 27 * @summary Test access to private constructors between nestmates and nest-top 28 * using different flavours of named nested types using method handles 29 * @compile -XDdisablePrivateAccessors TestMethodHandles.java 30 * @run main TestMethodHandles 31 */ 32 33 import java.lang.invoke.*; 34 import static java.lang.invoke.MethodHandles.*; 35 import static java.lang.invoke.MethodType.*; 36 37 public class TestMethodHandles { 38 39 static final MethodType NOARG_T = MethodType.methodType(void.class); 40 static final MethodType INNER_T = MethodType.methodType(void.class, TestMethodHandles.class); 41 42 // All constructors are private to ensure nestmate access checks apply 43 44 // All doConstruct methods are public so they don't involve invoke_special 45 46 private TestMethodHandles() {} 47 48 // The various nestmates 49 50 // Note: No constructor on interfaces so no StaticIface variants 51 52 static interface StaticIface { 53 54 // Methods that will access private constructors of nestmates. 55 // The arg is a dummy for overloading purposes 56 57 default void doConstruct(TestMethodHandles o) throws Throwable { 58 MethodHandle mh = 59 lookup().findConstructor(TestMethodHandles.class, NOARG_T); 60 TestMethodHandles obj = (TestMethodHandles) mh.invoke(); 61 obj = (TestMethodHandles) mh.invokeExact(); 62 } 63 default void doConstruct(TestMethodHandles outer, InnerNested o) throws Throwable { 64 MethodHandle mh = 65 lookup().findConstructor(InnerNested.class, INNER_T); 66 InnerNested obj = (InnerNested) mh.invoke(outer); 67 obj = (InnerNested) mh.invokeExact(outer); 68 } 69 default void doConstruct(StaticNested o) throws Throwable { 70 MethodHandle mh = 71 lookup().findConstructor(StaticNested.class, NOARG_T); 72 StaticNested obj = (StaticNested) mh.invoke(); 73 obj = (StaticNested) mh.invokeExact(); 74 } 75 } 76 77 static class StaticNested { 78 79 private StaticNested() {} 80 81 // Methods that will access private constructors of nestmates. 82 // The arg is a dummy for overloading purposes 83 84 public void doConstruct(TestMethodHandles o) throws Throwable { 85 MethodHandle mh = 86 lookup().findConstructor(TestMethodHandles.class, NOARG_T); 87 TestMethodHandles obj = (TestMethodHandles) mh.invoke(); 88 obj = (TestMethodHandles) mh.invokeExact(); 89 } 90 public void doConstruct(TestMethodHandles outer, InnerNested o) throws Throwable { 91 MethodHandle mh = 92 lookup().findConstructor(InnerNested.class, INNER_T); 93 InnerNested obj = (InnerNested) mh.invoke(outer); 94 obj = (InnerNested) mh.invokeExact(outer); 95 } 96 public void doConstruct(StaticNested o) throws Throwable { 97 MethodHandle mh = 98 lookup().findConstructor(StaticNested.class, NOARG_T); 99 StaticNested obj = (StaticNested) mh.invoke(); 100 obj = (StaticNested) mh.invokeExact(); 101 } 102 } 103 104 class InnerNested { 105 106 private InnerNested() {} 107 108 // Methods that will access private constructors of nestmates. 109 // The arg is a dummy for overloading purposes 110 111 public void doConstruct(TestMethodHandles o) throws Throwable { 112 MethodHandle mh = 113 lookup().findConstructor(TestMethodHandles.class, NOARG_T); 114 TestMethodHandles obj = (TestMethodHandles) mh.invoke(); 115 obj = (TestMethodHandles) mh.invokeExact(); 116 } 117 public void doConstruct(TestMethodHandles outer, InnerNested o) throws Throwable { 118 MethodHandle mh = 119 lookup().findConstructor(InnerNested.class, INNER_T); 120 InnerNested obj = (InnerNested) mh.invoke(outer); 121 obj = (InnerNested) mh.invokeExact(outer); 122 } 123 public void doConstruct(StaticNested o) throws Throwable { 124 MethodHandle mh = 125 lookup().findConstructor(StaticNested.class, NOARG_T); 126 StaticNested obj = (StaticNested) mh.invoke(); 127 obj = (StaticNested) mh.invokeExact(); 128 } 129 } 130 131 public static void main(String[] args) throws Throwable { 132 // These initial constructions test nest-top access 133 MethodHandle mh = 134 lookup().findConstructor(TestMethodHandles.class, NOARG_T); 135 TestMethodHandles o = (TestMethodHandles) mh.invoke(); 136 o = (TestMethodHandles) mh.invokeExact(); 137 138 mh = lookup().findConstructor(StaticNested.class, NOARG_T); 139 StaticNested s = (StaticNested) mh.invoke(); 140 s = (StaticNested) mh.invokeExact(); 141 142 mh = lookup().findConstructor(InnerNested.class, INNER_T); 143 InnerNested i = (InnerNested) mh.invoke(o); 144 i = (InnerNested) mh.invokeExact(o); 145 146 StaticIface intf = new StaticIface() {}; 147 148 s.doConstruct(o); 149 s.doConstruct(o, i); 150 s.doConstruct(s); 151 152 i.doConstruct(o); 153 i.doConstruct(o, i); 154 i.doConstruct(s); 155 156 intf.doConstruct(o); 157 intf.doConstruct(o, i); 158 intf.doConstruct(s); 159 } 160 }