jdk/src/share/classes/sun/reflect/misc/ReflectUtil.java

Print this page
rev 5671 : 7197546: (proxy) Reflect about creating reflective proxies
Reviewed-by: alanb, jdn, jrose


 127                 int b = cname.lastIndexOf('[') + 2;
 128                 if (b > 1 && b < cname.length()) {
 129                     cname = cname.substring(b);
 130                 }
 131             }
 132             int i = cname.lastIndexOf('.');
 133             if (i != -1) {
 134                 s.checkPackageAccess(cname.substring(0, i));
 135             }
 136         }
 137     }
 138 
 139     public static boolean isPackageAccessible(Class clazz) {
 140         try {
 141             checkPackageAccess(clazz);
 142         } catch (SecurityException e) {
 143             return false;
 144         }
 145         return true;
 146     }



























































 147 }


 127                 int b = cname.lastIndexOf('[') + 2;
 128                 if (b > 1 && b < cname.length()) {
 129                     cname = cname.substring(b);
 130                 }
 131             }
 132             int i = cname.lastIndexOf('.');
 133             if (i != -1) {
 134                 s.checkPackageAccess(cname.substring(0, i));
 135             }
 136         }
 137     }
 138 
 139     public static boolean isPackageAccessible(Class clazz) {
 140         try {
 141             checkPackageAccess(clazz);
 142         } catch (SecurityException e) {
 143             return false;
 144         }
 145         return true;
 146     }
 147 
 148     // Returns true if p is an ancestor of cl i.e. class loader 'p' can
 149     // be found in the cl's delegation chain
 150     private static boolean isAncestor(ClassLoader p, ClassLoader cl) {
 151         ClassLoader acl = cl;
 152         do {
 153             acl = acl.getParent();
 154             if (p == acl) {
 155                 return true;
 156             }
 157         } while (acl != null);
 158         return false;
 159     }
 160 
 161     /**
 162      * Returns true if package access check is needed for reflective
 163      * access from a class loader 'from' to classes or members in
 164      * a class defined by class loader 'to'.  This method returns true
 165      * if 'from' is not the same as or an ancestor of 'to'.  All code
 166      * in a system domain are granted with all permission and so this
 167      * method returns false if 'from' class loader is a class loader
 168      * loading system classes.  On the other hand, if a class loader
 169      * attempts to access system domain classes, it requires package
 170      * access check and this method will return true.
 171      */
 172     public static boolean needsPackageAccessCheck(ClassLoader from, ClassLoader to) {
 173         if (from == null || from == to)
 174             return false;
 175 
 176         if (to == null)
 177             return true;
 178 
 179         return !isAncestor(from, to);
 180     }
 181 
 182     /**
 183      * Access check on the interfaces that a proxy class implements and throw
 184      * {@code SecurityException} if it accesses a restricted package.
 185      *
 186      * @param ccl the caller's class loader
 187      * @param interfaces the list of interfaces that a proxy class implements
 188      *
 189      * @see Proxy#checkProxyAccess
 190      */
 191     public static void checkProxyPackageAccess(ClassLoader ccl,
 192                                                Class<?>... interfaces)
 193     {
 194         SecurityManager sm = System.getSecurityManager();
 195         if (sm != null) {
 196             for (Class<?> intf : interfaces) {
 197                 ClassLoader cl = intf.getClassLoader();
 198                 if (needsPackageAccessCheck(ccl, cl)) {
 199                     checkPackageAccess(intf);
 200                 }
 201             }
 202         }
 203     }
 204 
 205     public static final String PROXY_PACKAGE = "sun.proxy";
 206 }