< prev index next >

src/java.base/share/classes/jdk/internal/reflect/Reflection.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.reflect;
  27 
  28 
  29 import java.lang.reflect.*;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 import java.util.Objects;
  35 import jdk.internal.HotSpotIntrinsicCandidate;
  36 import jdk.internal.misc.VM;

  37 
  38 /** Common utility routines used by both java.lang and
  39     java.lang.reflect */
  40 
  41 public class Reflection {
  42 
  43     /** Used to filter out fields and methods from certain classes from public
  44         view, where they are sensitive or they may contain VM-internal objects.
  45         These Maps are updated very rarely. Rather than synchronize on
  46         each access, we use copy-on-write */
  47     private static volatile Map<Class<?>,String[]> fieldFilterMap;
  48     private static volatile Map<Class<?>,String[]> methodFilterMap;
  49 
  50     static {
  51         Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>();
  52         map.put(Reflection.class,
  53             new String[] {"fieldFilterMap", "methodFilterMap"});
  54         map.put(System.class, new String[] {"security"});
  55         map.put(Class.class, new String[] {"classLoader"});
  56         fieldFilterMap = map;


 327     private static boolean isExtClassLoader(ClassLoader loader) {
 328         ClassLoader cl = ClassLoader.getSystemClassLoader();
 329         while (cl != null) {
 330             if (cl.getParent() == null && cl == loader) {
 331                 return true;
 332             }
 333             cl = cl.getParent();
 334         }
 335         return false;
 336     }
 337 
 338 
 339     // true to print a stack trace when IAE is thrown
 340     private static volatile boolean printStackWhenAccessFails;
 341 
 342     // true if printStackWhenAccessFails has been initialized
 343     private static volatile boolean printStackWhenAccessFailsSet;
 344 
 345     private static void printStackTraceIfNeeded(Throwable e) {
 346         if (!printStackWhenAccessFailsSet && VM.initLevel() >= 1) {
 347             // can't use method reference here, might be too early in startup
 348             PrivilegedAction<Boolean> pa = new PrivilegedAction<Boolean>() {
 349                 public Boolean run() {
 350                     String s;
 351                     s = System.getProperty("sun.reflect.debugModuleAccessChecks");
 352                     return (s != null && !s.equalsIgnoreCase("false"));
 353                 }
 354             };
 355             printStackWhenAccessFails = AccessController.doPrivileged(pa);
 356             printStackWhenAccessFailsSet = true;
 357         }
 358         if (printStackWhenAccessFails) {
 359             e.printStackTrace();
 360         }
 361     }
 362 
 363     /**
 364      * Throws IllegalAccessException with the an exception message based on
 365      * the access that is denied.
 366      */
 367     private static void throwIllegalAccessException(Class<?> currentClass,
 368                                                     Class<?> memberClass,
 369                                                     Object target,
 370                                                     int modifiers)
 371         throws IllegalAccessException
 372     {
 373         String currentSuffix = "";
 374         String memberSuffix = "";
 375         Module m1 = currentClass.getModule();




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.reflect;
  27 
  28 
  29 import java.lang.reflect.*;


  30 import java.util.HashMap;
  31 import java.util.Map;
  32 import java.util.Objects;
  33 import jdk.internal.HotSpotIntrinsicCandidate;
  34 import jdk.internal.misc.VM;
  35 import sun.security.action.GetPropertyAction;
  36 
  37 /** Common utility routines used by both java.lang and
  38     java.lang.reflect */
  39 
  40 public class Reflection {
  41 
  42     /** Used to filter out fields and methods from certain classes from public
  43         view, where they are sensitive or they may contain VM-internal objects.
  44         These Maps are updated very rarely. Rather than synchronize on
  45         each access, we use copy-on-write */
  46     private static volatile Map<Class<?>,String[]> fieldFilterMap;
  47     private static volatile Map<Class<?>,String[]> methodFilterMap;
  48 
  49     static {
  50         Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>();
  51         map.put(Reflection.class,
  52             new String[] {"fieldFilterMap", "methodFilterMap"});
  53         map.put(System.class, new String[] {"security"});
  54         map.put(Class.class, new String[] {"classLoader"});
  55         fieldFilterMap = map;


 326     private static boolean isExtClassLoader(ClassLoader loader) {
 327         ClassLoader cl = ClassLoader.getSystemClassLoader();
 328         while (cl != null) {
 329             if (cl.getParent() == null && cl == loader) {
 330                 return true;
 331             }
 332             cl = cl.getParent();
 333         }
 334         return false;
 335     }
 336 
 337 
 338     // true to print a stack trace when IAE is thrown
 339     private static volatile boolean printStackWhenAccessFails;
 340 
 341     // true if printStackWhenAccessFails has been initialized
 342     private static volatile boolean printStackWhenAccessFailsSet;
 343 
 344     private static void printStackTraceIfNeeded(Throwable e) {
 345         if (!printStackWhenAccessFailsSet && VM.initLevel() >= 1) {
 346             String s = GetPropertyAction
 347                     .getProperty("sun.reflect.debugModuleAccessChecks");
 348             printStackWhenAccessFails =
 349                     (s != null && !s.equalsIgnoreCase("false"));





 350             printStackWhenAccessFailsSet = true;
 351         }
 352         if (printStackWhenAccessFails) {
 353             e.printStackTrace();
 354         }
 355     }
 356 
 357     /**
 358      * Throws IllegalAccessException with the an exception message based on
 359      * the access that is denied.
 360      */
 361     private static void throwIllegalAccessException(Class<?> currentClass,
 362                                                     Class<?> memberClass,
 363                                                     Object target,
 364                                                     int modifiers)
 365         throws IllegalAccessException
 366     {
 367         String currentSuffix = "";
 368         String memberSuffix = "";
 369         Module m1 = currentClass.getModule();


< prev index next >