开发者

Using Javassist, how do you determine if a CtField is a collection?

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 reflectio开发者_如何学JAVAn:

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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜