1 /* 2 * Copyright (c) 2005, 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 sun.reflect.misc; 28 29 import java.lang.reflect.Modifier; 30 import sun.reflect.Reflection; 31 32 public final class ReflectUtil { 33 34 private ReflectUtil() { 35 } 36 37 public static Class forName(String name) 38 throws ClassNotFoundException { 39 checkPackageAccess(name); 40 return Class.forName(name); 41 } 42 43 public static Object newInstance(Class cls) 44 throws InstantiationException, IllegalAccessException { 45 checkPackageAccess(cls); 46 return cls.newInstance(); 47 } 48 49 /* 50 * Reflection.ensureMemberAccess is overly-restrictive 51 * due to a bug. We awkwardly work around it for now. 52 */ 53 public static void ensureMemberAccess(Class currentClass, 54 Class memberClass, 55 Object target, 56 int modifiers) 57 throws IllegalAccessException 58 { 59 if (target == null && Modifier.isProtected(modifiers)) { 60 int mods = modifiers; 61 mods = mods & (~Modifier.PROTECTED); 62 mods = mods | Modifier.PUBLIC; 63 64 /* 65 * See if we fail because of class modifiers 66 */ 67 Reflection.ensureMemberAccess(currentClass, 68 memberClass, 69 target, 70 mods); 71 try { 72 /* 73 * We're still here so class access was ok. 74 * Now try with default field access. 75 */ 76 mods = mods & (~Modifier.PUBLIC); 77 Reflection.ensureMemberAccess(currentClass, 78 memberClass, 79 target, 80 mods); 81 /* 82 * We're still here so access is ok without 83 * checking for protected. 84 */ 85 return; 86 } catch (IllegalAccessException e) { 87 /* 88 * Access failed but we're 'protected' so 89 * if the test below succeeds then we're ok. 90 */ 91 if (isSubclassOf(currentClass, memberClass)) { 92 return; 93 } else { 94 throw e; 95 } 96 } 97 } else { 98 Reflection.ensureMemberAccess(currentClass, 99 memberClass, 100 target, 101 modifiers); 102 } 103 } 104 105 private static boolean isSubclassOf(Class queryClass, 106 Class ofClass) 107 { 108 while (queryClass != null) { 109 if (queryClass == ofClass) { 110 return true; 111 } 112 queryClass = queryClass.getSuperclass(); 113 } 114 return false; 115 } 116 117 118 public static void checkPackageAccess(Class clazz) { 119 checkPackageAccess(clazz.getName()); 120 } 121 122 public static void checkPackageAccess(String name) { 123 SecurityManager s = System.getSecurityManager(); 124 if (s != null) { 125 String cname = name.replace('/', '.'); 126 if (cname.startsWith("[")) { 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 }