feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
45
jdkSrc/jdk8/sun/reflect/misc/ConstructorUtil.java
Normal file
45
jdkSrc/jdk8/sun/reflect/misc/ConstructorUtil.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.reflect.misc;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
public final class ConstructorUtil {
|
||||
|
||||
private ConstructorUtil() {
|
||||
}
|
||||
|
||||
public static Constructor<?> getConstructor(Class<?> cls, Class<?>[] params)
|
||||
throws NoSuchMethodException {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getConstructor(params);
|
||||
}
|
||||
|
||||
public static Constructor<?>[] getConstructors(Class<?> cls) {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getConstructors();
|
||||
}
|
||||
}
|
||||
53
jdkSrc/jdk8/sun/reflect/misc/FieldUtil.java
Normal file
53
jdkSrc/jdk8/sun/reflect/misc/FieldUtil.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.reflect.misc;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/*
|
||||
* Create a trampoline class.
|
||||
*/
|
||||
public final class FieldUtil {
|
||||
|
||||
private FieldUtil() {
|
||||
}
|
||||
|
||||
public static Field getField(Class<?> cls, String name)
|
||||
throws NoSuchFieldException {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getField(name);
|
||||
}
|
||||
|
||||
public static Field[] getFields(Class<?> cls) {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getFields();
|
||||
}
|
||||
|
||||
public static Field[] getDeclaredFields(Class<?> cls) {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getDeclaredFields();
|
||||
}
|
||||
}
|
||||
410
jdkSrc/jdk8/sun/reflect/misc/MethodUtil.java
Normal file
410
jdkSrc/jdk8/sun/reflect/misc/MethodUtil.java
Normal file
@@ -0,0 +1,410 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.reflect.misc;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.security.AllPermission;
|
||||
import java.security.AccessController;
|
||||
import java.security.PermissionCollection;
|
||||
import java.security.SecureClassLoader;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.security.CodeSource;
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import sun.misc.IOUtils;
|
||||
|
||||
class Trampoline {
|
||||
static {
|
||||
if (Trampoline.class.getClassLoader() == null) {
|
||||
throw new Error(
|
||||
"Trampoline must not be defined by the bootstrap classloader");
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureInvocableMethod(Method m)
|
||||
throws InvocationTargetException
|
||||
{
|
||||
Class<?> clazz = m.getDeclaringClass();
|
||||
if (clazz.equals(AccessController.class) ||
|
||||
clazz.equals(Method.class) ||
|
||||
clazz.getName().startsWith("java.lang.invoke."))
|
||||
throw new InvocationTargetException(
|
||||
new UnsupportedOperationException("invocation not supported"));
|
||||
}
|
||||
|
||||
private static Object invoke(Method m, Object obj, Object[] params)
|
||||
throws InvocationTargetException, IllegalAccessException
|
||||
{
|
||||
ensureInvocableMethod(m);
|
||||
return m.invoke(obj, params);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a trampoline class.
|
||||
*/
|
||||
public final class MethodUtil extends SecureClassLoader {
|
||||
private static final String MISC_PKG = "sun.reflect.misc.";
|
||||
private static final String TRAMPOLINE = MISC_PKG + "Trampoline";
|
||||
private static final Method bounce = getTrampoline();
|
||||
|
||||
private MethodUtil() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static Method getMethod(Class<?> cls, String name, Class<?>[] args)
|
||||
throws NoSuchMethodException {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getMethod(name, args);
|
||||
}
|
||||
|
||||
public static Method[] getMethods(Class<?> cls) {
|
||||
ReflectUtil.checkPackageAccess(cls);
|
||||
return cls.getMethods();
|
||||
}
|
||||
|
||||
/*
|
||||
* Discover the public methods on public classes
|
||||
* and interfaces accessible to any caller by calling
|
||||
* Class.getMethods() and walking towards Object until
|
||||
* we're done.
|
||||
*/
|
||||
public static Method[] getPublicMethods(Class<?> cls) {
|
||||
// compatibility for update release
|
||||
if (System.getSecurityManager() == null) {
|
||||
return cls.getMethods();
|
||||
}
|
||||
Map<Signature, Method> sigs = new HashMap<Signature, Method>();
|
||||
while (cls != null) {
|
||||
boolean done = getInternalPublicMethods(cls, sigs);
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
getInterfaceMethods(cls, sigs);
|
||||
cls = cls.getSuperclass();
|
||||
}
|
||||
return sigs.values().toArray(new Method[sigs.size()]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Process the immediate interfaces of this class or interface.
|
||||
*/
|
||||
private static void getInterfaceMethods(Class<?> cls,
|
||||
Map<Signature, Method> sigs) {
|
||||
Class<?>[] intfs = cls.getInterfaces();
|
||||
for (int i=0; i < intfs.length; i++) {
|
||||
Class<?> intf = intfs[i];
|
||||
boolean done = getInternalPublicMethods(intf, sigs);
|
||||
if (!done) {
|
||||
getInterfaceMethods(intf, sigs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Process the methods in this class or interface
|
||||
*/
|
||||
private static boolean getInternalPublicMethods(Class<?> cls,
|
||||
Map<Signature, Method> sigs) {
|
||||
Method[] methods = null;
|
||||
try {
|
||||
/*
|
||||
* This class or interface is non-public so we
|
||||
* can't use any of it's methods. Go back and
|
||||
* try again with a superclass or superinterface.
|
||||
*/
|
||||
if (!Modifier.isPublic(cls.getModifiers())) {
|
||||
return false;
|
||||
}
|
||||
if (!ReflectUtil.isPackageAccessible(cls)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
methods = cls.getMethods();
|
||||
} catch (SecurityException se) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for inherited methods with non-public
|
||||
* declaring classes. They might override and hide
|
||||
* methods from their superclasses or
|
||||
* superinterfaces.
|
||||
*/
|
||||
boolean done = true;
|
||||
for (int i=0; i < methods.length; i++) {
|
||||
Class<?> dc = methods[i].getDeclaringClass();
|
||||
if (!Modifier.isPublic(dc.getModifiers())) {
|
||||
done = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (done) {
|
||||
/*
|
||||
* We're done. Spray all the methods into
|
||||
* the list and then we're out of here.
|
||||
*/
|
||||
for (int i=0; i < methods.length; i++) {
|
||||
addMethod(sigs, methods[i]);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Simulate cls.getDeclaredMethods() by
|
||||
* stripping away inherited methods.
|
||||
*/
|
||||
for (int i=0; i < methods.length; i++) {
|
||||
Class<?> dc = methods[i].getDeclaringClass();
|
||||
if (cls.equals(dc)) {
|
||||
addMethod(sigs, methods[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
private static void addMethod(Map<Signature, Method> sigs, Method method) {
|
||||
Signature signature = new Signature(method);
|
||||
if (!sigs.containsKey(signature)) {
|
||||
sigs.put(signature, method);
|
||||
} else if (!method.getDeclaringClass().isInterface()){
|
||||
/*
|
||||
* Superclasses beat interfaces.
|
||||
*/
|
||||
Method old = sigs.get(signature);
|
||||
if (old.getDeclaringClass().isInterface()) {
|
||||
sigs.put(signature, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A class that represents the unique elements of a method that will be a
|
||||
* key in the method cache.
|
||||
*/
|
||||
private static class Signature {
|
||||
private String methodName;
|
||||
private Class<?>[] argClasses;
|
||||
|
||||
private volatile int hashCode = 0;
|
||||
|
||||
Signature(Method m) {
|
||||
this.methodName = m.getName();
|
||||
this.argClasses = m.getParameterTypes();
|
||||
}
|
||||
|
||||
public boolean equals(Object o2) {
|
||||
if (this == o2) {
|
||||
return true;
|
||||
}
|
||||
Signature that = (Signature)o2;
|
||||
if (!(methodName.equals(that.methodName))) {
|
||||
return false;
|
||||
}
|
||||
if (argClasses.length != that.argClasses.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < argClasses.length; i++) {
|
||||
if (!(argClasses[i] == that.argClasses[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash code computed using algorithm suggested in
|
||||
* Effective Java, Item 8.
|
||||
*/
|
||||
public int hashCode() {
|
||||
if (hashCode == 0) {
|
||||
int result = 17;
|
||||
result = 37 * result + methodName.hashCode();
|
||||
if (argClasses != null) {
|
||||
for (int i = 0; i < argClasses.length; i++) {
|
||||
result = 37 * result + ((argClasses[i] == null) ? 0 :
|
||||
argClasses[i].hashCode());
|
||||
}
|
||||
}
|
||||
hashCode = result;
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Bounce through the trampoline.
|
||||
*/
|
||||
public static Object invoke(Method m, Object obj, Object[] params)
|
||||
throws InvocationTargetException, IllegalAccessException {
|
||||
try {
|
||||
return bounce.invoke(null, new Object[] {m, obj, params});
|
||||
} catch (InvocationTargetException ie) {
|
||||
Throwable t = ie.getCause();
|
||||
|
||||
if (t instanceof InvocationTargetException) {
|
||||
throw (InvocationTargetException)t;
|
||||
} else if (t instanceof IllegalAccessException) {
|
||||
throw (IllegalAccessException)t;
|
||||
} else if (t instanceof RuntimeException) {
|
||||
throw (RuntimeException)t;
|
||||
} else if (t instanceof Error) {
|
||||
throw (Error)t;
|
||||
} else {
|
||||
throw new Error("Unexpected invocation error", t);
|
||||
}
|
||||
} catch (IllegalAccessException iae) {
|
||||
// this can't happen
|
||||
throw new Error("Unexpected invocation error", iae);
|
||||
}
|
||||
}
|
||||
|
||||
private static Method getTrampoline() {
|
||||
try {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedExceptionAction<Method>() {
|
||||
public Method run() throws Exception {
|
||||
Class<?> t = getTrampolineClass();
|
||||
Class<?>[] types = {
|
||||
Method.class, Object.class, Object[].class
|
||||
};
|
||||
Method b = t.getDeclaredMethod("invoke", types);
|
||||
b.setAccessible(true);
|
||||
return b;
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new InternalError("bouncer cannot be found", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected synchronized Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException
|
||||
{
|
||||
// First, check if the class has already been loaded
|
||||
ReflectUtil.checkPackageAccess(name);
|
||||
Class<?> c = findLoadedClass(name);
|
||||
if (c == null) {
|
||||
try {
|
||||
c = findClass(name);
|
||||
} catch (ClassNotFoundException e) {
|
||||
// Fall through ...
|
||||
}
|
||||
if (c == null) {
|
||||
c = getParent().loadClass(name);
|
||||
}
|
||||
}
|
||||
if (resolve) {
|
||||
resolveClass(c);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
protected Class<?> findClass(final String name)
|
||||
throws ClassNotFoundException
|
||||
{
|
||||
if (!name.startsWith(MISC_PKG)) {
|
||||
throw new ClassNotFoundException(name);
|
||||
}
|
||||
String path = name.replace('.', '/').concat(".class");
|
||||
URL res = getResource(path);
|
||||
if (res != null) {
|
||||
try {
|
||||
return defineClass(name, res);
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException(name, e);
|
||||
}
|
||||
} else {
|
||||
throw new ClassNotFoundException(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Define the proxy classes
|
||||
*/
|
||||
private Class<?> defineClass(String name, URL url) throws IOException {
|
||||
byte[] b = getBytes(url);
|
||||
CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[])null);
|
||||
if (!name.equals(TRAMPOLINE)) {
|
||||
throw new IOException("MethodUtil: bad name " + name);
|
||||
}
|
||||
return defineClass(name, b, 0, b.length, cs);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Returns the contents of the specified URL as an array of bytes.
|
||||
*/
|
||||
private static byte[] getBytes(URL url) throws IOException {
|
||||
URLConnection uc = url.openConnection();
|
||||
if (uc instanceof java.net.HttpURLConnection) {
|
||||
java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
|
||||
int code = huc.getResponseCode();
|
||||
if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
|
||||
throw new IOException("open HTTP connection failed.");
|
||||
}
|
||||
}
|
||||
int len = uc.getContentLength();
|
||||
try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
|
||||
byte[] b = IOUtils.readAllBytes(in);
|
||||
if (len != -1 && b.length != len)
|
||||
throw new EOFException("Expected:" + len + ", read:" + b.length);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected PermissionCollection getPermissions(CodeSource codesource)
|
||||
{
|
||||
PermissionCollection perms = super.getPermissions(codesource);
|
||||
perms.add(new AllPermission());
|
||||
return perms;
|
||||
}
|
||||
|
||||
private static Class<?> getTrampolineClass() {
|
||||
try {
|
||||
return Class.forName(TRAMPOLINE, true, new MethodUtil());
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
346
jdkSrc/jdk8/sun/reflect/misc/ReflectUtil.java
Normal file
346
jdkSrc/jdk8/sun/reflect/misc/ReflectUtil.java
Normal file
@@ -0,0 +1,346 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
|
||||
package sun.reflect.misc;
|
||||
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Arrays;
|
||||
import sun.reflect.Reflection;
|
||||
import sun.security.util.SecurityConstants;
|
||||
|
||||
public final class ReflectUtil {
|
||||
|
||||
private ReflectUtil() {
|
||||
}
|
||||
|
||||
public static Class<?> forName(String name)
|
||||
throws ClassNotFoundException {
|
||||
checkPackageAccess(name);
|
||||
return Class.forName(name);
|
||||
}
|
||||
|
||||
public static Object newInstance(Class<?> cls)
|
||||
throws InstantiationException, IllegalAccessException {
|
||||
checkPackageAccess(cls);
|
||||
return cls.newInstance();
|
||||
}
|
||||
|
||||
/*
|
||||
* Reflection.ensureMemberAccess is overly-restrictive
|
||||
* due to a bug. We awkwardly work around it for now.
|
||||
*/
|
||||
public static void ensureMemberAccess(Class<?> currentClass,
|
||||
Class<?> memberClass,
|
||||
Object target,
|
||||
int modifiers)
|
||||
throws IllegalAccessException
|
||||
{
|
||||
if (target == null && Modifier.isProtected(modifiers)) {
|
||||
int mods = modifiers;
|
||||
mods = mods & (~Modifier.PROTECTED);
|
||||
mods = mods | Modifier.PUBLIC;
|
||||
|
||||
/*
|
||||
* See if we fail because of class modifiers
|
||||
*/
|
||||
Reflection.ensureMemberAccess(currentClass,
|
||||
memberClass,
|
||||
target,
|
||||
mods);
|
||||
try {
|
||||
/*
|
||||
* We're still here so class access was ok.
|
||||
* Now try with default field access.
|
||||
*/
|
||||
mods = mods & (~Modifier.PUBLIC);
|
||||
Reflection.ensureMemberAccess(currentClass,
|
||||
memberClass,
|
||||
target,
|
||||
mods);
|
||||
/*
|
||||
* We're still here so access is ok without
|
||||
* checking for protected.
|
||||
*/
|
||||
return;
|
||||
} catch (IllegalAccessException e) {
|
||||
/*
|
||||
* Access failed but we're 'protected' so
|
||||
* if the test below succeeds then we're ok.
|
||||
*/
|
||||
if (isSubclassOf(currentClass, memberClass)) {
|
||||
return;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Reflection.ensureMemberAccess(currentClass,
|
||||
memberClass,
|
||||
target,
|
||||
modifiers);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isSubclassOf(Class<?> queryClass,
|
||||
Class<?> ofClass)
|
||||
{
|
||||
while (queryClass != null) {
|
||||
if (queryClass == ofClass) {
|
||||
return true;
|
||||
}
|
||||
queryClass = queryClass.getSuperclass();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a conservative approximation of member access check. Use this if
|
||||
* you don't have an actual 'userland' caller Class/ClassLoader available.
|
||||
* This might be more restrictive than a precise member access check where
|
||||
* you have a caller, but should never allow a member access that is
|
||||
* forbidden.
|
||||
*
|
||||
* @param m the {@code Member} about to be accessed
|
||||
*/
|
||||
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
|
||||
final SecurityManager sm = System.getSecurityManager();
|
||||
if (sm == null)
|
||||
return;
|
||||
|
||||
// Check for package access on the declaring class.
|
||||
//
|
||||
// In addition, unless the member and the declaring class are both
|
||||
// public check for access declared member permissions.
|
||||
//
|
||||
// This is done regardless of ClassLoader relations between the {@code
|
||||
// Member m} and any potential caller.
|
||||
|
||||
final Class<?> declaringClass = m.getDeclaringClass();
|
||||
|
||||
checkPackageAccess(declaringClass);
|
||||
|
||||
if (Modifier.isPublic(m.getModifiers()) &&
|
||||
Modifier.isPublic(declaringClass.getModifiers()))
|
||||
return;
|
||||
|
||||
// Check for declared member access.
|
||||
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks package access on the given class.
|
||||
*
|
||||
* If it is a {@link Proxy#isProxyClass(java.lang.Class)} that implements
|
||||
* a non-public interface (i.e. may be in a non-restricted package),
|
||||
* also check the package access on the proxy interfaces.
|
||||
*/
|
||||
public static void checkPackageAccess(Class<?> clazz) {
|
||||
checkPackageAccess(clazz.getName());
|
||||
if (isNonPublicProxyClass(clazz)) {
|
||||
checkProxyPackageAccess(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks package access on the given classname.
|
||||
* This method is typically called when the Class instance is not
|
||||
* available and the caller attempts to load a class on behalf
|
||||
* the true caller (application).
|
||||
*/
|
||||
public static void checkPackageAccess(String name) {
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
if (s != null) {
|
||||
String cname = name.replace('/', '.');
|
||||
if (cname.startsWith("[")) {
|
||||
int b = cname.lastIndexOf('[') + 2;
|
||||
if (b > 1 && b < cname.length()) {
|
||||
cname = cname.substring(b);
|
||||
}
|
||||
}
|
||||
int i = cname.lastIndexOf('.');
|
||||
if (i != -1) {
|
||||
s.checkPackageAccess(cname.substring(0, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPackageAccessible(Class<?> clazz) {
|
||||
try {
|
||||
checkPackageAccess(clazz);
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns true if p is an ancestor of cl i.e. class loader 'p' can
|
||||
// be found in the cl's delegation chain
|
||||
private static boolean isAncestor(ClassLoader p, ClassLoader cl) {
|
||||
ClassLoader acl = cl;
|
||||
do {
|
||||
acl = acl.getParent();
|
||||
if (p == acl) {
|
||||
return true;
|
||||
}
|
||||
} while (acl != null);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if package access check is needed for reflective
|
||||
* access from a class loader 'from' to classes or members in
|
||||
* a class defined by class loader 'to'. This method returns true
|
||||
* if 'from' is not the same as or an ancestor of 'to'. All code
|
||||
* in a system domain are granted with all permission and so this
|
||||
* method returns false if 'from' class loader is a class loader
|
||||
* loading system classes. On the other hand, if a class loader
|
||||
* attempts to access system domain classes, it requires package
|
||||
* access check and this method will return true.
|
||||
*/
|
||||
public static boolean needsPackageAccessCheck(ClassLoader from, ClassLoader to) {
|
||||
if (from == null || from == to)
|
||||
return false;
|
||||
|
||||
if (to == null)
|
||||
return true;
|
||||
|
||||
return !isAncestor(from, to);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check package access on the proxy interfaces that the given proxy class
|
||||
* implements.
|
||||
*
|
||||
* @param clazz Proxy class object
|
||||
*/
|
||||
public static void checkProxyPackageAccess(Class<?> clazz) {
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
if (s != null) {
|
||||
// check proxy interfaces if the given class is a proxy class
|
||||
if (Proxy.isProxyClass(clazz)) {
|
||||
for (Class<?> intf : clazz.getInterfaces()) {
|
||||
checkPackageAccess(intf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Access check on the interfaces that a proxy class implements and throw
|
||||
* {@code SecurityException} if it accesses a restricted package from
|
||||
* the caller's class loader.
|
||||
*
|
||||
* @param ccl the caller's class loader
|
||||
* @param interfaces the list of interfaces that a proxy class implements
|
||||
*/
|
||||
public static void checkProxyPackageAccess(ClassLoader ccl,
|
||||
Class<?>... interfaces)
|
||||
{
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null) {
|
||||
for (Class<?> intf : interfaces) {
|
||||
ClassLoader cl = intf.getClassLoader();
|
||||
if (needsPackageAccessCheck(ccl, cl)) {
|
||||
checkPackageAccess(intf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note that bytecode instrumentation tools may exclude 'sun.*'
|
||||
// classes but not generated proxy classes and so keep it in com.sun.*
|
||||
public static final String PROXY_PACKAGE = "com.sun.proxy";
|
||||
|
||||
/**
|
||||
* Test if the given class is a proxy class that implements
|
||||
* non-public interface. Such proxy class may be in a non-restricted
|
||||
* package that bypasses checkPackageAccess.
|
||||
*/
|
||||
public static boolean isNonPublicProxyClass(Class<?> cls) {
|
||||
String name = cls.getName();
|
||||
int i = name.lastIndexOf('.');
|
||||
String pkg = (i != -1) ? name.substring(0, i) : "";
|
||||
return Proxy.isProxyClass(cls) && !pkg.equals(PROXY_PACKAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given method is a method declared in the proxy interface
|
||||
* implemented by the given proxy instance.
|
||||
*
|
||||
* @param proxy a proxy instance
|
||||
* @param method an interface method dispatched to a InvocationHandler
|
||||
*
|
||||
* @throws IllegalArgumentException if the given proxy or method is invalid.
|
||||
*/
|
||||
public static void checkProxyMethod(Object proxy, Method method) {
|
||||
// check if it is a valid proxy instance
|
||||
if (proxy == null || !Proxy.isProxyClass(proxy.getClass())) {
|
||||
throw new IllegalArgumentException("Not a Proxy instance");
|
||||
}
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
throw new IllegalArgumentException("Can't handle static method");
|
||||
}
|
||||
|
||||
Class<?> c = method.getDeclaringClass();
|
||||
if (c == Object.class) {
|
||||
String name = method.getName();
|
||||
if (name.equals("hashCode") || name.equals("equals") || name.equals("toString")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (isSuperInterface(proxy.getClass(), c)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// disallow any method not declared in one of the proxy intefaces
|
||||
throw new IllegalArgumentException("Can't handle: " + method);
|
||||
}
|
||||
|
||||
private static boolean isSuperInterface(Class<?> c, Class<?> intf) {
|
||||
for (Class<?> i : c.getInterfaces()) {
|
||||
if (i == intf) {
|
||||
return true;
|
||||
}
|
||||
if (isSuperInterface(i, intf)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if {@code Class cls} is a VM-anonymous class
|
||||
* as defined by {@link sun.misc.Unsafe#defineAnonymousClass}
|
||||
* (not to be confused with a Java Language anonymous inner class).
|
||||
*/
|
||||
public static boolean isVMAnonymousClass(Class<?> cls) {
|
||||
return cls.getName().indexOf("/") > -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user