001/* 002 * Copyright 2002-2012 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.util.Assert; 025 026/** 027 * Thrown when binding errors are considered fatal. Implements the 028 * {@link BindingResult} interface (and its super-interface {@link Errors}) 029 * to allow for the direct analysis of binding errors. 030 * 031 * <p>As of Spring 2.0, this is a special-purpose class. Normally, 032 * application code will work with the {@link BindingResult} interface, 033 * or with a {@link DataBinder} that in turn exposes a BindingResult via 034 * {@link org.springframework.validation.DataBinder#getBindingResult()}. 035 * 036 * @author Rod Johnson 037 * @author Juergen Hoeller 038 * @author Rob Harrop 039 * @see BindingResult 040 * @see DataBinder#getBindingResult() 041 * @see DataBinder#close() 042 */ 043@SuppressWarnings("serial") 044public class BindException extends Exception implements BindingResult { 045 046 private final BindingResult bindingResult; 047 048 049 /** 050 * Create a new BindException instance for a BindingResult. 051 * @param bindingResult the BindingResult instance to wrap 052 */ 053 public BindException(BindingResult bindingResult) { 054 Assert.notNull(bindingResult, "BindingResult must not be null"); 055 this.bindingResult = bindingResult; 056 } 057 058 /** 059 * Create a new BindException instance for a target bean. 060 * @param target target bean to bind onto 061 * @param objectName the name of the target object 062 * @see BeanPropertyBindingResult 063 */ 064 public BindException(Object target, String objectName) { 065 Assert.notNull(target, "Target object must not be null"); 066 this.bindingResult = new BeanPropertyBindingResult(target, objectName); 067 } 068 069 070 /** 071 * Return the BindingResult that this BindException wraps. 072 * Will typically be a BeanPropertyBindingResult. 073 * @see BeanPropertyBindingResult 074 */ 075 public final BindingResult getBindingResult() { 076 return this.bindingResult; 077 } 078 079 080 @Override 081 public String getObjectName() { 082 return this.bindingResult.getObjectName(); 083 } 084 085 @Override 086 public void setNestedPath(String nestedPath) { 087 this.bindingResult.setNestedPath(nestedPath); 088 } 089 090 @Override 091 public String getNestedPath() { 092 return this.bindingResult.getNestedPath(); 093 } 094 095 @Override 096 public void pushNestedPath(String subPath) { 097 this.bindingResult.pushNestedPath(subPath); 098 } 099 100 @Override 101 public void popNestedPath() throws IllegalStateException { 102 this.bindingResult.popNestedPath(); 103 } 104 105 106 @Override 107 public void reject(String errorCode) { 108 this.bindingResult.reject(errorCode); 109 } 110 111 @Override 112 public void reject(String errorCode, String defaultMessage) { 113 this.bindingResult.reject(errorCode, defaultMessage); 114 } 115 116 @Override 117 public void reject(String errorCode, Object[] errorArgs, String defaultMessage) { 118 this.bindingResult.reject(errorCode, errorArgs, defaultMessage); 119 } 120 121 @Override 122 public void rejectValue(String field, String errorCode) { 123 this.bindingResult.rejectValue(field, errorCode); 124 } 125 126 @Override 127 public void rejectValue(String field, String errorCode, String defaultMessage) { 128 this.bindingResult.rejectValue(field, errorCode, defaultMessage); 129 } 130 131 @Override 132 public void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage) { 133 this.bindingResult.rejectValue(field, errorCode, errorArgs, defaultMessage); 134 } 135 136 @Override 137 public void addAllErrors(Errors errors) { 138 this.bindingResult.addAllErrors(errors); 139 } 140 141 142 @Override 143 public boolean hasErrors() { 144 return this.bindingResult.hasErrors(); 145 } 146 147 @Override 148 public int getErrorCount() { 149 return this.bindingResult.getErrorCount(); 150 } 151 152 @Override 153 public List<ObjectError> getAllErrors() { 154 return this.bindingResult.getAllErrors(); 155 } 156 157 @Override 158 public boolean hasGlobalErrors() { 159 return this.bindingResult.hasGlobalErrors(); 160 } 161 162 @Override 163 public int getGlobalErrorCount() { 164 return this.bindingResult.getGlobalErrorCount(); 165 } 166 167 @Override 168 public List<ObjectError> getGlobalErrors() { 169 return this.bindingResult.getGlobalErrors(); 170 } 171 172 @Override 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 public FieldError getFieldError() { 194 return this.bindingResult.getFieldError(); 195 } 196 197 @Override 198 public boolean hasFieldErrors(String field) { 199 return this.bindingResult.hasFieldErrors(field); 200 } 201 202 @Override 203 public int getFieldErrorCount(String field) { 204 return this.bindingResult.getFieldErrorCount(field); 205 } 206 207 @Override 208 public List<FieldError> getFieldErrors(String field) { 209 return this.bindingResult.getFieldErrors(field); 210 } 211 212 @Override 213 public FieldError getFieldError(String field) { 214 return this.bindingResult.getFieldError(field); 215 } 216 217 @Override 218 public Object getFieldValue(String field) { 219 return this.bindingResult.getFieldValue(field); 220 } 221 222 @Override 223 public Class<?> getFieldType(String field) { 224 return this.bindingResult.getFieldType(field); 225 } 226 227 @Override 228 public Object getTarget() { 229 return this.bindingResult.getTarget(); 230 } 231 232 @Override 233 public Map<String, Object> getModel() { 234 return this.bindingResult.getModel(); 235 } 236 237 @Override 238 public Object getRawFieldValue(String field) { 239 return this.bindingResult.getRawFieldValue(field); 240 } 241 242 @Override 243 @SuppressWarnings("rawtypes") 244 public PropertyEditor findEditor(String field, Class valueType) { 245 return this.bindingResult.findEditor(field, valueType); 246 } 247 248 @Override 249 public PropertyEditorRegistry getPropertyEditorRegistry() { 250 return this.bindingResult.getPropertyEditorRegistry(); 251 } 252 253 @Override 254 public void addError(ObjectError error) { 255 this.bindingResult.addError(error); 256 } 257 258 @Override 259 public String[] resolveMessageCodes(String errorCode) { 260 return this.bindingResult.resolveMessageCodes(errorCode); 261 } 262 263 @Override 264 public String[] resolveMessageCodes(String errorCode, String field) { 265 return this.bindingResult.resolveMessageCodes(errorCode, field); 266 } 267 268 @Override 269 public void recordSuppressedField(String field) { 270 this.bindingResult.recordSuppressedField(field); 271 } 272 273 @Override 274 public String[] getSuppressedFields() { 275 return this.bindingResult.getSuppressedFields(); 276 } 277 278 279 /** 280 * Returns diagnostic information about the errors held in this object. 281 */ 282 @Override 283 public String getMessage() { 284 return this.bindingResult.toString(); 285 } 286 287 @Override 288 public boolean equals(Object other) { 289 return (this == other || this.bindingResult.equals(other)); 290 } 291 292 @Override 293 public int hashCode() { 294 return this.bindingResult.hashCode(); 295 } 296 297}