001/* 002 * Copyright 2002-2018 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 org.springframework.beans.ConfigurablePropertyAccessor; 020import org.springframework.beans.PropertyAccessorFactory; 021 022/** 023 * Special implementation of the Errors and BindingResult interfaces, 024 * supporting registration and evaluation of binding errors on value objects. 025 * Performs direct field access instead of going through JavaBean getters. 026 * 027 * <p>Since Spring 4.1 this implementation is able to traverse nested fields. 028 * 029 * @author Juergen Hoeller 030 * @since 2.0 031 * @see DataBinder#getBindingResult() 032 * @see DataBinder#initDirectFieldAccess() 033 * @see BeanPropertyBindingResult 034 */ 035@SuppressWarnings("serial") 036public class DirectFieldBindingResult extends AbstractPropertyBindingResult { 037 038 private final Object target; 039 040 private final boolean autoGrowNestedPaths; 041 042 private transient ConfigurablePropertyAccessor directFieldAccessor; 043 044 045 /** 046 * Create a new DirectFieldBindingResult instance. 047 * @param target the target object to bind onto 048 * @param objectName the name of the target object 049 */ 050 public DirectFieldBindingResult(Object target, String objectName) { 051 this(target, objectName, true); 052 } 053 054 /** 055 * Create a new DirectFieldBindingResult instance. 056 * @param target the target object to bind onto 057 * @param objectName the name of the target object 058 * @param autoGrowNestedPaths whether to "auto-grow" a nested path that contains a null value 059 */ 060 public DirectFieldBindingResult(Object target, String objectName, boolean autoGrowNestedPaths) { 061 super(objectName); 062 this.target = target; 063 this.autoGrowNestedPaths = autoGrowNestedPaths; 064 } 065 066 067 @Override 068 public final Object getTarget() { 069 return this.target; 070 } 071 072 /** 073 * Returns the DirectFieldAccessor that this instance uses. 074 * Creates a new one if none existed before. 075 * @see #createDirectFieldAccessor() 076 */ 077 @Override 078 public final ConfigurablePropertyAccessor getPropertyAccessor() { 079 if (this.directFieldAccessor == null) { 080 this.directFieldAccessor = createDirectFieldAccessor(); 081 this.directFieldAccessor.setExtractOldValueForEditor(true); 082 this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths); 083 } 084 return this.directFieldAccessor; 085 } 086 087 /** 088 * Create a new DirectFieldAccessor for the underlying target object. 089 * @see #getTarget() 090 */ 091 protected ConfigurablePropertyAccessor createDirectFieldAccessor() { 092 if (this.target == null) { 093 throw new IllegalStateException("Cannot access fields on null target instance '" + getObjectName() + "'"); 094 } 095 return PropertyAccessorFactory.forDirectFieldAccess(this.target); 096 } 097 098}