View Javadoc

1   package org.sourceforge.jemm.client;
2   
3   import java.util.Iterator;
4   import java.util.NoSuchElementException;
5   
6   /**
7    * Transverses a Class and its parent classes that are either
8    * abstract or concrete, but skips Object as the final parent.
9    * 
10   * The iterator does not iterate the interfaces.
11   * 
12   * @author Paul Keeble
13   *
14   */
15  public class ClassHierarchyIterator implements Iterator<Class<?>>,Iterable<Class<?>> {
16  	Class<?> current;
17  	
18  	public ClassHierarchyIterator(Class<?> clazz) {
19  		this.current = clazz;
20  	}
21  
22  	@Override
23  	public boolean hasNext() {
24  		if(current.isInterface() || current.isPrimitive())
25  			return false;
26  		return !current.equals(Object.class);
27  	}
28  
29  	@Override
30  	public Class<?> next() {
31  		
32  		if(!hasNext()) {
33  			throw new NoSuchElementException("No further classes found");
34  		}
35  		Class<?> result = current;
36  		current = current.getSuperclass();
37  		return result;
38  	}
39  
40  	@Override
41  	public void remove() {
42  		throw new RuntimeException("Remove not supported for classHierarchyIterator");
43  	}
44  
45  	@Override
46  	public Iterator<Class<?>> iterator() {
47  		return this;
48  	}
49  }