RMI + java反射 [英] RMI + java reflection
问题描述
我正在使用RMI来允许通过MATLAB访问我的Java应用程序,MATLAB在另一个JVM中运行。 MATLAB有一个很好的接口来打印Java对象的方法。但它失败了RMI,因为它得到的对象是代理。
I'm using RMI to allow access to my Java application via MATLAB, which runs in another JVM. MATLAB has a nice interface to print the methods of a Java object. But it fails with RMI, because the object it gets is a proxy.
所以我想添加自己的方法来提取/打印远程界面(RMI显然无法直接访问导出的远程接口中不可用的方法)。
So I would like to add my own method to extract/print the capability of a remote interface (RMI obviously can't directly access methods not available in exported remote interfaces).
我怎样才能在客户端上使用反射执行此操作RMI连接的结束,还是在服务器端?我没有太多使用反射的经验。下面的使用案例。
How could I do this with reflection, either on the client end of the RMI connection, or on the server end? I don't have much experience using reflection. Use case below.
编辑我最常遇到的是给定一个任意对象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中的客户会话示例
and sample client session in 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
是您的对象:
The objects on your client are proxies: they are called stubs. To get the interfaces from it you should code something like this, where o
is your object:
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()
;每个服务器对象都应该实现它并返回一个包含服务器对象的所有信息的字符串。此方法通过从此
对象反射获取信息来生成字符串。
Stubs are auto-generated: therefore you should not implement something into them, but you could implement a method getInformation()
in your remote interfaces; every server object should implement this and return a string which contains all information of the server object. This method generates the string by getting information via reflection from the this
object.
这篇关于RMI + java反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!