001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.validation; 018 019import java.beans.PropertyEditor; 020import java.util.List; 021import java.util.Map; 022 023import org.springframework.beans.PropertyEditorRegistry; 024import org.springframework.lang.Nullable; 025import org.springframework.util.Assert; 026 027/** 028 * Thrown when binding errors are considered fatal. Implements the 029 * {@link BindingResult} interface (and its super-interface {@link Errors}) 030 * to allow for the direct analysis of binding errors. 031 * 032 * <p>As of Spring 2.0, this is a special-purpose class. Normally, 033 * application code will work with the {@link BindingResult} interface, 034 * or with a {@link DataBinder} that in turn exposes a BindingResult via 035 * {@link org.springframework.validation.DataBinder#getBindingResult()}. 036 * 037 * @author Rod Johnson 038 * @author Juergen Hoeller 039 * @author Rob Harrop 040 * @see BindingResult 041 * @see DataBinder#getBindingResult() 042 * @see DataBinder#close() 043 */ 044@SuppressWarnings("serial") 045public class BindException extends Exception implements BindingResult { 046 047 private final BindingResult bindingResult; 048 049 050 /** 051 * Create a new BindException instance for a BindingResult. 052 * @param bindingResult the BindingResult instance to wrap 053 */ 054 public BindException(BindingResult bindingResult) { 055 Assert.notNull(bindingResult, "BindingResult must not be null"); 056 this.bindingResult = bindingResult; 057 } 058 059 /** 060 * Create a new BindException instance for a target bean. 061 * @param target the target bean to bind onto 062 * @param objectName the name of the target object 063 * @see BeanPropertyBindingResult 064 */ 065 public BindException(Object target, String objectName) { 066 Assert.notNull(target, "Target object must not be null"); 067 this.bindingResult = new BeanPropertyBindingResult(target, objectName); 068 } 069 070 071 /** 072 * Return the BindingResult that this BindException wraps. 073 */ 074 public final BindingResult getBindingResult() { 075 return this.bindingResult; 076 } 077 078 079 @Override 080 public String getObjectName() { 081 return this.bindingResult.getObjectName(); 082 } 083 084 @Override 085 public void setNestedPath(String nestedPath) { 086 this.bindingResult.setNestedPath(nestedPath); 087 } 088 089 @Override 090 public String getNestedPath() { 091 return this.bindingResult.getNestedPath(); 092 } 093 094 @Override 095 public void pushNestedPath(String subPath) { 096 this.bindingResult.pushNestedPath(subPath); 097 } 098 099 @Override 100 public void popNestedPath() throws IllegalStateException { 101 this.bindingResult.popNestedPath(); 102 } 103 104 105 @Override 106 public void reject(String errorCode) { 107 this.bindingResult.reject(errorCode); 108 } 109 110 @Override 111 public void reject(String errorCode, String defaultMessage) { 112 this.bindingResult.reject(errorCode, defaultMessage); 113 } 114 115 @Override 116 public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { 117 this.bindingResult.reject(errorCode, errorArgs, defaultMessage); 118 } 119 120 @Override 121 public void rejectValue(@Nullable String field, String errorCode) { 122 this.bindingResult.rejectValue(field, errorCode); 123 } 124 125 @Override 126 public void rejectValue(@Nullable String field, String errorCode, String defaultMessage) { 127 this.bindingResult.rejectValue(field, errorCode, defaultMessage); 128 } 129 130 @Override 131 public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { 132 this.bindingResult.rejectValue(field, errorCode, errorArgs, defaultMessage); 133 } 134 135 @Override 136 public void addAllErrors(Errors errors) { 137 this.bindingResult.addAllErrors(errors); 138 } 139 140 141 @Override 142 public boolean hasErrors() { 143 return this.bindingResult.hasErrors(); 144 } 145 146 @Override 147 public int getErrorCount() { 148 return this.bindingResult.getErrorCount(); 149 } 150 151 @Override 152 public List<ObjectError> getAllErrors() { 153 return this.bindingResult.getAllErrors(); 154 } 155 156 @Override 157 public boolean hasGlobalErrors() { 158 return this.bindingResult.hasGlobalErrors(); 159 } 160 161 @Override 162 public int getGlobalErrorCount() { 163 return this.bindingResult.getGlobalErrorCount(); 164 } 165 166 @Override 167 public List<ObjectError> getGlobalErrors() { 168 return this.bindingResult.getGlobalErrors(); 169 } 170 171 @Override 172 @Nullable 173 public ObjectError getGlobalError() { 174 return this.bindingResult.getGlobalError(); 175 } 176 177 @Override 178 public boolean hasFieldErrors() { 179 return this.bindingResult.hasFieldErrors(); 180 } 181 182 @Override 183 public int getFieldErrorCount() { 184 return this.bindingResult.getFieldErrorCount(); 185 } 186 187 @Override 188 public List<FieldError> getFieldErrors() { 189 return this.bindingResult.getFieldErrors(); 190 } 191 192 @Override 193 @Nullable 194 public FieldError getFieldError() { 195 return this.bindingResult.getFieldError(); 196 } 197 198 @Override 199 public boolean hasFieldErrors(String field) { 200 return this.bindingResult.hasFieldErrors(field); 201 } 202 203 @Override 204 public int getFieldErrorCount(String field) { 205 return this.bindingResult.getFieldErrorCount(field); 206 } 207 208 @Override 209 public List<FieldError> getFieldErrors(String field) { 210 return this.bindingResult.getFieldErrors(field); 211 } 212 213 @Override 214 @Nullable 215 public FieldError getFieldError(String field) { 216 return this.bindingResult.getFieldError(field); 217 } 218 219 @Override 220 @Nullable 221 public Object getFieldValue(String field) { 222 return this.bindingResult.getFieldValue(field); 223 } 224 225 @Override 226 @Nullable 227 public Class<?> getFieldType(String field) { 228 return this.bindingResult.getFieldType(field); 229 } 230 231 @Override 232 @Nullable 233 public Object getTarget() { 234 return this.bindingResult.getTarget(); 235 } 236 237 @Override 238 public Map<String, Object> getModel() { 239 return this.bindingResult.getModel(); 240 } 241 242 @Override 243 @Nullable 244 public Object getRawFieldValue(String field) { 245 return this.bindingResult.getRawFieldValue(field); 246 } 247 248 @Override 249 @SuppressWarnings("rawtypes") 250 @Nullable 251 public PropertyEditor findEditor(@Nullable String field, @Nullable Class valueType) { 252 return this.bindingResult.findEditor(field, valueType); 253 } 254 255 @Override 256 @Nullable 257 public PropertyEditorRegistry getPropertyEditorRegistry() { 258 return this.bindingResult.getPropertyEditorRegistry(); 259 } 260 261 @Override 262 public String[] resolveMessageCodes(String errorCode) { 263 return this.bindingResult.resolveMessageCodes(errorCode); 264 } 265 266 @Override 267 public String[] resolveMessageCodes(String errorCode, String field) { 268 return this.bindingResult.resolveMessageCodes(errorCode, field); 269 } 270 271 @Override 272 public void addError(ObjectError error) { 273 this.bindingResult.addError(error); 274 } 275 276 @Override 277 public void recordFieldValue(String field, Class<?> type, @Nullable Object value) { 278 this.bindingResult.recordFieldValue(field, type, value); 279 } 280 281 @Override 282 public void recordSuppressedField(String field) { 283 this.bindingResult.recordSuppressedField(field); 284 } 285 286 @Override 287 public String[] getSuppressedFields() { 288 return this.bindingResult.getSuppressedFields(); 289 } 290 291 292 /** 293 * Returns diagnostic information about the errors held in this object. 294 */ 295 @Override 296 public String getMessage() { 297 return this.bindingResult.toString(); 298 } 299 300 @Override 301 public boolean equals(@Nullable Object other) { 302 return (this == other || this.bindingResult.equals(other)); 303 } 304 305 @Override 306 public int hashCode() { 307 return this.bindingResult.hashCode(); 308 } 309 310}