1 /*
   2  * Copyright (c) 2018, 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 import java.lang.reflect.Constructor;
  25 import java.lang.reflect.Field;
  26 import java.lang.reflect.Method;
  27 import java.util.concurrent.Callable;
  28 
  29 /**
  30  * Each nested class tests a member of a specific access.
  31  *
  32  * Caller class is cached when setAccessible is not used to suppress the access
  33  * check and only access allowed.  If not accessible, caller class is not cached.
  34  */
  35 public class AccessTest {
  36     public static class PublicConstructor implements Callable<Object> {
  37         public Object call() throws Exception {
  38             Constructor c = Members.class.getConstructor();
  39             return c.newInstance();
  40         }
  41     }
  42 
  43     public static class PublicMethod extends Members implements Callable<Void> {
  44         public Void call() throws Exception {
  45             Method m = Members.class.getDeclaredMethod("publicMethod");
  46             m.invoke(new PublicMethod());
  47             return null;
  48         }
  49     }
  50 
  51     public static class ProtectedMethod extends Members implements Callable<Void> {
  52         public Void call() throws Exception {
  53             Method m = Members.class.getDeclaredMethod("protectedMethod");
  54             m.invoke(new ProtectedMethod());
  55             return null;
  56         }
  57     }
  58     
  59     /*
  60      * private field is not accessible.  So caller class is not cached.
  61      */
  62     public static class PrivateMethod extends Members implements Callable<Void> {
  63         public Void call() throws Exception {
  64             Method m = Members.class.getDeclaredMethod("privateMethod");
  65             try {
  66                 m.invoke(new ProtectedMethod());
  67             } catch (IllegalAccessException e) {
  68             }
  69             return null;
  70         }
  71     }
  72 
  73     public static class PublicField extends Members implements Callable<Void> {
  74         public Void call() throws Exception {
  75             Field f = Members.class.getDeclaredField("publicField");
  76             f.get(new PublicField());
  77             return null;
  78         }
  79     }
  80 
  81     public static class ProtectedField extends Members implements Callable<Void> {
  82         public Void call() throws Exception {
  83             Field f = Members.class.getDeclaredField("protectedField");
  84             f.get(new ProtectedField());
  85             return null;
  86         }
  87     }
  88 
  89     /*
  90      * private field is not accessible.  So caller class is not cached.
  91      */
  92     public static class PrivateField implements Callable<Void> {
  93         public Void call() throws Exception {
  94             Field f = Members.class.getDeclaredField("privateField");
  95             try {
  96                 f.get(new Members());
  97             } catch (IllegalAccessException e) {
  98             }
  99             return null;
 100         }
 101     }
 102 
 103 }