--- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sat Aug 15 11:52:38 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sat Aug 15 13:21:26 2015 +0200
@@ -24,9 +24,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Deque;
+import java.util.List;
import java.util.Map;
/**
@@ -164,4 +168,21 @@
return result.toString();
}
}
+
+ /**
+ * @param <P> type of the last parent
+ * @param <T> type of the examined class
+ * @param type examined class
+ * @param lastParent the last parent type to stop at
+ * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
+ */
+ public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
+ List<Class<? extends P>> hierarchy = new ArrayList<>();
+
+ for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
+ hierarchy.add(current);
+ }
+
+ return hierarchy;
+ }
}
--- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Sat Aug 15 11:52:38 2015 +0200
+++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Sat Aug 15 13:21:26 2015 +0200
@@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import static org.testng.Assert.*;
import org.testng.annotations.*;
@@ -74,4 +75,22 @@
assertEquals(Functions.repeat('f', 0), "");
assertEquals(Functions.repeat('f', 3), "fff");
}
+
+ @Test
+ public void testGetClassHierarchy() {
+ List<Class<? extends HierarchyMockClass2>> hierarchy = Functions.getClassHierarchy(HierarchyMockClass0.class, HierarchyMockClass2.class);
+ assertEquals(hierarchy.size(), 3, "invalid number of classes in the hierarchy");
+ assertEquals(hierarchy.get(0), HierarchyMockClass0.class);
+ assertEquals(hierarchy.get(1), HierarchyMockClass1.class);
+ assertEquals(hierarchy.get(2), HierarchyMockClass2.class);
+ }
+
+ private static class HierarchyMockClass0 extends HierarchyMockClass1 {
+ }
+
+ private static class HierarchyMockClass1 extends HierarchyMockClass2 {
+ }
+
+ private static class HierarchyMockClass2 {
+ }
}