1 /*
   2  * Copyright (c) 2005, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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 
  27 package com.sun.javafx.reflect;
  28 
  29 import java.lang.reflect.Proxy;
  30 
  31 public final class ReflectUtil {
  32 
  33     private ReflectUtil() {
  34     }
  35 
  36     /**
  37      * Checks package access on the given class.
  38      *
  39      * If it is a {@link Proxy#isProxyClass(java.lang.Class)} that implements
  40      * a non-public interface (i.e. may be in a non-restricted package),
  41      * also check the package access on the proxy interfaces.
  42      */
  43     public static void checkPackageAccess(Class<?> clazz) {
  44         SecurityManager s = System.getSecurityManager();
  45         if (s != null) {
  46             privateCheckPackageAccess(s, clazz);
  47         }
  48     }
  49 
  50     /**
  51      * NOTE: should only be called if a SecurityManager is installed
  52      */
  53     private static void privateCheckPackageAccess(SecurityManager s, Class<?> clazz) {
  54         while (clazz.isArray()) {
  55             clazz = clazz.getComponentType();
  56         }
  57 
  58         String pkg = clazz.getPackageName();
  59         if (pkg != null && !pkg.isEmpty()) {
  60             s.checkPackageAccess(pkg);
  61         }
  62 
  63         if (isNonPublicProxyClass(clazz)) {
  64             privateCheckProxyPackageAccess(s, clazz);
  65         }
  66     }
  67 
  68     /**
  69      * Checks package access on the given classname.
  70      * This method is typically called when the Class instance is not
  71      * available and the caller attempts to load a class on behalf
  72      * the true caller (application).
  73      */
  74     public static void checkPackageAccess(String name) {
  75         SecurityManager s = System.getSecurityManager();
  76         if (s != null) {
  77             String cname = name.replace('/', '.');
  78             if (cname.startsWith("[")) {
  79                 int b = cname.lastIndexOf('[') + 2;
  80                 if (b > 1 && b < cname.length()) {
  81                     cname = cname.substring(b);
  82                 }
  83             }
  84             int i = cname.lastIndexOf('.');
  85             if (i != -1) {
  86                 s.checkPackageAccess(cname.substring(0, i));
  87             }
  88         }
  89     }
  90 
  91     public static boolean isPackageAccessible(Class<?> clazz) {
  92         try {
  93             checkPackageAccess(clazz);
  94         } catch (SecurityException e) {
  95             return false;
  96         }
  97         return true;
  98     }
  99 
 100     /**
 101      * NOTE: should only be called if a SecurityManager is installed
 102      */
 103     private static void privateCheckProxyPackageAccess(SecurityManager s, Class<?> clazz) {
 104         // check proxy interfaces if the given class is a proxy class
 105         if (Proxy.isProxyClass(clazz)) {
 106             for (Class<?> intf : clazz.getInterfaces()) {
 107                 privateCheckPackageAccess(s, intf);
 108             }
 109         }
 110     }
 111 
 112     // Note that bytecode instrumentation tools may exclude 'sun.*'
 113     // classes but not generated proxy classes and so keep it in com.sun.*
 114     public static final String PROXY_PACKAGE = "com.sun.proxy";
 115 
 116     /**
 117      * Test if the given class is a proxy class that implements
 118      * non-public interface.  Such proxy class may be in a non-restricted
 119      * package that bypasses checkPackageAccess.
 120      */
 121     public static boolean isNonPublicProxyClass(Class<?> cls) {
 122         if (!Proxy.isProxyClass(cls)) {
 123             return false;
 124         }
 125         String pkg = cls.getPackageName();
 126         return pkg == null || !pkg.startsWith(PROXY_PACKAGE);
 127     }
 128 }