📄 beanwrapperimpl.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Default implementation of the BeanWrapper interface that should be sufficient
* for all typical use cases. Caches introspection results for efficiency.
*
* <p>Note: This class never tries to load a class by name, as this can pose
* class loading problems in J2EE applications with multiple deployment modules.
* The caller is responsible for loading a target class.
*
* <p>Note: Auto-registers default property editors from the
* <code>org.springframework.beans.propertyeditors</code> package, which apply
* in addition to the JDK's standard PropertyEditors. Applications can call
* the <code>registerCustomEditor</code> method to register an editor for a
* particular instance (i.e. they're not shared across the application).
* See the base class PropertyEditorRegistrySupport for details.
*
* <p>BeanWrapperImpl will convert collection and array values to the
* corresponding target collections or arrays, if necessary. Custom property
* editors that deal with collections or arrays can either be written via
* PropertyEditor's <code>setValue</code>, or against a comma-delimited String
* via <code>setAsText</code>, as String arrays are converted in such a format
* if the array itself is not assignable.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rob Harrop
* @since 15 April 2001
* @see #registerCustomEditor
* @see #setPropertyValues
* @see #setPropertyValue
* @see #getPropertyValue
* @see #getPropertyType
* @see BeanWrapper
* @see PropertyEditorRegistrySupport
*/
public class BeanWrapperImpl extends PropertyEditorRegistrySupport implements BeanWrapper {
/**
* We'll create a lot of these objects, so we don't want a new logger every time
*/
private static final Log logger = LogFactory.getLog(BeanWrapperImpl.class);
/** The wrapped object */
private Object object;
private String nestedPath = "";
private Object rootObject;
private boolean extractOldValueForEditor = false;
/**
* Cached introspections results for this object, to prevent encountering
* the cost of JavaBeans introspection every time.
*/
private CachedIntrospectionResults cachedIntrospectionResults;
/**
* Map with cached nested BeanWrappers: nested path -> BeanWrapper instance.
*/
private Map nestedBeanWrappers;
/**
* Create new empty BeanWrapperImpl. Wrapped instance needs to be set afterwards.
* Registers default editors.
* @see #setWrappedInstance
*/
public BeanWrapperImpl() {
this(true);
}
/**
* Create new empty BeanWrapperImpl. Wrapped instance needs to be set afterwards.
* @param registerDefaultEditors whether to register default editors
* (can be suppressed if the BeanWrapper won't need any type conversion)
* @see #setWrappedInstance
*/
public BeanWrapperImpl(boolean registerDefaultEditors) {
if (registerDefaultEditors) {
registerDefaultEditors();
}
}
/**
* Create new BeanWrapperImpl for the given object.
* @param object object wrapped by this BeanWrapper
*/
public BeanWrapperImpl(Object object) {
this();
setWrappedInstance(object);
}
/**
* Create new BeanWrapperImpl, wrapping a new instance of the specified class.
* @param clazz class to instantiate and wrap
*/
public BeanWrapperImpl(Class clazz) {
this();
setWrappedInstance(BeanUtils.instantiateClass(clazz));
}
/**
* Create new BeanWrapperImpl for the given object,
* registering a nested path that the object is in.
* @param object object wrapped by this BeanWrapper
* @param nestedPath the nested path of the object
* @param rootObject the root object at the top of the path
*/
public BeanWrapperImpl(Object object, String nestedPath, Object rootObject) {
this();
setWrappedInstance(object, nestedPath, rootObject);
}
/**
* Create new BeanWrapperImpl for the given object,
* registering a nested path that the object is in.
* @param object object wrapped by this BeanWrapper
* @param nestedPath the nested path of the object
* @param superBw the containing BeanWrapper (must not be <code>null</code>)
*/
private BeanWrapperImpl(Object object, String nestedPath, BeanWrapper superBw) {
setWrappedInstance(object, nestedPath, superBw.getWrappedInstance());
setExtractOldValueForEditor(superBw.isExtractOldValueForEditor());
}
//---------------------------------------------------------------------
// Implementation of BeanWrapper interface
//---------------------------------------------------------------------
/**
* Switch the target object, replacing the cached introspection results only
* if the class of the new object is different to that of the replaced object.
* @param object new target
*/
public void setWrappedInstance(Object object) {
setWrappedInstance(object, "", null);
}
/**
* Switch the target object, replacing the cached introspection results only
* if the class of the new object is different to that of the replaced object.
* @param object new target
* @param nestedPath the nested path of the object
* @param rootObject the root object at the top of the path
*/
public void setWrappedInstance(Object object, String nestedPath, Object rootObject) {
Assert.notNull(object, "Bean object must not be null");
this.object = object;
this.nestedPath = (nestedPath != null ? nestedPath : "");
this.rootObject = (!"".equals(this.nestedPath) ? rootObject : object);
this.nestedBeanWrappers = null;
setIntrospectionClass(object.getClass());
}
public Object getWrappedInstance() {
return this.object;
}
public Class getWrappedClass() {
return this.object.getClass();
}
/**
* Return the nested path of the object wrapped by this BeanWrapper.
*/
public String getNestedPath() {
return this.nestedPath;
}
/**
* Return the root object at the top of the path of this BeanWrapper.
* @see #getNestedPath
*/
public Object getRootInstance() {
return this.rootObject;
}
/**
* Return the class of the root object at the top of the path of this BeanWrapper.
* @see #getNestedPath
*/
public Class getRootClass() {
return (this.rootObject != null ? this.rootObject.getClass() : null);
}
/**
* Set the class to introspect.
* Needs to be called when the target object changes.
* @param clazz the class to introspect
*/
protected void setIntrospectionClass(Class clazz) {
if (this.cachedIntrospectionResults == null ||
!this.cachedIntrospectionResults.getBeanClass().equals(clazz)) {
this.cachedIntrospectionResults = CachedIntrospectionResults.forClass(clazz);
}
}
public void setExtractOldValueForEditor(boolean extractOldValueForEditor) {
this.extractOldValueForEditor = extractOldValueForEditor;
}
public boolean isExtractOldValueForEditor() {
return extractOldValueForEditor;
}
public PropertyDescriptor[] getPropertyDescriptors() {
return this.cachedIntrospectionResults.getBeanInfo().getPropertyDescriptors();
}
public PropertyDescriptor getPropertyDescriptor(String propertyName) throws BeansException {
Assert.notNull(propertyName, "Property name must not be null");
PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
if (pd != null) {
return pd;
}
else {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"No property '" + propertyName + "' found");
}
}
/**
* Internal version of getPropertyDescriptor:
* Returns null if not found rather than throwing an exception.
*/
protected PropertyDescriptor getPropertyDescriptorInternal(String propertyName) throws BeansException {
Assert.state(this.object != null, "BeanWrapper does not hold a bean instance");
BeanWrapperImpl nestedBw = getBeanWrapperForPropertyPath(propertyName);
return nestedBw.cachedIntrospectionResults.getPropertyDescriptor(getFinalPath(nestedBw, propertyName));
}
public boolean isReadableProperty(String propertyName) {
Assert.notNull(propertyName, "Property name must not be null");
try {
PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
if (pd != null) {
if (pd.getReadMethod() != null) {
return true;
}
}
else {
// Maybe an indexed/mapped property...
getPropertyValue(propertyName);
return true;
}
}
catch (InvalidPropertyException ex) {
// Cannot be evaluated, so can't be readable.
}
return false;
}
public boolean isWritableProperty(String propertyName) {
Assert.notNull(propertyName, "Property name must not be null");
try {
PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
if (pd != null) {
if (pd.getWriteMethod() != null) {
return true;
}
}
else {
// Maybe an indexed/mapped property...
getPropertyValue(propertyName);
return true;
}
}
catch (InvalidPropertyException ex) {
// Cannot be evaluated, so can't be writable.
}
return false;
}
public Class getPropertyType(String propertyName) throws BeansException {
try {
PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
if (pd != null) {
return pd.getPropertyType();
}
else {
// Maybe an indexed/mapped property...
Object value = getPropertyValue(propertyName);
if (value != null) {
return value.getClass();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -