有没有通过标准库或某些已经存在的库来确定Java中两个类/接口之间关系程度的方法?Java对象和类/接口之间的关系程度?
比方说,我有一个对象和类/接口的列表。现在我基本上想知道该列表的一个类,它具有到此对象的最短继承树路径。
我已通过java.lang.reflect包和类别看,但不能真正找到任何可以方便获取这样的信息。这可能已经是另一个图书馆的一部分了?
有没有通过标准库或某些已经存在的库来确定Java中两个类/接口之间关系程度的方法?Java对象和类/接口之间的关系程度?
比方说,我有一个对象和类/接口的列表。现在我基本上想知道该列表的一个类,它具有到此对象的最短继承树路径。
我已通过java.lang.reflect包和类别看,但不能真正找到任何可以方便获取这样的信息。这可能已经是另一个图书馆的一部分了?
我不知道任何准备使用。
我会使用反射来发现的关系。
难的是最短路径。
你必须定义你想要什么:
然后,寻找这些类或接口的顺序,从当前的类,那么它的父类(以及可能实现的接口),等等...
this有点帮助。 不知道如何获得最短路径。
思考会让你得到任何给定类的父类,这样你就可以提取足够的信息来建立自己的继承树,然后你就可以用它来回答你的问题。我想不出任何可以让你更优雅的内置机制。
此代码应该让你关闭。正如其他人所说,你可能会遇到接口问题,尽管继承深度可能很容易相同。您还需要添加一些空的检查等。
在此示例中,FooBar3扩展FooBar2扩展FooBar。
public static void main(String[] args) {
List<Class<?>> l = new ArrayList<Class<?>>() {{
add(FooBar2.class);
add(FooBar.class);
} };
System.out.println(getClosestParent(new FooBar3(), l));
}
public static Class getClosestParent(Object o, List<Class<?>> classes) {
List<Class<?>> related = getRelated(o, classes);
Collections.sort(related, new Comparator<Class<?>>() {
public int compare(Class<?> o1, Class<?> o2) {
if (o1.isAssignableFrom(o2)) {
return -1;
} else if (o2.isAssignableFrom(o1)) {
return 1;
}
return 0;
}
});
return related.get(0);
}
public static List<Class<?>> getRelated(Object o, List<Class<?>> classes) {
List<Class<?>> filtered = new ArrayList<Class<?>>();
for (Class<?> aClass : classes) {
if (aClass.isAssignableFrom(o.getClass())) {
filtered.add(aClass);
}
}
return filtered;
}
我不禁发现这是一个有趣的项目。这是原型代码,可以为您提供所需的信息。这段代码只是试图计算从给定类到另一个类的所有可能的继承路径。您可以使用它来获取从源对象到您感兴趣的所有可能类的所有路径。正如其他评论中提到的,您可能需要打电话询问您的偏好路径是否使用接口,但希望这段代码对你有帮助。
public class InheritenceDepth {
/**
* Obtains a list of all the possible inheritance paths from the given targetClass
* to the specified potentialAncestorClass. If the targetClass does not extend or implement
* the potentialAncestorClass the return list will be empty.
*/
public static List<InheritancePath> classInheritancePaths(Class<?> targetClass, Class<?> potentialAncestorClass){
List<InheritancePath> returnList = new ArrayList<InheritancePath>();
if(potentialAncestorClass.isAssignableFrom(targetClass)){
if(potentialAncestorClass.equals(targetClass)){
returnList.add(new InheritancePath(potentialAncestorClass));
}
if(targetClass.getSuperclass() != null){
// try superclass
List<InheritancePath> pathsFromSuperClass =
classInheritancePaths(targetClass.getSuperclass(), potentialAncestorClass);
if(!pathsFromSuperClass.isEmpty()){
for(InheritancePath path : pathsFromSuperClass){
path.add(targetClass);
returnList.add(path);
}
}
}
// try interfaces
for(Class<?> interf : targetClass.getInterfaces()){
List<InheritancePath> pathsFromInterface =
classInheritancePaths(interf, potentialAncestorClass);
if(!pathsFromInterface.isEmpty()){
for(InheritancePath path : pathsFromInterface){
path.add(targetClass);
returnList.add(path);
}
}
}
}
return returnList;
}
/**
* Represents the path from a base class to a superclass
*/
public static final class InheritancePath implements Iterable<Class<?>>{
private List<Class<?>> path = new ArrayList<Class<?>>();
public InheritancePath(Class<?> root){
path.add(root);
}
void add(Class<?> pathElement){
path.add(0, pathElement);
}
public Iterator<Class<?>> iterator(){
return path.iterator();
}
public int depth(){
return path.size();
}
public String toString(){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < path.size(); i++){
sb.append(path.get(i).getName());
if(i < path.size() - 1){
sb.append(" -> ");
}
}
return sb.toString();
}
}
public static void main(String[] args) {
List<InheritancePath> paths = classInheritancePaths(ConcurrentLinkedQueue.class, Collection.class);
for(InheritancePath path : paths){
System.out.println(path);
}
}
}
非常感谢:-)我已经有了代码在这个问题上的大部分在我心中,但只是想看看,如果有可能有一些BSD许可的图书馆,我可以使用,而无需将我自己的东西移动到可重用的图书馆:-) – 2010-01-05 18:25:44
非常感谢您和提供的东西的清单,记住:-) – 2010-01-05 18:26:20
@host最好的将是一个随时可以使用的解决方案。但缺乏一个,我认为这可能是有用的;-)很高兴我们分享相同的观点:-) – KLE 2010-01-07 07:54:08