一、前言
关于apache的commons-beanutils包中的org.apache.commons.beanutils.BeanUtils工具类,通过org.apache.commons.beanutils;BeanUtilsBean类对常用bean对象间的复制同步copyProperties、转为Map格式describe、获取属性为数组内容getArrayProperty、获取getIndexedProperty第一属性内容、指定属性内容getProperty等操作内容。
二、源码说明
1.BeanUtils类-其中FastHashMap参考其他文章
package org.apache.commons.beanutils;@b@@b@import java.lang.reflect.InvocationTargetException;@b@import java.util.Map;@b@import org.apache.commons.collections.FastHashMap;@b@@b@public class BeanUtils@b@{@b@ private static FastHashMap dummy = new FastHashMap();@b@@b@ /**@b@ * @deprecated@b@ */@b@ private static int debug = 0;@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static int getDebug()@b@ {@b@ return debug;@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static void setDebug(int newDebug)@b@ {@b@ debug = newDebug;@b@ }@b@@b@ public static Object cloneBean(Object bean)@b@ throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().cloneBean(bean);@b@ }@b@@b@ public static void copyProperties(Object dest, Object orig)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ BeanUtilsBean.getInstance().copyProperties(dest, orig);@b@ }@b@@b@ public static void copyProperty(Object bean, String name, Object value)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ BeanUtilsBean.getInstance().copyProperty(bean, name, value);@b@ }@b@@b@ public static Map describe(Object bean)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().describe(bean);@b@ }@b@@b@ public static String[] getArrayProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getArrayProperty(bean, name);@b@ }@b@@b@ public static String getIndexedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getIndexedProperty(bean, name);@b@ }@b@@b@ public static String getIndexedProperty(Object bean, String name, int index)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getIndexedProperty(bean, name, index);@b@ }@b@@b@ public static String getMappedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getMappedProperty(bean, name);@b@ }@b@@b@ public static String getMappedProperty(Object bean, String name, String key)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getMappedProperty(bean, name, key);@b@ }@b@@b@ public static String getNestedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getNestedProperty(bean, name);@b@ }@b@@b@ public static String getProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getProperty(bean, name);@b@ }@b@@b@ public static String getSimpleProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return BeanUtilsBean.getInstance().getSimpleProperty(bean, name);@b@ }@b@@b@ public static void populate(Object bean, Map properties)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ BeanUtilsBean.getInstance().populate(bean, properties);@b@ }@b@@b@ public static void setProperty(Object bean, String name, Object value)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ BeanUtilsBean.getInstance().setProperty(bean, name, value);@b@ }@b@}
2.BeanUtilsBean依赖类
package org.apache.commons.beanutils;@b@@b@import java.beans.IndexedPropertyDescriptor;@b@import java.beans.PropertyDescriptor;@b@import java.lang.reflect.Array;@b@import java.lang.reflect.InvocationTargetException;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Set;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public class BeanUtilsBean@b@{@b@ private static final ContextClassLoaderLocal beansByClassLoader = new ContextClassLoaderLocal()@b@ {@b@ protected Object initialValue() {@b@ return new BeanUtilsBean();@b@ }@b@ };@b@ private Log log;@b@ private ConvertUtilsBean convertUtilsBean;@b@ private PropertyUtilsBean propertyUtilsBean;@b@@b@ public static synchronized BeanUtilsBean getInstance()@b@ {@b@ return ((BeanUtilsBean)beansByClassLoader.get());@b@ }@b@@b@ public static synchronized void setInstance(BeanUtilsBean newInstance)@b@ {@b@ beansByClassLoader.set(newInstance);@b@ }@b@@b@ public BeanUtilsBean()@b@ {@b@ this(new ConvertUtilsBean(), new PropertyUtilsBean());@b@ }@b@@b@ public BeanUtilsBean(ConvertUtilsBean convertUtilsBean, PropertyUtilsBean propertyUtilsBean)@b@ {@b@ this.log = LogFactory.getLog(BeanUtils.class);@b@@b@ this.convertUtilsBean = convertUtilsBean;@b@ this.propertyUtilsBean = propertyUtilsBean;@b@ }@b@@b@ public Object cloneBean(Object bean)@b@ throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException@b@ {@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("Cloning bean: " + bean.getClass().getName());@b@@b@ Class clazz = bean.getClass();@b@ Object newBean = null;@b@ if (bean instanceof DynaBean)@b@ newBean = ((DynaBean)bean).getDynaClass().newInstance();@b@ else@b@ newBean = bean.getClass().newInstance();@b@@b@ getPropertyUtils().copyProperties(newBean, bean);@b@ return newBean;@b@ }@b@@b@ public void copyProperties(Object dest, Object orig)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ int i;@b@ String name;@b@ Object value;@b@ if (dest == null) {@b@ throw new IllegalArgumentException("No destination bean specified");@b@ }@b@@b@ if (orig == null)@b@ throw new IllegalArgumentException("No origin bean specified");@b@@b@ if (this.log.isDebugEnabled()) {@b@ this.log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")");@b@ }@b@@b@ if (orig instanceof DynaBean) {@b@ DynaProperty[] origDescriptors = ((DynaBean)orig).getDynaClass().getDynaProperties();@b@@b@ for (i = 0; i < origDescriptors.length; ++i) {@b@ name = origDescriptors[i].getName();@b@ if (getPropertyUtils().isWriteable(dest, name)) {@b@ value = ((DynaBean)orig).get(name);@b@ copyProperty(dest, name, value);@b@ }@b@ }@b@ } else if (orig instanceof Map) {@b@ Iterator names = ((Map)orig).keySet().iterator();@b@ while (names.hasNext()) {@b@ String name = (String)names.next();@b@ if (getPropertyUtils().isWriteable(dest, name)) {@b@ Object value = ((Map)orig).get(name);@b@ copyProperty(dest, name, value);@b@ }@b@ }@b@ } else {@b@ PropertyDescriptor[] origDescriptors = getPropertyUtils().getPropertyDescriptors(orig);@b@@b@ for (i = 0; i < origDescriptors.length; ++i) {@b@ name = origDescriptors[i].getName();@b@ if ("class".equals(name))@b@ continue;@b@@b@ if ((getPropertyUtils().isReadable(orig, name)) && (getPropertyUtils().isWriteable(dest, name)))@b@ try@b@ {@b@ value = getPropertyUtils().getSimpleProperty(orig, name);@b@@b@ copyProperty(dest, name, value);@b@ }@b@ catch (NoSuchMethodException e)@b@ {@b@ }@b@ }@b@ }@b@ }@b@@b@ public void copyProperty(Object bean, String name, Object value)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ Converter converter;@b@ if (this.log.isTraceEnabled()) {@b@ StringBuffer sb = new StringBuffer(" copyProperty(");@b@ sb.append(bean);@b@ sb.append(", ");@b@ sb.append(name);@b@ sb.append(", ");@b@ if (value == null) {@b@ sb.append("<NULL>");@b@ } else if (value instanceof String) {@b@ sb.append((String)value);@b@ } else if (value instanceof String[]) {@b@ String[] values = (String[])value;@b@ sb.append('[');@b@ for (int i = 0; i < values.length; ++i) {@b@ if (i > 0)@b@ sb.append(',');@b@@b@ sb.append(values[i]);@b@ }@b@ sb.append(']');@b@ } else {@b@ sb.append(value.toString());@b@ }@b@ sb.append(')');@b@ this.log.trace(sb.toString());@b@ }@b@@b@ Object target = bean;@b@ int delim = name.lastIndexOf(46);@b@ if (delim >= 0) {@b@ try {@b@ target = getPropertyUtils().getProperty(bean, name.substring(0, delim));@b@ }@b@ catch (NoSuchMethodException e) {@b@ return;@b@ }@b@ name = name.substring(delim + 1);@b@ if (this.log.isTraceEnabled()) {@b@ this.log.trace(" Target bean = " + target);@b@ this.log.trace(" Target name = " + name);@b@ }@b@@b@ }@b@@b@ String propName = null;@b@ Class type = null;@b@ int index = -1;@b@ String key = null;@b@@b@ propName = name;@b@ int i = propName.indexOf(91);@b@ if (i >= 0) {@b@ int k = propName.indexOf(93);@b@ try {@b@ index = Integer.parseInt(propName.substring(i + 1, k));@b@ }@b@ catch (NumberFormatException e)@b@ {@b@ }@b@ propName = propName.substring(0, i);@b@ }@b@ int j = propName.indexOf(40);@b@ if (j >= 0) {@b@ int k = propName.indexOf(41);@b@ try {@b@ key = propName.substring(j + 1, k);@b@ }@b@ catch (IndexOutOfBoundsException e) {@b@ }@b@ propName = propName.substring(0, j);@b@ }@b@@b@ if (target instanceof DynaBean) {@b@ DynaClass dynaClass = ((DynaBean)target).getDynaClass();@b@ DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);@b@ if (dynaProperty == null)@b@ return;@b@@b@ type = dynaProperty.getType();@b@ } else {@b@ PropertyDescriptor descriptor = null;@b@ try {@b@ descriptor = getPropertyUtils().getPropertyDescriptor(target, name);@b@@b@ if (descriptor == null)@b@ return;@b@ }@b@ catch (NoSuchMethodException e) {@b@ return;@b@ }@b@ type = descriptor.getPropertyType();@b@ if (type == null)@b@ {@b@ if (this.log.isTraceEnabled()) {@b@ this.log.trace(" target type for property '" + propName + "' is null, so skipping ths setter");@b@ }@b@@b@ return;@b@ }@b@ }@b@ if (this.log.isTraceEnabled()) {@b@ this.log.trace(" target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);@b@ }@b@@b@ if (index >= 0) {@b@ converter = getConvertUtils().lookup(type.getComponentType());@b@ if (converter != null) {@b@ this.log.trace(" USING CONVERTER " + converter);@b@ value = converter.convert(type, value);@b@ }@b@ try {@b@ getPropertyUtils().setIndexedProperty(target, propName, index, value);@b@ }@b@ catch (NoSuchMethodException e) {@b@ throw new InvocationTargetException(e, "Cannot set " + propName);@b@ }@b@ }@b@ else if (key != null)@b@ {@b@ try@b@ {@b@ getPropertyUtils().setMappedProperty(target, propName, key, value);@b@ }@b@ catch (NoSuchMethodException e) {@b@ throw new InvocationTargetException(e, "Cannot set " + propName);@b@ }@b@ }@b@ else {@b@ converter = getConvertUtils().lookup(type);@b@ if (converter != null) {@b@ this.log.trace(" USING CONVERTER " + converter);@b@ value = converter.convert(type, value);@b@ }@b@ try {@b@ getPropertyUtils().setSimpleProperty(target, propName, value);@b@ } catch (NoSuchMethodException e) {@b@ throw new InvocationTargetException(e, "Cannot set " + propName);@b@ }@b@ }@b@ }@b@@b@ public Map describe(Object bean)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ int i;@b@ String name;@b@ if (bean == null)@b@ {@b@ return new HashMap();@b@ }@b@@b@ if (this.log.isDebugEnabled()) {@b@ this.log.debug("Describing bean: " + bean.getClass().getName());@b@ }@b@@b@ Map description = new HashMap();@b@ if (bean instanceof DynaBean) {@b@ DynaProperty[] descriptors = ((DynaBean)bean).getDynaClass().getDynaProperties();@b@@b@ for (i = 0; i < descriptors.length; ++i) {@b@ name = descriptors[i].getName();@b@ description.put(name, getProperty(bean, name));@b@ }@b@ } else {@b@ PropertyDescriptor[] descriptors = getPropertyUtils().getPropertyDescriptors(bean);@b@@b@ for (i = 0; i < descriptors.length; ++i) {@b@ name = descriptors[i].getName();@b@ if (descriptors[i].getReadMethod() != null)@b@ description.put(name, getProperty(bean, name));@b@ }@b@ }@b@ return description;@b@ }@b@@b@ public String[] getArrayProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getProperty(bean, name);@b@ if (value == null)@b@ return null;@b@ if (value instanceof Collection) {@b@ ArrayList values = new ArrayList();@b@ Iterator items = ((Collection)value).iterator();@b@ while (items.hasNext()) {@b@ Object item = items.next();@b@ if (item == null) {@b@ values.add((String)null);@b@ }@b@ else@b@ values.add(getConvertUtils().convert(item));@b@ }@b@@b@ return ((String[])values.toArray(new String[values.size()])); }@b@ if (value.getClass().isArray()) {@b@ int n = Array.getLength(value);@b@ String[] results = new String[n];@b@ for (int i = 0; i < n; ++i) {@b@ Object item = Array.get(value, i);@b@ if (item == null) {@b@ results[i] = null;@b@ }@b@ else@b@ results[i] = getConvertUtils().convert(item);@b@ }@b@@b@ return results;@b@ }@b@ String[] results = new String[1];@b@ results[0] = value.toString();@b@ return results;@b@ }@b@@b@ public String getIndexedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getIndexedProperty(bean, name);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public String getIndexedProperty(Object bean, String name, int index)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getIndexedProperty(bean, name, index);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public String getMappedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getMappedProperty(bean, name);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public String getMappedProperty(Object bean, String name, String key)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getMappedProperty(bean, name, key);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public String getNestedProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getNestedProperty(bean, name);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public String getProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ return getNestedProperty(bean, name);@b@ }@b@@b@ public String getSimpleProperty(Object bean, String name)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Object value = getPropertyUtils().getSimpleProperty(bean, name);@b@ return getConvertUtils().convert(value);@b@ }@b@@b@ public void populate(Object bean, Map properties)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ if ((bean == null) || (properties == null))@b@ return;@b@@b@ if (this.log.isDebugEnabled()) {@b@ this.log.debug("BeanUtils.populate(" + bean + ", " + properties + ")");@b@ }@b@@b@ Iterator names = properties.keySet().iterator();@b@ while (names.hasNext())@b@ {@b@ String name = (String)names.next();@b@ if (name == null)@b@ continue;@b@@b@ Object value = properties.get(name);@b@@b@ setProperty(bean, name, value);@b@ }@b@ }@b@@b@ public void setProperty(Object bean, String name, Object value)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ if (this.log.isTraceEnabled()) {@b@ StringBuffer sb = new StringBuffer(" setProperty(");@b@ sb.append(bean);@b@ sb.append(", ");@b@ sb.append(name);@b@ sb.append(", ");@b@ if (value == null) {@b@ sb.append("<NULL>");@b@ } else if (value instanceof String) {@b@ sb.append((String)value);@b@ } else if (value instanceof String[]) {@b@ String[] values = (String[])value;@b@ sb.append('[');@b@ for (int i = 0; i < values.length; ++i) {@b@ if (i > 0)@b@ sb.append(',');@b@@b@ sb.append(values[i]);@b@ }@b@ sb.append(']');@b@ } else {@b@ sb.append(value.toString());@b@ }@b@ sb.append(')');@b@ this.log.trace(sb.toString());@b@ }@b@@b@ Object target = bean;@b@ int delim = findLastNestedIndex(name);@b@ if (delim >= 0) {@b@ try {@b@ target = getPropertyUtils().getProperty(bean, name.substring(0, delim));@b@ }@b@ catch (NoSuchMethodException e) {@b@ return;@b@ }@b@ name = name.substring(delim + 1);@b@ if (this.log.isTraceEnabled()) {@b@ this.log.trace(" Target bean = " + target);@b@ this.log.trace(" Target name = " + name);@b@ }@b@@b@ }@b@@b@ String propName = null;@b@ Class type = null;@b@ int index = -1;@b@ String key = null;@b@@b@ propName = name;@b@ int i = propName.indexOf(91);@b@ if (i >= 0) {@b@ int k = propName.indexOf(93);@b@ try {@b@ index = Integer.parseInt(propName.substring(i + 1, k));@b@ }@b@ catch (NumberFormatException e)@b@ {@b@ }@b@ propName = propName.substring(0, i);@b@ }@b@ int j = propName.indexOf(40);@b@ if (j >= 0) {@b@ int k = propName.indexOf(41);@b@ try {@b@ key = propName.substring(j + 1, k);@b@ }@b@ catch (IndexOutOfBoundsException e) {@b@ }@b@ propName = propName.substring(0, j);@b@ }@b@@b@ if (target instanceof DynaBean) {@b@ DynaClass dynaClass = ((DynaBean)target).getDynaClass();@b@ DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);@b@ if (dynaProperty == null)@b@ return;@b@@b@ type = dynaProperty.getType();@b@ } else {@b@ PropertyDescriptor descriptor = null;@b@ try {@b@ descriptor = getPropertyUtils().getPropertyDescriptor(target, name);@b@@b@ if (descriptor == null)@b@ return;@b@ }@b@ catch (NoSuchMethodException e) {@b@ return;@b@ }@b@ if (descriptor instanceof MappedPropertyDescriptor) {@b@ if (((MappedPropertyDescriptor)descriptor).getMappedWriteMethod() == null) {@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("Skipping read-only property");@b@@b@ return;@b@ }@b@ type = ((MappedPropertyDescriptor)descriptor).getMappedPropertyType();@b@ }@b@ else if (descriptor instanceof IndexedPropertyDescriptor) {@b@ if (((IndexedPropertyDescriptor)descriptor).getIndexedWriteMethod() == null) {@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("Skipping read-only property");@b@@b@ return;@b@ }@b@ type = ((IndexedPropertyDescriptor)descriptor).getIndexedPropertyType();@b@ }@b@ else {@b@ if (descriptor.getWriteMethod() == null) {@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("Skipping read-only property");@b@@b@ return;@b@ }@b@ type = descriptor.getPropertyType();@b@ }@b@@b@ }@b@@b@ Object newValue = null;@b@ if ((type.isArray()) && (index < 0)) {@b@ String[] values;@b@ if (value == null) {@b@ values = new String[1];@b@ values[0] = ((String)value);@b@ newValue = getConvertUtils().convert((String[])values, type);@b@ } else if (value instanceof String) {@b@ values = new String[1];@b@ values[0] = ((String)value);@b@ newValue = getConvertUtils().convert((String[])values, type);@b@ } else if (value instanceof String[]) {@b@ newValue = getConvertUtils().convert((String[])value, type);@b@ } else {@b@ newValue = value;@b@ }@b@ } else if (type.isArray()) {@b@ if (value instanceof String) {@b@ newValue = getConvertUtils().convert((String)value, type.getComponentType());@b@ }@b@ else if (value instanceof String[]) {@b@ newValue = getConvertUtils().convert(((String[])value)[0], type.getComponentType());@b@ }@b@ else@b@ newValue = value;@b@@b@ }@b@ else if ((value instanceof String) || (value == null)) {@b@ newValue = getConvertUtils().convert((String)value, type);@b@ } else if (value instanceof String[]) {@b@ newValue = getConvertUtils().convert(((String[])value)[0], type);@b@ }@b@ else if (getConvertUtils().lookup(value.getClass()) != null) {@b@ newValue = getConvertUtils().convert(value.toString(), type);@b@ } else {@b@ newValue = value;@b@ }@b@@b@ try@b@ {@b@ if (index >= 0) {@b@ getPropertyUtils().setIndexedProperty(target, propName, index, newValue);@b@ }@b@ else if (key != null) {@b@ getPropertyUtils().setMappedProperty(target, propName, key, newValue);@b@ }@b@ else@b@ getPropertyUtils().setProperty(target, propName, newValue);@b@ }@b@ catch (NoSuchMethodException values) {@b@ throw new InvocationTargetException(e, "Cannot set " + propName);@b@ }@b@ }@b@@b@ private int findLastNestedIndex(String expression)@b@ {@b@ int bracketCount = 0;@b@ for (int i = expression.length() - 1; i >= 0; --i) {@b@ char at = expression.charAt(i);@b@ switch (at)@b@ {@b@ case '.':@b@ if (bracketCount < 1) {@b@ return i;@b@ }@b@@b@ case '(':@b@ case '[':@b@ --bracketCount;@b@ break;@b@ case ')':@b@ case ']':@b@ ++bracketCount;@b@ }@b@@b@ }@b@@b@ return -1;@b@ }@b@@b@ public ConvertUtilsBean getConvertUtils()@b@ {@b@ return this.convertUtilsBean;@b@ }@b@@b@ public PropertyUtilsBean getPropertyUtils()@b@ {@b@ return this.propertyUtilsBean;@b@ }@b@}
3.ContextClassLoaderLocal
package org.apache.commons.beanutils;@b@@b@import java.util.Map;@b@import java.util.WeakHashMap;@b@@b@public class ContextClassLoaderLocal@b@{@b@ private Map valueByClassLoader = new WeakHashMap();@b@ private boolean globalValueInitialized = false;@b@ private Object globalValue;@b@@b@ protected Object initialValue()@b@ {@b@ return null;@b@ }@b@@b@ public synchronized Object get()@b@ {@b@ this.valueByClassLoader.isEmpty();@b@ try@b@ {@b@ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();@b@ if (contextClassLoader != null)@b@ {@b@ Object value = this.valueByClassLoader.get(contextClassLoader);@b@ if ((value == null) && (!(this.valueByClassLoader.containsKey(contextClassLoader))))@b@ {@b@ value = initialValue();@b@ this.valueByClassLoader.put(contextClassLoader, value);@b@ }@b@ return value;@b@ }@b@ }@b@ catch (SecurityException e)@b@ {@b@ }@b@@b@ if (!(this.globalValueInitialized)) {@b@ this.globalValue = initialValue();@b@ this.globalValueInitialized = true;@b@ }@b@ return this.globalValue;@b@ }@b@@b@ public synchronized void set(Object value)@b@ {@b@ this.valueByClassLoader.isEmpty();@b@ try@b@ {@b@ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();@b@ if (contextClassLoader != null) {@b@ this.valueByClassLoader.put(contextClassLoader, value);@b@ return;@b@ }@b@ }@b@ catch (SecurityException e)@b@ {@b@ }@b@ this.globalValue = value;@b@ this.globalValueInitialized = true;@b@ }@b@@b@ public synchronized void unset()@b@ {@b@ ClassLoader contextClassLoader;@b@ try@b@ {@b@ contextClassLoader = Thread.currentThread().getContextClassLoader();@b@ unset(contextClassLoader);@b@ }@b@ catch (SecurityException e)@b@ {@b@ }@b@ }@b@@b@ public synchronized void unset(ClassLoader classLoader)@b@ {@b@ this.valueByClassLoader.remove(classLoader);@b@ }@b@}