public class RMIClassLoader extends Object
RMIClassLoader
包括静态方法来支持动态类加载与RMI。包括从网络位置(一个或多个链接)加载类的方法,并获得一个现有的类应该由远程方加载的位置。这些方法都是通过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
将抛出,试图用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
作为第一个参数,name
作为第二个参数。
name
-类的名称来加载
类
对象
MalformedURLException
如果用来加载类提供程序特定的URL是无效的
ClassNotFoundException
-如果一个类的定义不能在代码库的位置找到
loadClass(String,String)
public static 类<?> loadClass(URL codebase, String name) throws MalformedURLException, ClassNotFoundException
codebase
是
null
,本方法将表现为一个
null
codebase
和给定的类的名字
loadClass(String,String)
相同。
这种方法代表的RMIClassLoaderSpi.loadClass(String,String,ClassLoader)
提供程序实例的方法,对给定的URL通过调用URL.toString()
结果(或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
,如果指定,在试图解决从编码的URL路径的类。
这种方法代表的提供程序的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
null
cl
NullPointerException
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.