public class RMIClassLoader extends Object
RMIClassLoader
包括使用RMI支持动态类加载的静态方法。
包括用于从网络位置(一个或多个URL)加载类的方法,并获取远程方应由其加载现有类的位置。
这些方法由RMI运行时使用,当编组和解组合包含在远程方法调用的参数和返回值中时,它们也可以由应用程序直接调用,以模仿RMI的动态类加载行为。
执行以下静态方法
loadClass(URL,String)
loadClass(String,String)
loadClass(String,String,ClassLoader)
loadProxyClass(String,String[],ClassLoader)
getClassLoader(String)
getClassAnnotation(Class)
RMIClassLoaderSpi
的实例提供,这些方法的服务提供者接口。
当调用其中一种方法时,其行为是委托给服务提供者实例上的相应方法。
每个方法委托给提供者实例的详细信息,请参见每个特定方法的文档。
服务提供商实例选择如下:
java.rmi.server.RMIClassLoaderSpi
,那么如果其值等于字符串"default"
,则提供者实例将是通过调用getDefaultProviderInstance()
方法返回的值,对于任何其他值,如果使用属性值命名的类可以由系统类加载器加载(参见ClassLoader.getSystemClassLoader()
),该类可以分配给RMIClassLoaderSpi
并具有公共无参数构造函数,那么将调用该构造函数来创建提供者实例。 如果属性被定义,但这些条件中的任何其他条件都不为真,那么未指定的Error
将被抛出到尝试使用RMIClassLoader
代码中,表示无法获取提供者实例。 META-INF/services/java.rmi.server.RMIClassLoaderSpi
的资源对于系统类加载器是可见的,那么该资源的内容将被解释为提供者配置文件,并且该文件中指定的第一个类名被用作提供者类名称。 如果具有该名称的类可以由系统类加载器加载,并且该类可以分配给RMIClassLoaderSpi
并具有公共无参构造函数,则将调用该构造函数来创建提供者实例。 如果找到资源,但提供者无法按照描述进行实例化,那么未指定的Error
将被Error
到尝试使用RMIClassLoader
代码,指示无法获取提供程序实例。 getDefaultProviderInstance()
方法返回的值。 RMIClassLoaderSpi
Modifier and Type | Method and Description |
---|---|
static String |
getClassAnnotation(类<?> cl)
返回在编组给定类的对象时,RMI将用于注释类描述符的注释字符串(表示类定义的位置)。
|
static ClassLoader |
getClassLoader(String codebase)
返回从给定的代码库URL路径加载类的类加载器。
|
static RMIClassLoaderSpi |
getDefaultProviderInstance()
返回服务提供商接口 RMIClassLoaderSpi 的默认提供程序的规范实例 。
|
static Object |
getSecurityContext(ClassLoader loader)
已弃用
没有替换。
从Java 2平台v1.2开始,RMI不再使用此方法来获取类加载器的安全上下文。
|
static 类<?> |
loadClass(String name)
已弃用
取代
loadClass(String,String) 法
|
static 类<?> |
loadClass(String codebase, String name)
从代码库URL路径加载一个类。
|
static 类<?> |
loadClass(String codebase, String name, ClassLoader defaultLoader)
从代码库URL路径加载类,可选地使用提供的加载程序。
|
static 类<?> |
loadClass(URL codebase, String name)
从代码库URL加载一个类。
|
static 类<?> |
loadProxyClass(String codebase, String[] interfaces, ClassLoader defaultLoader)
加载动态代理类(参见 Proxy ),它从代码库URL路径中实现了一组具有给定名称的接口。
|
@Deprecated public static 类<?> loadClass(String name) throws MalformedURLException, ClassNotFoundException
loadClass(String,String)
法
name
。
该方法代表loadClass(String,String)
,作为第一个参数传递null
,第8个参数为name
。
name
- 要加载的类的名称
类
对象
MalformedURLException
- 如果用于加载类的提供者特定URL无效
ClassNotFoundException
- 如果在代码库位置找不到类的定义
loadClass(String,String)
public static 类<?> loadClass(URL codebase, String name) throws MalformedURLException, ClassNotFoundException
codebase
是null
,那么这个方法的行为与loadClass(String,String)
一样 ,具有null
codebase
和给定的类名。
此方法委托给RMIClassLoaderSpi.loadClass(String,String,ClassLoader)
提供程序实例的方法,并传递调用的结果URL.toString()
在给定的URL(或null
如果codebase
为null)作为第一个参数, name
作为第二个参数,和null
作为第三个参数。
codebase
- 加载类的URL,或
null
name
- 要加载的类的名称
类
对象
MalformedURLException
- 如果
codebase
是
null
,用于加载类的提供者特定的URL是无效的
ClassNotFoundException
- 如果在指定的URL处找不到类的定义
public static 类<?> loadClass(String codebase, String name) throws MalformedURLException, ClassNotFoundException
该方法委托给提供者实例的RMIClassLoaderSpi.loadClass(String,String,ClassLoader)
方法,以codebase
作为第一个参数, name
作为第二个参数, null
作为第三个参数。
codebase
- 加载类的URL列表(由空格分隔)或
null
name
- 要加载的类的名称
类
加载类的
类
对象
MalformedURLException
- 如果
codebase
不是
null
并且包含无效的URL,或者如果
codebase
是
null
,并且用于加载类的提供者特定URL无效
ClassNotFoundException
- 如果在指定位置找不到类的定义
public static 类<?> loadClass(String codebase, String name, ClassLoader defaultLoader) throws MalformedURLException, ClassNotFoundException
defaultLoader
(如果指定)来解析命名类。
此方法委托给RMIClassLoaderSpi.loadClass(String,String,ClassLoader)
提供程序实例的方法,传递codebase
作为第一个参数, name
作为第二个参数,和defaultLoader
作为第三个参数。
codebase
- 加载类的URL列表(由空格分隔)或
null
name
- 要加载的类的名称
defaultLoader
- 要使用的附加上下文类加载器,或
null
类
加载类的
类
对象
MalformedURLException
- 如果
codebase
不是
null
并且包含无效的URL,或者如果
codebase
是
null
,并且用于加载类的提供者特定的URL无效
ClassNotFoundException
- 如果在指定位置找不到类的定义
public static 类<?> loadProxyClass(String codebase, String[] interfaces, ClassLoader defaultLoader) throws ClassNotFoundException, MalformedURLException
Proxy
),它从代码库URL路径中实现了一组具有给定名称的接口。
这些接口将被解析为类似于使用给定的codebase
通过loadClass(String,String)
方法加载的类。
该方法委托给提供者实例的RMIClassLoaderSpi.loadProxyClass(String,String[],ClassLoader)
方法,以codebase
作为第一个参数,以interfaces
作为第二个参数,以defaultLoader
作为第三个参数。
codebase
- 加载类的URL列表(空格分隔),或
null
interfaces
- 要实现的代理类的接口的名称
defaultLoader
- 附加的上下文类加载器使用,或
null
MalformedURLException
- 如果
codebase
不是
null
并且包含无效的URL,或者如果
codebase
是
null
,并且用于加载类的提供者特定URL无效
ClassNotFoundException
-如果指定的接口之一的定义无法在指定位置找到,或者如果动态代理类的创建失败(例如,如果
Proxy.getProxyClass(ClassLoader,Class[])
将抛出一个
IllegalArgumentException
对于给定的接口列表)
public static ClassLoader getClassLoader(String codebase) throws MalformedURLException, SecurityException
返回的类加载器是loadClass(String,String)
方法用于为相同的codebase
参数加载类的类加载器。
该方法委托给提供者实例的RMIClassLoaderSpi.getClassLoader(String)
方法,传递codebase
作为参数。
如果有安全管理员,其checkPermission
方法将被调用RuntimePermission("getClassLoader")
权限; 这可能会导致一个SecurityException
。 该方法的提供者实现还可以执行进一步的安全检查,以验证呼叫上下文是否具有连接到代码库URL路径中的所有URL的权限。
codebase
- 返回的类加载器从其加载类的URL(空格分隔)列表,或
null
MalformedURLException
- 如果
codebase
是非
null
并且包含无效的URL,或者如果
codebase
是
null
,并且用于标识类加载器的提供者特定的URL是无效的
SecurityException
- 如果有一个安全管理器,并且其
checkPermission
方法的调用失败,或者如果调用者没有权限连接到代码库URL路径中的所有URL
public static String getClassAnnotation(类<?> cl)
该方法委托给提供者实例的RMIClassLoaderSpi.getClassAnnotation(Class)
方法,传递cl
作为参数。
cl
- 获取注释的类
null
NullPointerException
- 如果
cl
是
null
public static RMIClassLoaderSpi getDefaultProviderInstance()
RMIClassLoaderSpi
的默认提供程序的规范实例 。
如果系统属性java.rmi.server.RMIClassLoaderSpi
未定义,那么RMIClassLoader
静态方法
loadClass(URL,String)
loadClass(String,String)
loadClass(String,String,ClassLoader)
loadProxyClass(String,String[],ClassLoader)
getClassLoader(String)
getClassAnnotation(Class)
如果有安全管理员,其checkPermission
方法将被调用RuntimePermission("setFactory")
权限; 这可能会导致一个SecurityException
。
默认服务提供者实例实现RMIClassLoaderSpi
如下:
The
getClassAnnotation
method returns aString
representing the codebase URL path that a remote party should use to download the definition for the specified class. The format of the returned string is a path of URLs separated by spaces. The codebase string returned depends on the defining class loader of the specified class:
If the class loader is the system class loader (see
ClassLoader.getSystemClassLoader()
), a parent of the system class loader such as the loader used for installed extensions, or the bootstrap class loader (which may be represented bynull
), then the value of thejava.rmi.server.codebase
property (or possibly an earlier cached value) is returned, ornull
is returned if that property is not set.Otherwise, if the class loader is an instance of
URLClassLoader
, then the returned string is a space-separated list of the external forms of the URLs returned by invoking thegetURLs
methods of the loader. If theURLClassLoader
was created by this provider to service an invocation of itsloadClass
orloadProxyClass
methods, then no permissions are required to get the associated codebase string. If it is an arbitrary otherURLClassLoader
instance, then if there is a security manager, itscheckPermission
method will be invoked once for each URL returned by thegetURLs
method, with the permission returned by invokingopenConnection().getPermission()
on each URL; if any of those invocations throws aSecurityException
or anIOException
, then the value of thejava.rmi.server.codebase
property (or possibly an earlier cached value) is returned, ornull
is returned if that property is not set.Finally, if the class loader is not an instance of
URLClassLoader
, then the value of thejava.rmi.server.codebase
property (or possibly an earlier cached value) is returned, ornull
is returned if that property is not set.For the implementations of the methods described below, which all take a
String
parameter namedcodebase
that is a space-separated list of URLs, each invocation has an associated codebase loader that is identified using thecodebase
argument in conjunction with the current thread's context class loader (seeThread.getContextClassLoader()
). When there is a security manager, this provider maintains an internal table of class loader instances (which are at least instances ofURLClassLoader
) keyed by the pair of their parent class loader and their codebase URL path (an ordered list of URLs). If thecodebase
argument isnull
, the codebase URL path is the value of the system propertyjava.rmi.server.codebase
or possibly an earlier cached value. For a given codebase URL path passed as thecodebase
argument to an invocation of one of the below methods in a given context, the codebase loader is the loader in the table with the specified codebase URL path and the current thread's context class loader as its parent. If no such loader exists, then one is created and added to the table. The table does not maintain strong references to its contained loaders, in order to allow them and their defined classes to be garbage collected when not otherwise reachable. In order to prevent arbitrary untrusted code from being implicitly loaded into a virtual machine with no security manager, if there is no security manager set, the codebase loader is just the current thread's context class loader (the supplied codebase URL path is ignored, so remote class loading is disabled).The
getClassLoader
method returns the codebase loader for the specified codebase URL path. If there is a security manager, then if the calling context does not have permission to connect to all of the URLs in the codebase URL path, aSecurityException
will be thrown.The
loadClass
method attempts to load the class with the specified name as follows:If thedefaultLoader
argument is non-null
, it first attempts to load the class with the specifiedname
using thedefaultLoader
, such as by evaluatingClass.forName(name, false, defaultLoader)If the class is successfully loaded from thedefaultLoader
, that class is returned. If an exception other thanClassNotFoundException
is thrown, that exception is thrown to the caller.Next, the
loadClass
method attempts to load the class with the specifiedname
using the codebase loader for the specified codebase URL path. If there is a security manager, then the calling context must have permission to connect to all of the URLs in the codebase URL path; otherwise, the current thread's context class loader will be used instead of the codebase loader.The
loadProxyClass
method attempts to return a dynamic proxy class with the named interface as follows:If the
defaultLoader
argument is non-null
and all of the named interfaces can be resolved through that loader, then,
- if all of the resolved interfaces are
public
, then it first attempts to obtain a dynamic proxy class (usingProxy.getProxyClass
) for the resolved interfaces defined in the codebase loader; if that attempt throws anIllegalArgumentException
, it then attempts to obtain a dynamic proxy class for the resolved interfaces defined in thedefaultLoader
. If both attempts throwIllegalArgumentException
, then this method throws aClassNotFoundException
. If any other exception is thrown, that exception is thrown to the caller.- if all of the non-
public
resolved interfaces are defined in the same class loader, then it attempts to obtain a dynamic proxy class for the resolved interfaces defined in that loader.- otherwise, a
LinkageError
is thrown (because a class that implements all of the specified interfaces cannot be defined in any loader).Otherwise, if all of the named interfaces can be resolved through the codebase loader, then,
- if all of the resolved interfaces are
public
, then it attempts to obtain a dynamic proxy class for the resolved interfaces in the codebase loader. If the attempt throws anIllegalArgumentException
, then this method throws aClassNotFoundException
.- if all of the non-
public
resolved interfaces are defined in the same class loader, then it attempts to obtain a dynamic proxy class for the resolved interfaces defined in that loader.- otherwise, a
LinkageError
is thrown (because a class that implements all of the specified interfaces cannot be defined in any loader).Otherwise, a
ClassNotFoundException
is thrown for one of the named interfaces that could not be resolved.
SecurityException
- 如果有安全管理器,并且调用其
checkPermission
方法失败
@Deprecated public static Object getSecurityContext(ClassLoader loader)
loader
- 从中获取安全上下文的类加载器
SecurityManager.getSecurityContext()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.