2

I have the following code:

Reflections reflections = new Reflections("com.mypackage.cmds");
Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class);

What it does is to store in a Set all classes descendant from Command class that are within the package com.mypackage.cmds.

Now what I want to do is to iterate or traverse through that returned set so each of those classes are loaded calling Class.fromName method. How can I achieve this?

Thanks a lot in advance,

4

2 回答 2

6

You do it with a standard for-each loop:

for (Class clazz: commandClasses) {
    // operate on clazz
}

This works for any Collection that implements the Iterator interface.

于 2012-07-21T13:11:12.783 回答
0

你可以这样做:

Iterator i = commandClasses.iterator();
        while (i.hasNext()) {
            i.next(); //will return next object     
        }
于 2012-07-21T13:14:37.943 回答