/** * 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; }
/** * Get the underlying class for a type, or null if the type is a variable type. * * @param type the type * @return the underlying class */ private static Class<?> getClass(Type type) { if (type instanceof Class) { return (Class) type; } else if (type instanceof ParameterizedType) { return getClass(((ParameterizedType) type).getRawType()); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); Class<?> componentClass = getClass(componentType); if (componentClass != null) { return Array.newInstance(componentClass, 0).getClass(); } else { return null; } } else { return null; } }
moduleTypeClass = ClassUtils.getTypeArguments(AbstractModuleDescriptor.class, getClass()).get(0); } catch (RuntimeException ex) { log.debug("Unable to get generic type, usually due to Class.forName() problems", ex);
/** * 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); } }
Map<Type, Type> resolvedTypes = new HashMap<>(); Type type = childClass; Class typeClass = getClass(type); typeClass = getClass(type); if (typeClass == null) { throw new IllegalArgumentException("Unable to find the class for the type " + type); baseType = resolvedTypes.get(baseType); typeArgumentsAsClasses.add(getClass(baseType));
/** * 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); }