RMI reflection + java

I use RMI to access my Java application through MATLAB, which works in another JVM. MATLAB has a good interface for printing Java object methods. But it does not work with RMI, because the object that it receives is a proxy server.

Therefore, I would like to add my own method for extracting / printing the capabilities of the remote interface (RMI, obviously, cannot directly access methods not available in the exported remote interfaces).

How could I do this with reflection, either on the client end of the RMI connection or on the server? I do not have much experience using reflection. Use the example below.

edit: the fact that I am getting the most stuck is given by an arbitrary X object (including where X is the RMI proxy), how can I use reflection to get the interfaces implemented through this object?

java classes:

/** 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; } 

and example 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 
+4
source share
1 answer

The objects on your client are proxies: they are called stubs. To get the interfaces from it, you should encode 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); } 

Letters are automatically generated: therefore, you should not embed something in them, but you can implement the getInformation() method in your remote interfaces; each server object must implement this and return a string containing all the information about the server object. This method generates a string, receiving information through reflection from the this object.

+1
source

All Articles