📄 propertyeditorregistrysupport.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.PropertyEditor;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor;
import org.springframework.beans.propertyeditors.CharacterEditor;
import org.springframework.beans.propertyeditors.ClassEditor;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.FileEditor;
import org.springframework.beans.propertyeditors.InputStreamEditor;
import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.beans.propertyeditors.URLEditor;
import org.springframework.core.CollectionFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceArrayPropertyEditor;
/**
* Base implementation of the PropertyEditorRegistry interface.
* Provides management of default editors and custom editors.
* Mainly serves as base class for BeanWrapperImpl.
*
* @author Juergen Hoeller
* @since 1.2.6
* @see PropertyEditorRegistry
* @see BeanWrapperImpl
* @see java.beans.PropertyEditorManager
* @see java.beans.PropertyEditorSupport#setAsText
* @see java.beans.PropertyEditorSupport#setValue
*/
public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
private Map defaultEditors;
private Map customEditors;
private Map customEditorCache;
//---------------------------------------------------------------------
// Management of default editors
//---------------------------------------------------------------------
/**
* Register default editors in this instance, for restricted environments.
* We're not using the JRE's PropertyEditorManager to avoid potential
* SecurityExceptions when running in a SecurityManager.
* <p>Registers a <code>CustomNumberEditor</code> for all primitive number types,
* their corresponding wrapper types, <code>BigInteger</code> and <code>BigDecimal</code>.
* @see org.springframework.beans.propertyeditors.ByteArrayPropertyEditor
* @see org.springframework.beans.propertyeditors.ClassEditor
* @see org.springframework.beans.propertyeditors.CharacterEditor
* @see org.springframework.beans.propertyeditors.CustomBooleanEditor
* @see org.springframework.beans.propertyeditors.CustomNumberEditor
* @see org.springframework.beans.propertyeditors.CustomCollectionEditor
* @see org.springframework.beans.propertyeditors.FileEditor
* @see org.springframework.beans.propertyeditors.InputStreamEditor
* @see org.springframework.jndi.JndiTemplateEditor
* @see org.springframework.beans.propertyeditors.LocaleEditor
* @see org.springframework.beans.propertyeditors.PropertiesEditor
* @see org.springframework.beans.PropertyValuesEditor
* @see org.springframework.core.io.support.ResourceArrayPropertyEditor
* @see org.springframework.core.io.ResourceEditor
* @see org.springframework.beans.propertyeditors.StringArrayPropertyEditor
* @see org.springframework.transaction.interceptor.TransactionAttributeEditor
* @see org.springframework.transaction.interceptor.TransactionAttributeSourceEditor
* @see org.springframework.beans.propertyeditors.URLEditor
*/
protected void registerDefaultEditors() {
this.defaultEditors = new HashMap(32);
// Simple editors, without parameterization capabilities.
// The JDK does not contain a default editor for any of these target types.
this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
this.defaultEditors.put(Locale.class, new LocaleEditor());
this.defaultEditors.put(Properties.class, new PropertiesEditor());
this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
this.defaultEditors.put(String[].class, new StringArrayPropertyEditor());
this.defaultEditors.put(URL.class, new URLEditor());
// Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
// Default instances of character and boolean editors.
// Can be overridden by registering custom instances of those as custom editors.
PropertyEditor characterEditor = new CharacterEditor(false);
PropertyEditor booleanEditor = new CustomBooleanEditor(false);
// The JDK does not contain a default editor for char!
this.defaultEditors.put(char.class, characterEditor);
this.defaultEditors.put(Character.class, characterEditor);
// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
this.defaultEditors.put(boolean.class, booleanEditor);
this.defaultEditors.put(Boolean.class, booleanEditor);
// The JDK does not contain default editors for number wrapper types!
// Override JDK primitive number editors with our own CustomNumberEditor.
PropertyEditor byteEditor = new CustomNumberEditor(Byte.class, false);
PropertyEditor shortEditor = new CustomNumberEditor(Short.class, false);
PropertyEditor integerEditor = new CustomNumberEditor(Integer.class, false);
PropertyEditor longEditor = new CustomNumberEditor(Long.class, false);
PropertyEditor floatEditor = new CustomNumberEditor(Float.class, false);
PropertyEditor doubleEditor = new CustomNumberEditor(Double.class, false);
this.defaultEditors.put(byte.class, byteEditor);
this.defaultEditors.put(Byte.class, byteEditor);
this.defaultEditors.put(short.class, shortEditor);
this.defaultEditors.put(Short.class, shortEditor);
this.defaultEditors.put(int.class, integerEditor);
this.defaultEditors.put(Integer.class, integerEditor);
this.defaultEditors.put(long.class, longEditor);
this.defaultEditors.put(Long.class, longEditor);
this.defaultEditors.put(float.class, floatEditor);
this.defaultEditors.put(Float.class, floatEditor);
this.defaultEditors.put(double.class, doubleEditor);
this.defaultEditors.put(Double.class, doubleEditor);
this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, false));
this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, false));
}
/**
* Retrieve the default editor for the given property type, if any.
* @param requiredType type of the property
* @return the default editor, or <code>null</code> if none found
*/
protected PropertyEditor getDefaultEditor(Class requiredType) {
if (this.defaultEditors == null) {
return null;
}
return (PropertyEditor) this.defaultEditors.get(requiredType);
}
/**
* Copy the default editors registered in this instance to the given target registry.
* @param target the target registry to copy to
*/
protected void copyDefaultEditorsTo(PropertyEditorRegistrySupport target) {
target.defaultEditors = this.defaultEditors;
}
//---------------------------------------------------------------------
// Management of custom editors
//---------------------------------------------------------------------
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
registerCustomEditor(requiredType, null, propertyEditor);
}
public void registerCustomEditor(Class requiredType, String propertyPath, PropertyEditor propertyEditor) {
if (requiredType == null && propertyPath == null) {
throw new IllegalArgumentException("Either requiredType or propertyPath is required");
}
if (this.customEditors == null) {
this.customEditors = CollectionFactory.createLinkedMapIfPossible(16);
}
if (propertyPath != null) {
this.customEditors.put(propertyPath, new CustomEditorHolder(propertyEditor, requiredType));
}
else {
this.customEditors.put(requiredType, propertyEditor);
this.customEditorCache = null;
}
}
public PropertyEditor findCustomEditor(Class requiredType, String propertyPath) {
if (this.customEditors == null) {
return null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -