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