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 that will
  29  *          generate invokespecial for the calls
  30  * @compile -XDdisablePrivateAccessors TestInvokeSpecial.java
  31  * @run main TestInvokeSpecial
  32  */
  33 
  34 public class TestInvokeSpecial {
  35 
  36     // All constructors are private to ensure nestmate access checks apply
  37 
  38     // All doConstruct methods are public so they don't involve invoke_special
  39 
  40     private TestInvokeSpecial() {}
  41 
  42     // The various nestmates
  43 
  44     static interface StaticIface {
  45 
  46         // Methods that will access private constructors of nestmates.
  47         // The arg is a dummy for overloading purposes
  48 
  49         default void doConstruct(TestInvokeSpecial o) {
  50             Object obj = new TestInvokeSpecial();
  51         }
  52         default void doConstruct(InnerNested o) {
  53             Object obj = new TestInvokeSpecial().new InnerNested();
  54         }
  55         default void doConstruct(StaticNested o) {
  56             Object obj = new StaticNested();
  57         }
  58         default void doConstruct(StaticIface o) {
  59             Object obj = new StaticIface() {};
  60         }
  61     }
  62 
  63     static class StaticNested {
  64 
  65         private StaticNested() {}
  66 
  67         // Methods that will access private constructors of nestmates.
  68         // The arg is a dummy for overloading purposes
  69 
  70         public void doConstruct(TestInvokeSpecial o) {
  71             Object obj = new TestInvokeSpecial();
  72         }
  73         public void doConstruct(InnerNested o) {
  74             Object obj = new TestInvokeSpecial().new InnerNested();
  75         }
  76         public void doConstruct(StaticNested o) {
  77             Object obj = new StaticNested();
  78         }
  79         public void doConstruct(StaticIface o) {
  80             Object obj = new StaticIface() {};
  81         }
  82     }
  83 
  84     class InnerNested {
  85 
  86         private InnerNested() {}
  87 
  88         // Methods that will access private constructors of nestmates.
  89         // The arg is a dummy for overloading purposes
  90 
  91         public void doConstruct(TestInvokeSpecial o) {
  92             Object obj = new TestInvokeSpecial();
  93         }
  94         public void doConstruct(InnerNested o) {
  95             Object obj = new TestInvokeSpecial().new InnerNested();
  96         }
  97         public void doConstruct(StaticNested o) {
  98             Object obj = new StaticNested();
  99         }
 100         public void doConstruct(StaticIface o) {
 101             Object obj = new StaticIface() {};
 102         }
 103     }
 104 
 105     public static void main(String[] args) {
 106         // These initial constructions test nest-top access
 107         TestInvokeSpecial o = new TestInvokeSpecial();
 108         StaticNested s = new StaticNested();
 109         InnerNested i = o.new InnerNested();
 110         StaticIface intf = new StaticIface() {};
 111 
 112         s.doConstruct(o);
 113         s.doConstruct(i);
 114         s.doConstruct(s);
 115         s.doConstruct(intf);
 116 
 117         i.doConstruct(o);
 118         i.doConstruct(i);
 119         i.doConstruct(s);
 120         i.doConstruct(intf);
 121 
 122         intf.doConstruct(o);
 123         intf.doConstruct(i);
 124         intf.doConstruct(s);
 125         intf.doConstruct(intf);
 126     }
 127 }