01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.validation;
18:
19: import org.springframework.beans.ConfigurablePropertyAccessor;
20: import org.springframework.beans.DirectFieldAccessor;
21: import org.springframework.util.Assert;
22:
23: /**
24: * Special implementation of the Errors and BindingResult interfaces,
25: * supporting registration and evaluation of binding errors on value objects.
26: * Performs direct field access instead of going through JavaBean getters.
27: *
28: * <p>This implementation just supports fields in the actual target object.
29: * It is not able to traverse nested fields.
30: *
31: * @author Juergen Hoeller
32: * @since 2.0
33: * @see DataBinder#getBindingResult()
34: * @see DataBinder#initDirectFieldAccess()
35: * @see BeanPropertyBindingResult
36: */
37: public class DirectFieldBindingResult extends
38: AbstractPropertyBindingResult {
39:
40: private final Object target;
41:
42: private transient DirectFieldAccessor directFieldAccessor;
43:
44: /**
45: * Create a new DirectFieldBindingResult instance.
46: * @param target the target object to bind onto
47: * @param objectName the name of the target object
48: */
49: public DirectFieldBindingResult(Object target, String objectName) {
50: super (objectName);
51: Assert.notNull(target, "Target bean must not be null");
52: this .target = target;
53: }
54:
55: public final Object getTarget() {
56: return target;
57: }
58:
59: /**
60: * Returns the DirectFieldAccessor that this instance uses.
61: * Creates a new one if none existed before.
62: * @see #createDirectFieldAccessor()
63: */
64: public final ConfigurablePropertyAccessor getPropertyAccessor() {
65: if (this .directFieldAccessor == null) {
66: this .directFieldAccessor = createDirectFieldAccessor();
67: }
68: return this .directFieldAccessor;
69: }
70:
71: /**
72: * Create a new DirectFieldAccessor for the underlying target object.
73: * @see #getTarget()
74: */
75: protected DirectFieldAccessor createDirectFieldAccessor() {
76: return new DirectFieldAccessor(getTarget());
77: }
78:
79: }
|