インタフェースがjava.lang.Runnableのを拡張するように変更することはできませんので、オプションはあなたのA1、A2へのRunnableのインスタンスそのデリゲートを構築するjava.lang.reflect.Proxyのを使用することです。 ..インタフェース。
これは簡単なことではありませんが、この例をjava.lang.reflect.Proxyで見てみましょう。このサンプルは、単にメソッド名に基づいてデリゲートオブジェクトに委譲します。
public class ProxyTest
{
public static void main(String... args)
{
List<?> instances = Arrays.asList(new A1());
List<ARunnable> runnableInstances = new ArrayList<ARunnable>(instances.size());
for (Object instance : instances)
{
ARunnable runnableInstance = (ARunnable)Proxy.newProxyInstance(
ARunnable.class.getClassLoader(),
new Class<?>[] {ARunnable.class},
new RunnableWrapper(instance));
runnableInstances.add(runnableInstance);
}
//Now we have a list of ARunnables!
//Use them for something
for (ARunnable runnableInstance : runnableInstances)
{
runnableInstance.run();
}
}
private static class RunnableWrapper implements InvocationHandler
{
private final Object instance;
public RunnableWrapper(Object instance)
{
this.instance = instance;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
//Ensure that your methods match exactly or you'll get NoSuchMethodExceptions here
Method delegateMethod = instance.getClass().getMethod(method.getName(), method.getParameterTypes());
return(delegateMethod.invoke(instance, args));
}
}
public static class A1
{
public void run()
{
System.out.println("Something");
}
}
public static interface ARunnable
{
public void run();
}
}
また、私はあなたが
List<ARunnable> runnables = (List<ARunnable>)getARunnables();
ラインあなたが無視するべきではないと型の安全性警告を修正することをお勧めします。そのオブジェクトのリストには実際にはARunnablesが含まれていないため、ClassCastExceptionが存在します。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Main {
interface Interface {
public void run();
}
static class Hello /* does't implement Interface */{
public void run() {
System.out.println("Hello, world!!");
}
}
static <T> T dirtyCast(Class<T> intrface, final Object target) {
return intrface.cast(Proxy.newProxyInstance(
intrface.getClassLoader(),
new Class<?>[] { intrface }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
Method targetMethod = target.getClass().getMethod(
method.getName(), method.getParameterTypes());
return targetMethod.invoke(target, args);
}
}));
}
public static void main(String[] args) {
Interface proxy = dirtyCast(Interface.class, new Hello());
proxy.run();
}
}
あなたは引数を渡すか、値を返すか、例外をスローする場合に実現可能なように、このソリューションを考慮しないでください。
インターフェイスとクラスが異なるクラスローダーに存在するため、ClassCastExceptionが発生しています。 – mschonaker
これらの各インタフェースA1->共有の共通インタフェースはありますか?何かのように: インターフェイスA1はARunnableを拡張します。{} –
アミール:いいえ、彼らは –