Mike S.
Mike S.

Reputation: 2111

Java RMI - Casting from interface/$Proxy to original class?

I have some server and client using Java RMI. For simplifying, on server exists method returning Task (iTask) and receiving iTask. For example, server:

interface iTask extends Remote{
    void publicMethod();
}

class Task extends UnicastRemoteObject implements iTask
{
    public void publicMethod(){...} //interface
    void packageMethod(){...} //not interface
}

And methods in some (doesn't matter in what) class:

iTask getTask(){
    return new Task();
}

void doSomethingSecret(iTask task){
    Task needthis = (Task)task; //BOOM! ClassCastException: $Proxy9 cannot be cast to nextQuest.server.Task
    needthis.packageMethod(); // I need this...
}

In client, I do just

iTask tsk = abc.getTask()
def.doSomethingSecret(tsk);

abc and def are remote objects!

Is there some way, how to call "packageMethod"? Thank you very much, Mike S. (cz)

Upvotes: 1

Views: 781

Answers (1)

user207421
user207421

Reputation: 310874

You can't. You have to cast it to the remote interface. It isn't the original class. It is a proxy. If there is a method you want to call on it, that method must be defined in the remote interface.

Upvotes: 1

Related Questions