package org.hildan.livedoc.springmvc.scanner;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.hildan.livedoc.core.scanner.types.records.FieldTypesScanner;
import org.hildan.livedoc.core.scanner.types.filters.DefaultTypeFilter;
import org.hildan.livedoc.core.scanner.types.TypesExplorer;
import org.hildan.livedoc.springmvc.scanner.builder.SpringRequestBodyBuilder;
import org.hildan.livedoc.springmvc.scanner.utils.ClasspathUtils;
import org.reflections.Reflections;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Sets;

public class TypesScanner {

    private final Reflections reflections;

    public TypesScanner(Reflections reflections) {
        this.reflections = reflections;
    }

    public Set<Class<?>> findJsondocObjects(List<String> packages) {
        Set<Type> rootTypes = getRootApiTypes();

        TypesExplorer explorer = new TypesExplorer(reflections, new FieldTypesScanner(),
                new DefaultTypeFilter());
        Set<Class<?>> classes = explorer.findTypes(rootTypes);

        return classes.stream().filter(clazz -> inWhiteListedPackages(packages, clazz)).collect(Collectors.toSet());
    }

    private Set<Type> getRootApiTypes() {
        Set<Type> types = Sets.newHashSet();
        Set<Method> methodsToDocument = getMethodsToDocument();
        for (Method method : methodsToDocument) {
            types.add(method.getGenericReturnType());
            Type requestBodyType = getBodyParamType(method);
            if (requestBodyType != null) {
                types.add(requestBodyType);
            }
        }
        return types;
    }

    private Set<Method> getMethodsToDocument() {
        Set<Method> methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(RequestMapping.class);
        if (ClasspathUtils.isSubscribeMappingOnClasspath()) {
            methodsAnnotatedWith.addAll(reflections.getMethodsAnnotatedWith(SubscribeMapping.class));
        }
        if (ClasspathUtils.isMessageMappingOnClasspath()) {
            methodsAnnotatedWith.addAll(reflections.getMethodsAnnotatedWith(MessageMapping.class));
        }
        return methodsAnnotatedWith;
    }

    private Type getBodyParamType(Method method) {
        int bodyParamIndex = SpringRequestBodyBuilder.getIndexOfBodyParam(method);
        if (bodyParamIndex < 0) {
            return null;
        }
        return method.getGenericParameterTypes()[bodyParamIndex];
    }

    private boolean inWhiteListedPackages(List<String> packages, Class<?> clazz) {
        Package p = clazz.getPackage();
        return p != null && packages.stream().anyMatch(whiteListedPkg -> p.getName().startsWith(whiteListedPkg));
    }
}

@@ -229,32 +229,32 @@
 ner.
-types.records.FieldTypes
+properties.FieldProperty
 Scan
@@ -1180,21 +1180,24 @@
 ew Field
-Types
+Property
 Scanner(
@@ -1198,32 +1198,16 @@
 anner(),
-%0A               
  new Def

Fix compile error in Spring TypesScanner since refac