Passed
Push — master ( cdc8de...2a9b10 )
by Filip
05:17
created

com.hltech.pact.gen.domain.client.util.RequestParametersExtractor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A RequestParametersExtractor() 0 1 1
A extractParamsFromMethod(Parameter[]) 0 5 1
A extractParamDefaultValue(Parameter) 0 8 3
A extractParamsFromAnnotation(String[]) 0 17 3
A extractAll(Parameter[],String[]) 0 3 1
A extractParam(Parameter) 0 12 2
A extractParamName(Parameter) 0 10 3
1
package com.hltech.pact.gen.domain.client.util;
2
3
import com.hltech.pact.gen.domain.client.model.Param;
4
import org.springframework.web.bind.annotation.RequestParam;
5
import org.springframework.web.bind.annotation.ValueConstants;
6
7
import java.lang.reflect.Parameter;
8
import java.util.Arrays;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Optional;
12
import java.util.stream.Collectors;
13
import java.util.stream.Stream;
14
15
public final class RequestParametersExtractor {
16
17
    private RequestParametersExtractor() {}
18
19
    public static List<Param> extractAll(Parameter[] parameters, String[] path) {
20
        return Stream.concat(extractParamsFromMethod(parameters), extractParamsFromAnnotation(path))
21
            .collect(Collectors.toList());
22
    }
23
24
    private static Stream<Param> extractParamsFromAnnotation(String[] path) {
25
26
        if (path.length < 1) {
27
            return Stream.empty();
28
        }
29
30
        String[] splitPath = path[0].split("\\?");
31
32
        if (splitPath.length < 2) {
33
            return Stream.empty();
34
        }
35
36
        return Arrays.stream(splitPath[1].split("&"))
37
            .map(paramString -> Param.builder()
38
                .name(paramString.split("=")[0])
39
                .defaultValue(paramString.split("=")[1])
40
                .build());
41
    }
42
43
    private static Stream<Param> extractParamsFromMethod(Parameter[] parameters) {
44
        return Arrays.stream(parameters)
45
            .filter(param -> param.getAnnotation(RequestParam.class) != null)
46
            .filter(param -> param.getType() != Map.class)
47
            .map(RequestParametersExtractor::extractParam);
48
    }
49
50
    private static Param extractParam(Parameter param) {
51
        Param.ParamBuilder builder = Param.builder();
52
53
        extractParamDefaultValue(param).ifPresent(builder::defaultValue);
54
55
        List<Class<?>> paramTypes = TypeExtractor.extractParameterTypesFromType(param.getParameterizedType());
56
57
        return builder
58
            .name(extractParamName(param))
59
            .type(param.getType())
60
            .genericArgumentType(paramTypes.isEmpty() ? null : paramTypes.get(0))
61
            .build();
62
    }
63
64
    private static Optional<Object> extractParamDefaultValue(Parameter param) {
65
        RequestParam annotation = param.getAnnotation(RequestParam.class);
66
67
        if (annotation.defaultValue().equals(ValueConstants.DEFAULT_NONE) || annotation.defaultValue().isEmpty()) {
68
            return Optional.empty();
69
        }
70
71
        return Optional.of(annotation.defaultValue());
72
    }
73
74
    private static String extractParamName(Parameter param) {
75
        RequestParam annotation = param.getAnnotation(RequestParam.class);
76
77
        if (!annotation.name().isEmpty()) {
78
            return annotation.name();
79
        } else if (!annotation.value().isEmpty()) {
80
            return annotation.value();
81
        }
82
83
        return param.getName();
84
    }
85
}
86