MATSIM
ClassUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.utils.misc;
21 
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.Stack;
25 
29 public final class ClassUtils {
30  private ClassUtils(){} // do not instantiate
31 
40  public static Set<Class<?>> getAllTypes(final Class<?> klass) {
41  Set<Class<?>> set = new HashSet<Class<?>>();
42  Stack<Class<?>> stack = new Stack<Class<?>>();
43  stack.add(klass);
44 
45  while (!stack.isEmpty()) {
46  Class<?> c = stack.pop();
47  set.add(c);
48  for (Class<?> k : c.getInterfaces()) {
49  stack.push(k);
50  }
51  if (c.getSuperclass() != null) {
52  stack.push(c.getSuperclass());
53  }
54  }
55 
56  return set;
57  }
58 
59  public static Set<Class<?>> getAllInterfaces( final Class<?> klass ) {
60  Set<Class<?>> intfs = new HashSet<Class<?>>();
61  for (Class<?> intf : klass.getInterfaces()) {
62  intfs.add(intf);
63  intfs.addAll(getAllInterfaces(intf));
64  }
65  if (!klass.isInterface()) {
66  Class<?> superclass = klass.getSuperclass();
67  while (superclass != Object.class) {
68  intfs.addAll(getAllInterfaces(superclass));
69  superclass = superclass.getSuperclass();
70  }
71  }
72  return intfs;
73  }
74 }
static Set< Class<?> > getAllTypes(final Class<?> klass)
Definition: ClassUtils.java:40
static Set< Class<?> > getAllInterfaces(final Class<?> klass)
Definition: ClassUtils.java:59