国外网站打开速度慢的原因,c 做网站需要什么知识,北京网站建设推,网页设计教程 表单一、前言基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateClass、class对象方法名称methodName查找findMethod、属性查找对于class类信息findPropertyType、对象属性复制copyProperties等常用操作#xff0c;具…一、前言基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateClass、class对象方法名称methodName查找findMethod、属性查找对于class类信息findPropertyType、对象属性复制copyProperties等常用操作具体如下源码所示。二、源码说明package org.springframework.beans;bbimport java.beans.PropertyDescriptor;bimport java.beans.PropertyEditor;bimport java.lang.reflect.Constructor;bimport java.lang.reflect.InvocationTargetException;bimport java.lang.reflect.Method;bimport java.lang.reflect.Modifier;bimport java.net.URI;bimport java.net.URL;bimport java.util.Arrays;bimport java.util.Collections;bimport java.util.Date;bimport java.util.List;bimport java.util.Locale;bimport java.util.Set;bimport org.apache.commons.logging.Log;bimport org.apache.commons.logging.LogFactory;bimport org.springframework.core.MethodParameter;bimport org.springframework.util.Assert;bimport org.springframework.util.ClassUtils;bimport org.springframework.util.ConcurrentReferenceHashMap;bimport org.springframework.util.ReflectionUtils;bimport org.springframework.util.StringUtils;bbpublic abstract class BeanUtilsb{b private static final Log logger LogFactory.getLog(BeanUtils.class);b private static final Set unknownEditorTypes Collections.newSetFromMap(new ConcurrentReferenceHashMap(64));bb public static T instantiate(Class clazz)b throws BeanInstantiationExceptionb {b Assert.notNull(clazz, Class must not be null);b if (clazz.isInterface())b throw new BeanInstantiationException(clazz, Specified class is an interface);b tryb {b return clazz.newInstance();b }b catch (InstantiationException ex) {b throw new BeanInstantiationException(clazz, Is it an abstract class?, ex);b }b catch (IllegalAccessException ex) {b throw new BeanInstantiationException(clazz, Is the constructor accessible?, ex);b }b }bb public static T instantiateClass(Class clazz)b throws BeanInstantiationExceptionb {b Assert.notNull(clazz, Class must not be null);b if (clazz.isInterface())b throw new BeanInstantiationException(clazz, Specified class is an interface);b tryb {b return instantiateClass(clazz.getDeclaredConstructor(new Class[0]), new Object[0]);b }b catch (NoSuchMethodException ex) {b throw new BeanInstantiationException(clazz, No default constructor found, ex);b }b }bb public static T instantiateClass(Class clazz, Class assignableTo)b throws BeanInstantiationExceptionb {b Assert.isAssignable(assignableTo, clazz);b return instantiateClass(clazz);b }bb public static T instantiateClass(Constructor ctor, Object[] args)b throws BeanInstantiationExceptionb {b Assert.notNull(ctor, Constructor must not be null);b try {b ReflectionUtils.makeAccessible(ctor);b return ctor.newInstance(args);b }b catch (InstantiationException ex) {b throw new BeanInstantiationException(ctor.getDeclaringClass(), Is it an abstract class?, ex);b }b catch (IllegalAccessException ex)b {b throw new BeanInstantiationException(ctor.getDeclaringClass(), Is the constructor accessible?, ex);b }b catch (IllegalArgumentException ex)b {b throw new BeanInstantiationException(ctor.getDeclaringClass(), Illegal arguments for constructor, ex);b }b catch (InvocationTargetException ex)b {b throw new BeanInstantiationException(ctor.getDeclaringClass(), Constructor threw exception, exb .getTargetException());b }b }bb public static Method findMethod(Class clazz, String methodName, Class[] paramTypes)b {b tryb {b return clazz.getMethod(methodName, paramTypes);b } catch (NoSuchMethodException ex) {b }b return findDeclaredMethod(clazz, methodName, paramTypes);b }bb public static Method findDeclaredMethod(Class clazz, String methodName, Class[] paramTypes)b {b tryb {b return clazz.getDeclaredMethod(methodName, paramTypes);b }b catch (NoSuchMethodException ex) {b if (clazz.getSuperclass() ! null)b return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);b }b return null;b }bb public static Method findMethodWithMinimalParameters(Class clazz, String methodName)b throws IllegalArgumentExceptionb {b Method targetMethod findMethodWithMinimalParameters(clazz.getMethods(), methodName);b if (targetMethod null)b targetMethod findDeclaredMethodWithMinimalParameters(clazz, methodName);bb return targetMethod;b }bb public static Method findDeclaredMethodWithMinimalParameters(Class clazz, String methodName)b throws IllegalArgumentExceptionb {b Method targetMethod findMethodWithMinimalParameters(clazz.getDeclaredMethods(), methodName);b if ((targetMethod null) (clazz.getSuperclass() ! null))b targetMethod findDeclaredMethodWithMinimalParameters(clazz.getSuperclass(), methodName);bb return targetMethod;b }bb public static Method findMethodWithMinimalParameters(Method[] methods, String methodName)b throws IllegalArgumentExceptionb {b Method targetMethod null;b int numMethodsFoundWithCurrentMinimumArgs 0;b Method[] arrayOfMethod methods; int i arrayOfMethod.length; for (int j 0; j 1) {b throw new IllegalArgumentException(Cannot resolve method methodName to a unique method. Attempted to resolve to overloaded method with the least number of parameters, but there were numMethodsFoundWithCurrentMinimumArgs candidates.);b }bb return targetMethod;b }bb public static Method resolveSignature(String signature, Class clazz)b {b Assert.hasText(signature, signature must not be empty);b Assert.notNull(clazz, Class must not be null);b int firstParen signature.indexOf(();b int lastParen signature.indexOf());b if ((firstParen -1) (lastParen -1)) {b throw new IllegalArgumentException(Invalid method signature signature : expected closing ) for args list);b }bb if ((lastParen -1) (firstParen -1)) {b throw new IllegalArgumentException(Invalid method signature signature : expected opening ( for args list);b }bb if ((firstParen -1) (lastParen -1)) {b return findMethodWithMinimalParameters(clazz, signature);b }bb String methodName signature.substring(0, firstParen);bb String[] parameterTypeNames StringUtils.commaDelimitedListToStringArray(signatureb .substring(firstParen 1, lastParen));bb Class[] parameterTypes new Class[parameterTypeNames.length];b for (int i 0; i clazz)b throws BeansExceptionb {b CachedIntrospectionResults cr CachedIntrospectionResults.forClass(clazz);b return cr.getPropertyDescriptors();b }bb public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName)b throws BeansExceptionb {b CachedIntrospectionResults cr CachedIntrospectionResults.forClass(clazz);b return cr.getPropertyDescriptor(propertyName);b }bb public static PropertyDescriptor findPropertyForMethod(Method method)b throws BeansExceptionb {b return findPropertyForMethod(method, method.getDeclaringClass());b }bb public static PropertyDescriptor findPropertyForMethod(Method method, Class clazz)b throws BeansExceptionb {b Assert.notNull(method, Method must not be null);b PropertyDescriptor[] pds getPropertyDescriptors(clazz);b PropertyDescriptor[] arrayOfPropertyDescriptor1 pds; int i arrayOfPropertyDescriptor1.length; for (int j 0; j targetType)b {b if ((targetType null) || (targetType.isArray()) || (unknownEditorTypes.contains(targetType)))b return null;bb ClassLoader cl targetType.getClassLoader();b if (cl null)b try {b cl ClassLoader.getSystemClassLoader();b if (cl null)b return null;bb }b catch (Throwable ex)b {b if (logger.isDebugEnabled())b logger.debug(Could not access system ClassLoader: ex);bb return null;b }bb String editorName targetType.getName() Editor;b try {b Class editorClass cl.loadClass(editorName);b if (!(PropertyEditor.class.isAssignableFrom(editorClass))) {b if (logger.isWarnEnabled()) {b logger.warn(Editor class [ editorName ] does not implement [java.beans.PropertyEditor] interface);b }bb unknownEditorTypes.add(targetType);b return null;b }b return ((PropertyEditor)instantiateClass(editorClass));b }b catch (ClassNotFoundException ex) {b if (logger.isDebugEnabled())b logger.debug(No property editor [ editorName ] found for type targetTypeb .getName() according to Editor suffix convention);bb unknownEditorTypes.add(targetType); }b return null;b }bb public static Class findPropertyType(String propertyName, Class[] beanClasses)b {b Class[] arrayOfClass;b int j;b if (beanClasses ! null) {b arrayOfClass beanClasses; int i arrayOfClass.length; for (j 0; j clazz)b {b Assert.notNull(clazz, Class must not be null);b return ((isSimpleValueType(clazz)) || ((clazz.isArray()) (isSimpleValueType(clazz.getComponentType()))));b }bb public static boolean isSimpleValueType(Class clazz)b {b return ((ClassUtils.isPrimitiveOrWrapper(clazz)) || (clazz.isEnum()) || b (CharSequence.classb .isAssignableFrom(clazz)) || b (Number.classb .isAssignableFrom(clazz)) || b (Date.classb .isAssignableFrom(clazz)) || b (clazzb .equals(URI.class)) || b (clazz.equals(URL.class)) || b (clazzb .equals(Locale.class)) || b (clazz.equals(Class.class)));b }bb public static void copyProperties(Object source, Object target)b throws BeansExceptionb {b copyProperties(source, target, null, (String[])null);b }bb public static void copyProperties(Object source, Object target, Class editable)b throws BeansExceptionb {b copyProperties(source, target, editable, (String[])null);b }bb public static void copyProperties(Object source, Object target, String[] ignoreProperties)b throws BeansExceptionb {b copyProperties(source, target, null, ignoreProperties);b }bb private static void copyProperties(Object source, Object target, Class editable, String[] ignoreProperties)b throws BeansExceptionb {b Assert.notNull(source, Source must not be null);b Assert.notNull(target, Target must not be null);bb Class actualEditable target.getClass();b if (editable ! null) {b if (!(editable.isInstance(target)))b {b throw new IllegalArgumentException(Target class [ target.getClass().getName() ] not assignable to Editable class [ editableb .getName() ]);b }b actualEditable editable;b }b PropertyDescriptor[] targetPds getPropertyDescriptors(actualEditable);b List ignoreList (ignoreProperties ! null) ? Arrays.asList(ignoreProperties) : null;bb PropertyDescriptor[] arrayOfPropertyDescriptor1 targetPds; int i arrayOfPropertyDescriptor1.length; for (int j 0; j