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 using core reflection
  29  * @run main TestReflection
  30  * @run main/othervm -Dsun.reflect.noInflation=true TestReflection
  31  */
  32 
  33 // The first run will use NativeConstructorAccessor and due to the limited
  34 // number of calls we will not reach the inflation threshold.
  35 // The second run disables inflation so we will use the
  36 // GeneratedConstructorAccessor instead.
  37 // In this way both sets of Reflection classes are tested.
  38 
  39 public class TestReflection {
  40 
  41     // All constructors are private to ensure nestmate access checks apply
  42 
  43     // All doConstruct methods are public so they don't involve invoke_special
  44 
  45     private TestReflection() {}
  46 
  47     // The various nestmates
  48 
  49     // Note: No constructor on interfaces so no StaticIface variants
  50 
  51     static interface StaticIface {
  52 
  53         // Methods that will access private constructors of nestmates.
  54 
  55         default void doConstruct(TestReflection o) throws Throwable {
  56           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  57         }
  58         default void doConstruct(TestReflection tr, InnerNested o) throws Throwable {
  59           Object obj = InnerNested.class.getDeclaredConstructor(new Class<?>[] {TestReflection.class}).newInstance(new Object[] { tr });
  60         }
  61         default void doConstruct(StaticNested o) throws Throwable {
  62           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  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(TestReflection o) throws Throwable {
  74           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  75         }
  76         public  void doConstruct(TestReflection tr, InnerNested o) throws Throwable {
  77           Object obj = InnerNested.class.getDeclaredConstructor(new Class<?>[] {TestReflection.class}).newInstance(new Object[] { tr });
  78         }
  79         public void doConstruct(StaticNested o) throws Throwable {
  80           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  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(TestReflection o) throws Throwable {
  92           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  93         }
  94         public  void doConstruct(TestReflection tr, InnerNested o) throws Throwable {
  95           Object obj = InnerNested.class.getDeclaredConstructor(new Class<?>[] {TestReflection.class}).newInstance(new Object[] { tr });
  96         }
  97         public void doConstruct(StaticNested o) throws Throwable {
  98           Object obj = o.getClass().getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
  99         }
 100     }
 101 
 102     public static void main(String[] args) throws Throwable {
 103         // These initial constructions test nest-host access
 104         TestReflection o = TestReflection.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
 105         StaticNested s = StaticNested.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]);
 106         InnerNested i = InnerNested.class.getDeclaredConstructor(new Class<?>[] {TestReflection.class}).newInstance(new Object[] { o });
 107 
 108         StaticIface intf = new StaticIface() {};
 109 
 110         s.doConstruct(o);
 111         s.doConstruct(o, i);
 112         s.doConstruct(s);
 113 
 114         i.doConstruct(o);
 115         i.doConstruct(o, i);
 116         i.doConstruct(s);
 117 
 118         intf.doConstruct(o);
 119         intf.doConstruct(o, i);
 120         intf.doConstruct(s);
 121     }
 122 }