RMI java reflection

I am using RMI to allow access to my java application through MATLAB, which runs in another JVM MATLAB has a good interface to print Java objects But it failed RMI because the object it got was a proxy

So I want to add my own method to extract / print the function of the remote interface (RMI obviously can't directly access the unavailable methods in the exported remote interface)

How do I do this using reflection on the client or server side of an RMI connection? I don't have much experience with reflection The use cases are as follows

Edit: what I get most is that given an arbitrary object x (including X is an RMI proxy), how can I use reflection to obtain the interface implemented by the object?

Java class:

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

Client session examples in and 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

Solution

The objects on the client are proxies: they are called stubs To get an interface from it, you should write code 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);
}

Stubs are automatically generated: so you shouldn't implement something in them, but you can implement a method getinformation () in the remote interface; Each server object should implement it and return a string containing all the information of the server object This method generates a string by getting information from this object reflection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>