Note. Do not use this, see comments.
I have the same requirement when displaying the IP address of clients in the GUI. After debugging and analyzing the types of the remote object and its sub-objects, I came up with the following code:
public static String getIpAddress(Object proxy) throws IllegalArgumentException, RemoteException, NotExpectedTypeException { InvocationHandler h = Proxy.getInvocationHandler(proxy); if (h instanceof RemoteObjectInvocationHandler) { RemoteRef remoteRef = ((RemoteObjectInvocationHandler) h).getRef(); if (remoteRef instanceof UnicastRef) { Endpoint ep = ((UnicastRef) remoteRef).getLiveRef().getChannel().getEndpoint(); if (ep instanceof TCPEndpoint) { return ((TCPEndpoint) ep).getHost(); } throw new NotExpectedTypeException(ep.getClass().getSimpleName(), "TCPEndpoint"); } throw new NotExpectedTypeException(remoteRef.getClass().getSimpleName(), "UnicastRef"); } throw new NotExpectedTypeException(h.getClass().getSimpleName(), "RemoteObjectInvocationHandler"); }
where NotExpectedTypeException is a self- NotExpectedTypeException exception.
I'm not sure how reliable the method is, since it contains a lot of instanceof s. However, I successfully tested it on a local network where I used TCP connections.
source share