按标题,如果你要检查,如果一个类的接口:
ClassNode node = // However you wish to load the class
if ((node.access & Opcodes.ACC_INTERFACE) != 0){
// is interface
} else {
// is not an interface
}
在您的文章,你陈述你想找到的java.sql.Connection
儿童/实现。
您遇到的问题是这样的:
java.sql.Connection -> com.mysql.jdbc.Connection -> ConnectionImpl
ConnectionImpl
不直接实现java.sql.Connection
所以它不会被检测为孩子/实施。对于这样的问题,你需要做的是旅行类层次结构。如果我处于你的情况,我会加载一个ClassNode <String, ClassNode>
的地图,其中字符串是ClassNode的名称。然后,我会使用递归方法来检查给定的类是否是预期的类型。所以如果你发送一个ClassNode,它会自己调用它的父节点,然后是接口。最终,如果最初给定的节点是正确的类型,那么最终java.sql.Connection
接口将被传递并找到。此外,如果您想跳过重复行为,您可以将每个ClassNode的结果存储在地图中,这样就不必一遍又一遍地检查整个层次结构。
编辑:有点儿像这样
public static boolean isMatch(ClassNode cn, Map<String,ClassNode> nodes, String target){
if (cn.name.equals(target)) return true;
else{
if (nodes.containsKey(cn.superName) && isMatch(nodes.get(cn.superName),nodes,target)) return true;
for (String interf : cn.interfaces){
if (nodes.containsKey(interf) && isMatch(nodes.get(interf),nodes,target)) return true;
}
}
return false;
}
最后,我认为我被洗脑了。即使我可以做这样的事情,也没有什么可以确保界面事实上在课前加载。例如,从我所看到的,ConnectionImpl在mysql.jdbc.Connection之前加载,因此我无法知道ConnectionImpl实际上实现了java.sql.Connection。 – ticktock