InheritanceAnalyser implements ClassAnalyser<Map<String, Class<?>[]>> { @Override public Map<String, Class<?>[]> analyse(Map<String, Class<?>> classes) { Map<String, Class<?>[]> inheritances = new TreeMap<String, Class<?>[]>(); List<Class<?>> classList = new ArrayList<Class<?>>(); String[] classNames = classes.keySet().toArray(EMPTY_STRING_ARRAY); Class<?> type; for (Map.Entry<String, Class<?>> entry : classes.entrySet()) { classList.clear(); LOGGER.debug("analyse - looking for {}", entry.getKey()); for (String className : classNames) { if (!entry.getKey().equals(className)) { type = classes.get(className); if (entry.getValue().isAssignableFrom(type) && !type.isInterface() && !Modifier.isAbstract(type.getModifiers())) { classList.add(type); LOGGER.debug("analyse - found {}", type); } } } if (!classList.isEmpty()) { inheritances.put(entry.getKey(), classList.toArray(EMPTY_CLASS_ARRAY)); } } return inheritances; }  @Override Map<String, Class<?>[]> analyse(Map<String, Class<?>> classes);  }
@Test public void analyse_NoClasses() { Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); Map<String, Class<?>[]> inheritances; inheritances = inheritanceAnalyser.analyse(classes); assertNotNull(inheritances); assertEquals(0, inheritances.size()); }
@Test public void analyse_NoInheritances() { Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); Map<String, Class<?>[]> inheritances; classes.put(Object.class.getName(), Object.class); classes.put(CharSequence.class.getName(), CharSequence.class); classes.put(Runnable.class.getName(), Runnable.class); inheritances = inheritanceAnalyser.analyse(classes); assertNotNull(inheritances); assertEquals(0, inheritances.size()); }
@Test public void analyse_Inheritances() { Map<String, Class<?>> classes = new TreeMap<String, Class<?>>(); Map<String, Class<?>[]> inheritances; classes.put(Object.class.getName(), Object.class); classes.put(CharSequence.class.getName(), CharSequence.class); classes.put(Runnable.class.getName(), Runnable.class); classes.put(Thread.class.getName(), Thread.class); classes.put(String.class.getName(), String.class); inheritances = inheritanceAnalyser.analyse(classes); assertNotNull(inheritances); assertEquals(3, inheritances.size()); assertEquals(Thread.class, inheritances.get(Runnable.class.getName())[0]); assertEquals(String.class, inheritances.get(CharSequence.class.getName())[0]); assertEquals(String.class, inheritances.get(Object.class.getName())[0]); assertEquals(Thread.class, inheritances.get(Object.class.getName())[1]); }