views:

70

answers:

1

I'm using Javassist in a code generator I'm writing. It's pretty nice, but I've run into a problem.

When I'm looking at the CtField in question, I want to determine if it's a collection or not. This is pretty trivial with normal reflection:

Collection.class.isAssignableFrom(...)

But I haven't quite figured out how to get that same effect from Javassist. This is pretty basic stuff, so I must just be missing it.

+1  A: 

I don't think Javassist has anything similar, but walking the interface hierarchy is fairly easy.

public boolean isImplementor(final CtClass interfaceClass,
                             final CtClass clazz) throws NotFoundException {
    return isImplementor(interfaceClass, clazz.getInterfaces())
        || isImplementor(interfaceClass, clazz.getSuperclass());
}

private boolean isImplementor(final CtClass interfaceClass,
                              final CtClass[] interfaces) throws NotFoundException {
    for (final CtClass intf : interfaces) {
        if (intf == interfaceClass || isImplementor(interfaceClass, 
                                                    intf.getInterfaces())) {
            return true;
        }
    }
    return false;
}
Michael Barker