Guess you just have to settle with reflection:
import java.lang.reflect.*;
interface I {}
class A implements I {}
class B implements I {}
public class Foo {
public void f(A a) { System.out.println("from A"); }
public void f(B b) { System.out.println("from B"); }
static public void main(String[]args ) throws InvocationTargetException
, NoSuchMethodException, IllegalAccessException
{
I[] elements = new I[] {new A(), new B(), new B(), new A()};
Foo o = new Foo();
for (I element : elements) {
o.multiDispatch(element);
}
}
void multiDispatch(I x) throws NoSuchMethodException
, InvocationTargetException, IllegalAccessException
{
Class cls = this.getClass();
Class[] parameterTypes = { x.getClass() };
Object[] arguments = { x };
Method fMethod = cls.getMethod("f", parameterTypes);
fMethod.invoke(this,arguments);
}
}
Output:
from A
from B
from B
from A