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

TAKETODAY / today-infrastructure / 20632861616

01 Jan 2026 04:53AM UTC coverage: 84.18% (-0.3%) from 84.439%
20632861616

push

github

TAKETODAY
:sparkles: ApplicationType 支持通过 SPI 获取

55643 of 70608 branches covered (78.81%)

Branch coverage included in aggregate %.

130472 of 150485 relevant lines covered (86.7%)

3.73 hits per line

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

74.36
today-context/src/main/java/infra/validation/FieldError.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;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.io.Serial;
23

24
import infra.lang.Assert;
25
import infra.util.ObjectUtils;
26

27
/**
28
 * Encapsulates a field error, that is, a reason for rejecting a specific
29
 * field value.
30
 *
31
 * <p>See the {@link DefaultMessageCodesResolver} javadoc for details on
32
 * how a message code list is built for a {@code FieldError}.
33
 *
34
 * @author Rod Johnson
35
 * @author Juergen Hoeller
36
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
37
 * @see DefaultMessageCodesResolver
38
 * @since 4.0
39
 */
40
public class FieldError extends ObjectError {
41

42
  @Serial
43
  private static final long serialVersionUID = 1L;
44

45
  private final String field;
46

47
  @Nullable
48
  private final Object rejectedValue;
49

50
  private final boolean bindingFailure;
51

52
  /**
53
   * Create a new FieldError instance.
54
   *
55
   * @param objectName the name of the affected object
56
   * @param field the affected field of the object
57
   * @param defaultMessage the default message to be used to resolve this message
58
   */
59
  public FieldError(String objectName, String field, String defaultMessage) {
60
    this(objectName, field, null, false, null, null, defaultMessage);
9✔
61
  }
1✔
62

63
  /**
64
   * Create a new FieldError instance.
65
   *
66
   * @param objectName the name of the affected object
67
   * @param field the affected field of the object
68
   * @param rejectedValue the rejected field value
69
   * @param bindingFailure whether this error represents a binding failure
70
   * (like a type mismatch); else, it is a validation failure
71
   * @param codes the codes to be used to resolve this message
72
   * @param arguments the array of arguments to be used to resolve this message
73
   * @param defaultMessage the default message to be used to resolve this message
74
   */
75
  public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure,
76
          String @Nullable [] codes, Object @Nullable [] arguments, @Nullable String defaultMessage) {
77

78
    super(objectName, codes, arguments, defaultMessage);
6✔
79
    Assert.notNull(field, "Field is required");
3✔
80
    this.field = field;
3✔
81
    this.rejectedValue = rejectedValue;
3✔
82
    this.bindingFailure = bindingFailure;
3✔
83
  }
1✔
84

85
  /**
86
   * Return the affected field of the object.
87
   */
88
  public String getField() {
89
    return this.field;
3✔
90
  }
91

92
  /**
93
   * Return the rejected field value.
94
   */
95
  @Nullable
96
  public Object getRejectedValue() {
97
    return this.rejectedValue;
3✔
98
  }
99

100
  /**
101
   * Return whether this error represents a binding failure
102
   * (like a type mismatch); otherwise it is a validation failure.
103
   */
104
  public boolean isBindingFailure() {
105
    return this.bindingFailure;
3✔
106
  }
107

108
  @Override
109
  @SuppressWarnings("NullAway")
110
  public boolean equals(@Nullable Object other) {
111
    if (this == other) {
3✔
112
      return true;
2✔
113
    }
114
    if (!super.equals(other)) {
4✔
115
      return false;
2✔
116
    }
117
    FieldError otherError = (FieldError) other;
3✔
118
    return getField().equals(otherError.getField())
8!
119
            && ObjectUtils.nullSafeEquals(getRejectedValue(), otherError.getRejectedValue())
6!
120
            && isBindingFailure() == otherError.isBindingFailure();
6!
121
  }
122

123
  @Override
124
  public int hashCode() {
125
    int hashCode = super.hashCode();
×
126
    hashCode = 29 * hashCode + getField().hashCode();
×
127
    hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getRejectedValue());
×
128
    hashCode = 29 * hashCode + (isBindingFailure() ? 1 : 0);
×
129
    return hashCode;
×
130
  }
131

132
  @Override
133
  public String toString() {
134
    // We would preferably use ObjectUtils.nullSafeConciseToString(rejectedValue) here but
135
    // keep including the full nullSafeToString representation for backwards compatibility.
136
    return "Field error in object '" + getObjectName() + "' on field '" + this.field +
7✔
137
            "': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
2✔
138
            resolvableToString();
2✔
139
  }
140

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

© 2026 Coveralls, Inc