private static List<Class<?>> getClassesForRecursion(String basePackage, Method method) {
        List<Class<?>> classesForRecursion = new ArrayList<>();

        // Check regular return types
        if (method.getReturnType().toString().contains(basePackage)) {
            classesForRecursion.add(method.getReturnType());
        } else {
            // Check for memory return types
            if (method.getGenericReturnType() instanceof ParameterizedType) {
                ParameterizedType type = (ParameterizedType) method.getGenericReturnType();
                Arrays.stream(type.getActualTypeArguments())
                        .filter(actualType -> actualType.getTypeName().contains(basePackage))
                        .forEach(actualType -> classesForRecursion.add((Class<?>) actualType));
            }
        }

        // Check for parameter types
        Arrays.stream(method.getParameterTypes())
                .filter(paramType -> paramType.toString().contains(basePackage))
                .forEach(classesForRecursion::add);

        return classesForRecursion;
    }
Looks at a method and gets any return or parameter types that match the basePackage.
stroom-query/stroom-query-api/src/test/java/stroom/query/api/current/ClassPhotographer.java