首页 / JAVA / RMI java反射
RMI java反射
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了RMI java反射,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2192字,纯文字阅读大概需要4分钟。
内容图文
![RMI java反射](/upload/InfoBanner/zyjiaocheng/769/95acaad25e354378b153280d56d3bdf7.jpg)
我正在使用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();每个服务器对象都应该实现它并返回一个包含服务器对象的所有信息的字符串.此方法通过从此对象反射获取信息来生成字符串.
内容总结
以上是互联网集市为您收集整理的RMI java反射全部内容,希望文章能够帮你解决RMI java反射所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。