/** * Finds all super classes and interfaces for a given class * * @param cls The class to scan * @return The collected related classes found */ public static Set<Class> findAllTypes(Class cls) { Set<Class> types = new HashSet<>(); findAllTypes(cls, types); return types; }
/** * Finds all super classes and interfaces for a given class * * @param cls The class to scan * @param types The collected related classes found */ public static void findAllTypes(Class cls, Set<Class> types) { if (cls == null) { return; } // check to ensure it hasn't been scanned yet if (types.contains(cls)) { return; } types.add(cls); findAllTypes(cls.getSuperclass(), types); for (int x = 0; x < cls.getInterfaces().length; x++) { findAllTypes(cls.getInterfaces()[x], types); } }
/** * Finds all referred packages for the specified set of classes/interfaces by scanning their bytecode. * Packages starting with "java." are ignored. * * @param classes The set of classes/interfaces to scan * @return The set of referred packages * @throws IOException If there are any problems scanning bytecode * @since 5.0.0 */ public static Set<String> findReferredPackageNames(Collection<Class<?>> classes) throws IOException { if (classes == null || classes.isEmpty()) { return Collections.emptySet(); } Set<Class> classesToScan = new HashSet<>(); for (Class<?> clazz : classes) { ClassUtils.findAllTypes(clazz, classesToScan); } Set<String> referredClasses = new HashSet<>(); Set<String> referredPackages = new HashSet<>(); for (Class inf : classesToScan) { String clsName = inf.getName().replace('.', '/') + ".class"; crawlReferenceTree(clsName, referredClasses, referredPackages, 1); } return ImmutableSet.copyOf(referredPackages); }