java - ClassCastException when trying to create Proxy object -
i'm trying create proxy given runnable object using following code:
public class workinvocationhandler implements invocationhandler { public static runnable newproxyinstance(runnable work) { return (runnable)java.lang.reflect.proxy.newproxyinstance( work.getclass().getclassloader(), getinterfaceswithmarker(work), new workinvocationhandler(work)); } private static class[] getinterfaceswithmarker(runnable work) { list allinterfaces = new arraylist(); // add direct interfaces allinterfaces.addall(arrays.aslist(work.getclass().getinterfaces())); // add interfaces of super classes class superclass = work.getclass().getsuperclass(); while (!superclass.equals(object.class)) { allinterfaces.addall(arrays.aslist(superclass.getinterfaces())); superclass = superclass.getclass().getsuperclass(); } // add marker interface allinterfaces.add(iworkproxy.class); return (class [])allinterfaces.toarray(new class[allinterfaces.size()]); } } the proxy should implement interfaces given object implements additional marker interface indicates whether proxy created. since don't know sure given object implements runnable directly traverse on super classes, assume if implements interface implements runnable work don't need traverse on interfaces hierarchy.
however, still classcastexception when trying cast proxy runnable:
java.lang.classcastexception: $proxy24 incompatible java.lang.runnable i'm trying think cause exception. class hierarchy of given object not available.
any ideas ?
update removed useless code.
this not problem, should use set<class<?>> when gather interfaces because can duplicates of same interface in hierarchy.
Comments
Post a Comment