< prev index next >

test/hotspot/jtreg/runtime/Nestmates/privateStaticMethods/TestReflection.java

Print this page




  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 static methods between nestmates and nest-top
  28  *          using different flavours of named nested types using core reflection
  29  * @run main TestReflection

  30  */
  31 






  32 public class TestReflection {
  33 
  34     // Private method of nest-top for nestmates to access
  35     private static void priv_static_invoke() {
  36         System.out.println("TestReflection::priv_static_invoke");
  37     }
  38 
  39     // public constructor so we aren't relying on private access
  40     public TestReflection() {}
  41 


  42     // Methods that will access private static methods of nestmates
  43 
  44     // NOTE: No InnerNested calls in this test because non-static nested types
  45     // can't have static methods
  46 
  47     void access_priv(TestReflection o) throws Throwable {
  48         o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  49     }
  50     void access_priv(StaticNested o) throws Throwable {
  51         o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  52     }
  53     void access_priv(StaticIface o) throws Throwable {

  54         StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  55     }
  56 
  57     // The various nestmates
  58 
  59     static interface StaticIface {
  60 
  61         private static void priv_static_invoke() {
  62             System.out.println("StaticIface::priv_static_invoke");
  63         }
  64 
  65         // Methods that will access private static methods of nestmates
  66 
  67         default void access_priv(TestReflection o) throws Throwable {
  68             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  69         }
  70         default void access_priv(StaticNested o) throws Throwable {
  71             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  72         }
  73         default void access_priv(StaticIface o) throws Throwable {

  74             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  75         }
  76     }
  77 
  78     static class StaticNested {
  79 
  80         private static void priv_static_invoke() {
  81             System.out.println("StaticNested::priv_static_invoke");
  82         }
  83 
  84         // public constructor so we aren't relying on private access
  85         public StaticNested() {}
  86 
  87         // Methods that will access private static methods of nestmates
  88 
  89         void access_priv(TestReflection o) throws Throwable {
  90             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  91         }
  92         void access_priv(StaticNested o) throws Throwable {
  93             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

  94         }
  95         void access_priv(StaticIface o) throws Throwable {

  96             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  97         }
  98     }
  99 
 100     class InnerNested {
 101 
 102         // public constructor so we aren't relying on private access
 103         public InnerNested() {}
 104 
 105         void access_priv(TestReflection o) throws Throwable {
 106             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

 107         }
 108         void access_priv(StaticNested o) throws Throwable {
 109             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);

 110         }
 111         void access_priv(StaticIface o) throws Throwable {

 112             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 113         }
 114     }
 115 
 116     public static void main(String[] args) throws Throwable {
 117         TestReflection o = new TestReflection();
 118         StaticNested s = new StaticNested();
 119         InnerNested i = o.new InnerNested();
 120         StaticIface intf = new StaticIface() {};
 121 
 122         o.access_priv(new TestReflection());
 123         o.access_priv(s);
 124         o.access_priv(intf);
 125 
 126         s.access_priv(o);
 127         s.access_priv(new StaticNested());
 128         s.access_priv(intf);
 129 
 130         i.access_priv(o);
 131         i.access_priv(s);


  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 static methods between nestmates and nest-top
  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 NativeMethodAccessor and due to the limited number
  34 // of calls we will not reach the inflation threshold.
  35 // The second run disables inflation so we will use the GeneratedMethodAccessor
  36 // instead. In this way both sets of Reflection classes are tested.
  37 
  38 
  39 public class TestReflection {
  40 
  41     // Private method of nest-top for nestmates to access
  42     private static void priv_static_invoke() {
  43         System.out.println("TestReflection::priv_static_invoke");
  44     }
  45 
  46     // public constructor so we aren't relying on private access
  47     public TestReflection() {}
  48 
  49     // We test access via getClass() as well as Foo.class
  50 
  51     // Methods that will access private static methods of nestmates
  52 
  53     // NOTE: No InnerNested calls in this test because non-static nested types
  54     // can't have static methods
  55 
  56     void access_priv(TestReflection o) throws Throwable {
  57         o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  58         TestReflection.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  59     }
  60     void access_priv(StaticNested o) throws Throwable {
  61         o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  62         StaticNested.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  63     }
  64     void access_priv(StaticIface o) throws Throwable {
  65         // Can't use o.getClass() as the method is not in that class
  66         StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  67     }
  68 
  69     // The various nestmates
  70 
  71     static interface StaticIface {
  72 
  73         private static void priv_static_invoke() {
  74             System.out.println("StaticIface::priv_static_invoke");
  75         }
  76 
  77         // Methods that will access private static methods of nestmates
  78 
  79         default void access_priv(TestReflection o) throws Throwable {
  80             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  81             TestReflection.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  82         }
  83         default void access_priv(StaticNested o) throws Throwable {
  84             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  85             StaticNested.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  86         }
  87         default void access_priv(StaticIface o) throws Throwable {
  88             // Can't use o.getClass() as the method is not in that class
  89             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
  90         }
  91     }
  92 
  93     static class StaticNested {
  94 
  95         private static void priv_static_invoke() {
  96             System.out.println("StaticNested::priv_static_invoke");
  97         }
  98 
  99         // public constructor so we aren't relying on private access
 100         public StaticNested() {}
 101 
 102         // Methods that will access private static methods of nestmates
 103 
 104         void access_priv(TestReflection o) throws Throwable {
 105             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 106             TestReflection.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 107         }
 108         void access_priv(StaticNested o) throws Throwable {
 109             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 110             StaticNested.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 111         }
 112         void access_priv(StaticIface o) throws Throwable {
 113             // Can't use o.getClass() as the method is not in that class
 114             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 115         }
 116     }
 117 
 118     class InnerNested {
 119 
 120         // public constructor so we aren't relying on private access
 121         public InnerNested() {}
 122 
 123         void access_priv(TestReflection o) throws Throwable {
 124             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 125             TestReflection.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 126         }
 127         void access_priv(StaticNested o) throws Throwable {
 128             o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 129             StaticNested.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 130         }
 131         void access_priv(StaticIface o) throws Throwable {
 132             // Can't use o.getClass() as the method is not in that class
 133             StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]);
 134         }
 135     }
 136 
 137     public static void main(String[] args) throws Throwable {
 138         TestReflection o = new TestReflection();
 139         StaticNested s = new StaticNested();
 140         InnerNested i = o.new InnerNested();
 141         StaticIface intf = new StaticIface() {};
 142 
 143         o.access_priv(new TestReflection());
 144         o.access_priv(s);
 145         o.access_priv(intf);
 146 
 147         s.access_priv(o);
 148         s.access_priv(new StaticNested());
 149         s.access_priv(intf);
 150 
 151         i.access_priv(o);
 152         i.access_priv(s);
< prev index next >