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