I’m using some Java code which produces the following interfaces. This code is non-modifiable.
interface A1 {
public void run();
}
interface A2 {
public void run();
}
...
interface A24 {
public void run();
}
I’m having a class cast error with the following code. How would I dynamically build an adapter to my interface?
interface ARunnable {
public void run();
}
public void someMethod() {
// getARunnables() returns a list of A1, A2, ... A24
List<ARunnable> runnables = (List<ARunnable>)getARunnables();
for (ARunnable a : runnables) {
a.run();
}
}
Since the interfaces can’t be modified to extend java.lang.Runnable, an option would be to use java.lang.reflect.Proxy to build up instances of Runnable that delegate to your A1, A2… interfaces.
It’s not trivial, but take a look at this example using java.lang.reflect.Proxy. The sample simply delegates to a delegate object based on method name.
Also I would recommend you fix the line
That type safety warning you should not ignore. That list of objects does not actually contain ARunnables, hence the ClassCastException.