加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

RMI java反射

发布时间:2020-12-15 02:21:57 所属栏目:Java 来源:网络整理
导读:我正在使用RMI来允许通过MATLAB访问我的 Java应用程序,MATLAB在另一个JVM中运行. MATLAB有一个很好的接口来打印Java对象的方法.但它失败了RMI,因为它获得的对象是代理. 所以我想添加自己的方法来提取/打印远程接口的功能(RMI显然不能直接访问导出的远程接口
我正在使用RMI来允许通过MATLAB访问我的 Java应用程序,MATLAB在另一个JVM中运行. MATLAB有一个很好的接口来打印Java对象的方法.但它失败了RMI,因为它获得的对象是代理.

所以我想添加自己的方法来提取/打印远程接口的功能(RMI显然不能直接访问导出的远程接口中不可用的方法).

如何在RMI连接的客户端或服务器端使用反射执行此操作?我没有太多使用反射的经验.用例如下.

编辑:我得到最多的是给定一个任意对象X(包括X是一个RMI代理),我如何使用反射来获取该对象实现的接口?

java类:

/** client-side remote describer */
class RemoteDescriber
{
    RemoteDescription describe(Remote remote) { ... }
}

/* representation of remote interfaces implemented by an object */
class RemoteDescription implements Serializable
{
    /* string representation of remote interfaces implemented by an object */
    @Override public String toString() { ... }

    /* maybe there are other methods permitting object-model-style navigation
     * of a remote interface
     */
}

interface FooRemote extends Remote
{
    /* some sample methods */
    public int getValue() throws RemoteException;
    public void setValue(int x) throws RemoteException;
    public void doSomethingSpecial() throws RemoteException;
    /* other methods omitted */        

    /** server-side */
    public RemoteDescription describe() throws RemoteException;        
}

和MATLAB中的客户端会话示例

x = ...;     % get something that implements FooRemote
describer = com.example.RemoteDescriber;
% describer is a client-side Java object

description1 = describer.describe(x)

%%% prints a description of the FooRemote interface 
%%% obtained by the client-side RemoteDescriber

description2 = x.describe()

%%% prints a description of the FooRemote interface 
%%% obtained on the server-side by x itself,and marshalled
%%% to the client

解决方法

客户端上的对象是代理:它们被称为存根.要从中获取接口,您应该编写类似这样的代码,其中o是您的对象:

Class c = o.getClass();
Class[] theInterfaces = c.getInterfaces();
for (int i = 0; i < theInterfaces.length; i++) {
   String interfaceName = theInterfaces[i].getName();
   System.out.println(interfaceName);
}

存根是自动生成的:因此你不应该在它们中实现某些东西,但你可以在远程接口中实现一个方法getInformation();每个服务器对象都应该实现它并返回一个包含服务器对象的所有信息的字符串.此方法通过从此对象反射获取信息来生成字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读