< prev index next >

test/hotspot/jtreg/runtime/Nestmates/privateMethods/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 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 void priv_invoke() {
  36         System.out.println("TestReflection::priv_invoke");
  37     }
  38 
  39     // public constructor so we aren't relying on private access
  40     public TestReflection() {}
  41 
  42     // Methods that will access private methods of nestmates
  43 
  44     void access_priv(TestReflection o) throws Throwable {
  45         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  46     }
  47     void access_priv(InnerNested o) throws Throwable {
  48         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  49     }
  50     void access_priv(StaticNested o) throws Throwable {
  51         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  52     }
  53     void access_priv(StaticIface o) throws Throwable {

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

  77             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  78         }
  79     }
  80 
  81     static class StaticNested {
  82 
  83         private void priv_invoke() {
  84             System.out.println("StaticNested::priv_invoke");
  85         }
  86 
  87         // public constructor so we aren't relying on private access
  88         public StaticNested() {}
  89 
  90         // Methods that will access private methods of nestmates
  91 
  92         void access_priv(TestReflection o) throws Throwable {
  93             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  94         }
  95         void access_priv(InnerNested o) throws Throwable {
  96             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  97         }
  98         void access_priv(StaticNested o) throws Throwable {
  99             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 100         }
 101         void access_priv(StaticIface o) throws Throwable {

 102             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 103         }
 104     }
 105 
 106     class InnerNested {
 107 
 108         private void priv_invoke() {
 109             System.out.println("InnerNested::priv_invoke");
 110         }
 111 
 112         // public constructor so we aren't relying on private access
 113         public InnerNested() {}
 114 
 115         void access_priv(TestReflection o) throws Throwable {
 116             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 117         }
 118         void access_priv(InnerNested o) throws Throwable {
 119             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 120         }
 121         void access_priv(StaticNested o) throws Throwable {
 122             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 123         }
 124         void access_priv(StaticIface o) throws Throwable {

 125             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 126         }
 127     }
 128 
 129     public static void main(String[] args) throws Throwable {
 130         TestReflection o = new TestReflection();
 131         StaticNested s = new StaticNested();
 132         InnerNested i = o.new InnerNested();
 133         StaticIface intf = new StaticIface() {};
 134 
 135         o.access_priv(new TestReflection());
 136         o.access_priv(i);
 137         o.access_priv(s);
 138         o.access_priv(intf);
 139 
 140         s.access_priv(o);
 141         s.access_priv(i);
 142         s.access_priv(new StaticNested());
 143         s.access_priv(intf);
 144 


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