Vector is an old, somewhat deprecated class. I'll use a generic list version instead:
public static List<Class<?>> getAssignableClasses(final Object o){
final List<Class<?>> list;
if(o == null){
list = Collections.emptyList();
} else{
list = new ArrayList<Class<?>>();
Class<?> tmp = o.getClass();
do{
list.add(tmp);
tmp = tmp.getSuperclass();
} while(tmp != null);
}
return list;
}
And as a bonus, here is a method that gathers all assignable interfaces based on the above method:
public static List<Class<?>> getAssignableInterfaces(final Object o){
final List<Class<?>> result;
final List<Class<?>> assignableClasses = getAssignableClasses(o);
if(assignableClasses.isEmpty()){
result = Collections.emptyList();
} else{
final Set<Class<?>> interfaces =
new LinkedHashSet<Class<?>>(assignableClasses.size() * 2);
for(final Class<?> clazz : assignableClasses){
interfaces.addAll(Arrays.asList(clazz.getInterfaces()));
}
result = new ArrayList<Class<?>>(interfaces);
}
return result;
}
Test:
public static void main(final String[] args){
final Map<String, String> map =
new ConcurrentHashMap<String, String>();
System.out.println(getAssignableClasses(map));
System.out.println(getAssignableInterfaces(map));
}
Output:
[class java.util.concurrent.ConcurrentHashMap, class java.util.AbstractMap, class java.lang.Object]
[interface java.util.concurrent.ConcurrentMap, interface java.io.Serializable, interface java.util.Map]