8

I have been trying to do this for quite some time now and can't seem to get the desired output.

What I want to do is have a class name say java.util.Vector

get the:

  • the directly implemented interfaces if java.util.Vector.
  • the interfaces directly implemented by the superclasses.
  • and, transitively, all superinterfaces of these interfaces.

Any help would be appreciated.

hvgotcodes
121k33 gold badges209 silver badges237 bronze badges
asked Mar 27, 2012 at 0:19
3
  • Are you looking for java.lang.Class methods? getInterfaces? Commented Mar 27, 2012 at 0:22
  • Yes i use getInterfaces(); do i use recursion for this or is there another way? Commented Mar 27, 2012 at 0:26
  • I would use recursion. There are a number of samples around, ex java2s.com/Tutorial/Java/0125__Reflection/… Commented Mar 27, 2012 at 0:40

4 Answers 4

4

You could do a BFS using the reflection for it.

Start with a Set<Class<?>> that contains only Vector, and iteratively increase the set with new elements using Class.getInterfaces() and Class.getSuperclass()

Add the just added elements to the Queue [which is needed for the BFS run]. Terminate when the queue is empty.

Post processing: iterate the Set - and take only objects that are interfaces using Class.isInterface()

Should look something like that:

Class<?> cl = Vector.class;
Queue<Class<?>> queue = new LinkedList<Class<?>>();
Set<Class<?>> types =new HashSet<Class<?>>();
queue.add(cl);
types.add(cl);
 //BFS:
while (queue.isEmpty() == false) {
 Class<?> curr = queue.poll();
 Class<?>[] supers = curr.getInterfaces();
 for (Class<?> next : supers) {
 if (next != null && types.contains(next) == false) {
 types.add(next);
 queue.add(next);
 }
 }
 Class<?> next = curr.getSuperclass();
 if (next != null && types.contains(next) == false) {
 queue.add(next);
 types.add(next);
 }
}
 //post processing:
for (Class<?> curr : types) { 
 if (curr.isInterface()) System.out.println(curr);
}
answered Mar 27, 2012 at 0:54
Sign up to request clarification or add additional context in comments.

Comments

0

Although java.util.Vector is not an interface, and thus you cannot extend it with an interface, what you can do is use a library like Reflections to accommodate these sorts of features. Reflections allows you to scan the classpath and query for a set of conditions like what implements or extends the given class/interface. Ive used it successfully on a couple projects where I needed to scan for interface implementations and annotated classes.

Here's the explicit link: http://code.google.com/p/reflections/

Additionally, if you are looking to just find out what class/interface a class extends/implements you can just use the class reflection api via the class attribute.

Here's some examples:

//get all public methods of Vector
Vector.class.getMethods();
//get all methods of the superclass (AbstractList) of Vector
Vector.class.getSuperclass().getMethods();
//get all interfaces implemented by Vector
Vector.class.getInterfaces();
answered Mar 27, 2012 at 0:28

Comments

0

If you are open to use another library : use apache-commons-lang

answered Mar 27, 2012 at 4:29

1 Comment

The link you provided is dead. Please consider deleting the post or updating the link. Thanks!
0

The ClassUtils.getAllInterfaces method from Apache Commons Lang will do this:

List<Class<?>> supernterfaces = ClassUtils.getAllInterfaces(java.util.Vector.class);
answered Mar 24, 2021 at 2:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.