Passed
Pull Request — master (#33)
by
unknown
05:54
created

createHandlers(List)   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
1
package com.hltech.pact.gen.domain.client.annotation;
2
3
import com.google.common.collect.Lists;
4
import com.hltech.pact.gen.domain.client.annotation.handlers.AnnotatedMethodHandler;
5
import io.github.classgraph.ClassGraph;
6
7
import java.util.List;
8
9
public class MappingHandlerFactory {
10
11
    private static final String PACKAGE = "com.hltech.pact.gen";
12
13
    public List<AnnotatedMethodHandler> createAll() {
14
        try {
15
            return createHandlers(findClasses());
16
        } catch (IllegalAccessException | InstantiationException exception) {
17
            throw new MappingMethodCreationException("Cannot create mapping handler", exception);
18
        }
19
    }
20
21
    private List<Class<?>> findClasses() {
22
        return new ClassGraph()
23
            .whitelistPackages(PACKAGE)
24
            .enableAnnotationInfo()
25
            .scan()
26
            .getClassesWithAnnotation(MappingMethodHandler.class.getName())
27
            .loadClasses();
28
    }
29
30
    private List<AnnotatedMethodHandler> createHandlers(List<Class<?>> classes)
31
        throws IllegalAccessException, InstantiationException {
32
33
        List<AnnotatedMethodHandler> result = Lists.newArrayList();
34
35
        for (Class<?> handlerClass : classes) {
36
            result.add((AnnotatedMethodHandler) handlerClass.newInstance());
37
        }
38
39
        return result;
40
    }
41
}
42