MCPcopy Create free account
hub / github.com/cmu-db/benchbase / ClassUtil

Class ClassUtil

src/main/java/com/oltpbenchmark/util/ClassUtil.java:33–204  ·  view source on GitHub ↗

@author pavlo

Source from the content-addressed store, hash-verified

31 * @author pavlo
32 */
33public abstract class ClassUtil {
34 private static final Logger LOG = LoggerFactory.getLogger(ClassUtil.class);
35
36 private static final Map<Class<?>, List<Class<?>>> CACHE_getSuperClasses = new HashMap<>();
37 private static final Map<Class<?>, Set<Class<?>>> CACHE_getInterfaceClasses = new HashMap<>();
38
39 /**
40 * Get the generic types for the given field
41 *
42 * @param field
43 * @return
44 */
45 public static List<Class<?>> getGenericTypes(Field field) {
46 ArrayList<Class<?>> generic_classes = new ArrayList<>();
47 Type gtype = field.getGenericType();
48 if (gtype instanceof ParameterizedType) {
49 ParameterizedType ptype = (ParameterizedType) gtype;
50 getGenericTypesImpl(ptype, generic_classes);
51 }
52 return (generic_classes);
53 }
54
55 private static void getGenericTypesImpl(ParameterizedType ptype, List<Class<?>> classes) {
56 // list the actual type arguments
57 for (Type t : ptype.getActualTypeArguments()) {
58 if (t instanceof Class) {
59 // System.err.println("C: " + t);
60 classes.add((Class<?>) t);
61 } else if (t instanceof ParameterizedType) {
62 ParameterizedType next = (ParameterizedType) t;
63 // System.err.println("PT: " + next);
64 classes.add((Class<?>) next.getRawType());
65 getGenericTypesImpl(next, classes);
66 }
67 }
68 }
69
70 /**
71 * Return an ordered list of all the sub-classes for a given class Useful when dealing with
72 * generics
73 *
74 * @param element_class
75 * @return
76 */
77 public static List<Class<?>> getSuperClasses(Class<?> element_class) {
78 List<Class<?>> ret = ClassUtil.CACHE_getSuperClasses.get(element_class);
79 if (ret == null) {
80 ret = new ArrayList<>();
81 while (element_class != null) {
82 ret.add(element_class);
83 element_class = element_class.getSuperclass();
84 }
85 ret = Collections.unmodifiableList(ret);
86 ClassUtil.CACHE_getSuperClasses.put(element_class, ret);
87 }
88 return (ret);
89 }
90

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected