buildExpression(CheckRule)   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
package io.github.ro4.annotation;
2
3
import io.github.ro4.CheckTemplate;
4
import io.github.ro4.Context;
5
import io.github.ro4.check.CheckResult;
6
import io.github.ro4.check.ExceptionProvider;
7
import io.github.ro4.constant.MagicMark;
8
import org.aopalliance.intercept.MethodInterceptor;
9
import org.aopalliance.intercept.MethodInvocation;
10
import org.springframework.util.ObjectUtils;
11
import org.springframework.validation.BindingResult;
12
13 1
public class CheckableMethodInterceptor implements MethodInterceptor {
14
15
    private CheckTemplate checkTemplate;
16
17
    private ExceptionProvider<?> exceptionProvider;
18
19
    public void setCheckTemplate(CheckTemplate checkTemplate) {
20 1
        this.checkTemplate = checkTemplate;
21 1
    }
22
23
    public void setExceptionProvider(ExceptionProvider<?> exceptionProvider) {
24 1
        this.exceptionProvider = exceptionProvider;
25 1
    }
26
27
    @Override
28
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
29 1
        Checkable anno = methodInvocation.getMethod().getAnnotation(Checkable.class);
30 1
        boolean stopOnFirstFailure = anno.stopOnFirstFailure();
31 1
        Object[] args = methodInvocation.getArguments();
32 1
        BindingResult bindingResult = new CheckResult(methodInvocation.getMethod().getName());
33 1
        for (CheckRule checkRule : anno.value()) {
34 1
            Context ctx = new Context();
35 1
            for (int i = 0; i < args.length; i++) {
36 1
                ctx.setAttribute(MagicMark.ARGS_NAME + i, args[i]);
37
            }
38 1
            ctx.setAttribute(MagicMark.EXPRESSION, buildExpression(checkRule));
39 1
            ctx.setAttribute(MagicMark.MESSAGE, checkRule.message());
40 1
            if (checkTemplate.check(ctx)) {
41 1
                continue;
42
            }
43 1
            bindingResult.reject(checkRule.field(), checkRule.message());
44 1
            if (stopOnFirstFailure) {
45 1
                break;
46
            }
47
        }
48 1
        if (bindingResult.hasErrors()) {
49 1
            RuntimeException throwable = exceptionProvider.produce(bindingResult);
50 1
            if (throwable != null) {
51 1
                throw throwable;
52
            }
53
        }
54 1
        return methodInvocation.proceed();
55
    }
56
57
    protected String buildExpression(CheckRule checkRule) {
58 1
        String expression = checkRule.expression();
59
60 1
        if (!ObjectUtils.isEmpty(checkRule.andExpression())) {
61 1
            expression = String.format("(%s) && (%s)", expression, checkRule.andExpression());
62
        }
63
64 1
        if (!ObjectUtils.isEmpty(checkRule.orExpression())) {
65 1
            expression = String.format("(%s) || (%s)", expression, checkRule.orExpression());
66
        }
67 1
        return expression;
68
    }
69
}