Menu

#3 Class cast exception in client - Inherited Interface

open
nobody
None
5
2004-09-27
2004-09-27
No

There is a problem when a factory object (f) which
declares a method as returning an instance of interface
(i) returns an instance of an object that inherits the
interface implementation from one of it's superclasses.
The attached code re-creates the problem. If you
comment out the "implements SessionInterface" from
ConcreteSession (Leaving the object to exted
AbstraceSession which itself implements
SessionInterface) then things go a bit wonky. If you
leave it so that both ConcreteSession and
AbstractSession implement SessionInterface then
everything seems to work fine. Found this whilst
working in JDK1.5 and I've got a sneaky suspicion that
it actually relates to a feature of java rather than a
bug per-se, but it's worth noting. In fact the crash
caused me to look at some of my code again and move the
interfaces around a little, so this is no longer a
problem for me.

Kind regards again for a cool project,
Ian.

Discussion

  • Ian Ibbotson

    Ian Ibbotson - 2004-09-27

    Tar file containig some test code

     
  • William Underwood

    Logged In: YES
    user_id=546878

    No, it's an issue with 1.4, and I think with 1.3 as well.

    It's been a while since I looked at the code, but issue is
    that the matching of exported and implemented interfaces was
    broken: it only matches the first implemented interface
    against the exported interfaces, and ignores interfaces
    implemented through superclasses (easily fixed in the
    library, replacing a block of code with an
    aClass.isInstance(anObject) call).

     
  • Joe Yandle

    Joe Yandle - 2006-04-03

    Logged In: YES
    user_id=7412

    I fixed this bug in my local trmi source by adding the
    following function to Naming.java:

    public static Class[] getAllImplementedInterfaces(Object o) {
    Vector<Class> interfaces = new Vector<Class>();
    Class c = o.getClass();

    while (c != null) {
    for (Class i : c.getInterfaces()) {
    interfaces.add(i);
    }
    c = c.getSuperclass();
    }

    Class[] ret = new Class[interfaces.size()];
    interfaces.copyInto(ret);
    return ret;
    }

    Then, call this function instead of
    obj.getClass().getInterfaces() in Naming.java and
    RemoteObjectWrapperImpl.java.

     

Log in to post a comment.

MongoDB Logo MongoDB