• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 19225086188

10 Nov 2025 08:15AM UTC coverage: 84.13%. Remained the same
19225086188

push

github

TAKETODAY
:white_check_mark:

61359 of 78029 branches covered (78.64%)

Branch coverage included in aggregate %.

145112 of 167391 relevant lines covered (86.69%)

3.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.31
today-context/src/main/java/infra/validation/beanvalidation/BeanValidationPostProcessor.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.validation.beanvalidation;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Iterator;
23
import java.util.Set;
24

25
import infra.aop.framework.AopProxyUtils;
26
import infra.beans.BeansException;
27
import infra.beans.factory.BeanInitializationException;
28
import infra.beans.factory.InitializationBeanPostProcessor;
29
import infra.beans.factory.InitializingBean;
30
import infra.lang.Assert;
31
import jakarta.validation.ConstraintViolation;
32
import jakarta.validation.Validation;
33
import jakarta.validation.Validator;
34
import jakarta.validation.ValidatorFactory;
35

36
/**
37
 * Simple {@link InitializationBeanPostProcessor} that checks JSR-303 constraint annotations
38
 * in Framework-managed beans, throwing an initialization exception in case of
39
 * constraint violations right before calling the bean's init method (if any).
40
 *
41
 * @author Juergen Hoeller
42
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
43
 * @since 4.0s
44
 */
45
public class BeanValidationPostProcessor implements InitializationBeanPostProcessor, InitializingBean {
2✔
46

47
  @Nullable
48
  private Validator validator;
49

50
  private boolean afterInitialization = false;
4✔
51

52
  /**
53
   * Set the JSR-303 Validator to delegate to for validating beans.
54
   * <p>Default is the default ValidatorFactory's default Validator.
55
   */
56
  public void setValidator(Validator validator) {
57
    this.validator = validator;
×
58
  }
×
59

60
  /**
61
   * Set the JSR-303 ValidatorFactory to delegate to for validating beans,
62
   * using its default Validator.
63
   * <p>Default is the default ValidatorFactory's default Validator.
64
   *
65
   * @see ValidatorFactory#getValidator()
66
   */
67
  public void setValidatorFactory(ValidatorFactory validatorFactory) {
68
    this.validator = validatorFactory.getValidator();
×
69
  }
×
70

71
  /**
72
   * Choose whether to perform validation after bean initialization
73
   * (i.e. after init methods) instead of before (which is the default).
74
   * <p>Default is "false" (before initialization). Switch this to "true"
75
   * (after initialization) if you would like to give init methods a chance
76
   * to populate constrained fields before they get validated.
77
   */
78
  public void setAfterInitialization(boolean afterInitialization) {
79
    this.afterInitialization = afterInitialization;
3✔
80
  }
1✔
81

82
  @Override
83
  public void afterPropertiesSet() {
84
    if (this.validator == null) {
3!
85
      this.validator = Validation.buildDefaultValidatorFactory().getValidator();
4✔
86
    }
87
  }
1✔
88

89
  @Override
90
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
91
    if (!this.afterInitialization) {
3✔
92
      doValidate(bean);
3✔
93
    }
94
    return bean;
2✔
95
  }
96

97
  @Override
98
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
99
    if (this.afterInitialization) {
3✔
100
      doValidate(bean);
3✔
101
    }
102
    return bean;
2✔
103
  }
104

105
  /**
106
   * Perform validation of the given bean.
107
   *
108
   * @param bean the bean instance to validate
109
   * @see Validator#validate
110
   */
111
  protected void doValidate(Object bean) {
112
    Assert.state(this.validator != null, "No Validator set");
7!
113
    Object objectToValidate = AopProxyUtils.getSingletonTarget(bean);
3✔
114
    if (objectToValidate == null) {
2✔
115
      objectToValidate = bean;
2✔
116
    }
117
    Set<ConstraintViolation<Object>> result = this.validator.validate(objectToValidate);
7✔
118

119
    if (!result.isEmpty()) {
3✔
120
      StringBuilder sb = new StringBuilder("Bean state is invalid: ");
5✔
121
      Iterator<ConstraintViolation<Object>> it = result.iterator();
3✔
122
      while (it.hasNext()) {
3✔
123
        ConstraintViolation<Object> violation = it.next();
4✔
124
        sb.append(violation.getPropertyPath())
5✔
125
                .append(" - ")
2✔
126
                .append(violation.getMessage());
3✔
127
        if (it.hasNext()) {
3!
128
          sb.append("; ");
×
129
        }
130
      }
1✔
131
      throw new BeanInitializationException(sb.toString());
6✔
132
    }
133
  }
1✔
134

135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc