Spring-API简析_org.springframework.util.BeanUtils类(基于 Latest JDK)(浅析源码)

【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
https://blog.csdn.net/m0_69908381/article/details/132038537
出自【进步*于辰的博客

1、概述

内容整理中。。。

2、方法摘要

2.1 拷贝对象的属性

曾几何时,你有没有这样的需求:把某个对象中的所有属性,都拷贝到另外一个对象中?

示例:

public static void copyProperties(Object source, Object target) throws BeansException {
	copyProperties(source, target, null, (String[]) null);
}
public static void copyProperties(Object source, Object target, Class<?> editable) throws BeansException {
	copyProperties(source, target, editable, (String[]) null);
}
public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
	copyProperties(source, target, null, ignoreProperties);
}
private static void copyProperties(Object source, Object target, @Nullable Class<?> editable,
		@Nullable String... ignoreProperties) throws BeansException {

	Assert.notNull(source, "Source must not be null");
	Assert.notNull(target, "Target must not be null");

	Class<?> actualEditable = target.getClass();
	if (editable != null) {
		if (!editable.isInstance(target)) {
			throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
					"] not assignable to Editable class [" + editable.getName() + "]");
		}
		actualEditable = editable;
	}
	PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
	List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

	for (PropertyDescriptor targetPd : targetPds) {
		Method writeMethod = targetPd.getWriteMethod();
		if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
			PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
			if (sourcePd != null) {
				Method readMethod = sourcePd.getReadMethod();
				if (readMethod != null &&
						ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
					try {
						if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
							readMethod.setAccessible(true);
						}
						Object value = readMethod.invoke(source);
						if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
							writeMethod.setAccessible(true);
						}
						writeMethod.invoke(target, value);
					}
					catch (Throwable ex) {
						throw new FatalBeanException(
								"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
					}
				}
			}
		}
	}
}

2.2 实例化某个类

示例:

public static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo) throws BeanInstantiationException {
	Assert.isAssignable(assignableTo, clazz);
	return (T) instantiateClass(clazz);
}
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
	Assert.notNull(clazz, "Class must not be null");
	if (clazz.isInterface()) {
		throw new BeanInstantiationException(clazz, "Specified class is an interface");
	}
	try {
		Constructor<T> ctor = (KotlinDetector.isKotlinType(clazz) ?
				KotlinDelegate.getPrimaryConstructor(clazz) : clazz.getDeclaredConstructor());
		return instantiateClass(ctor);
	}
	catch (NoSuchMethodException ex) {
		throw new BeanInstantiationException(clazz, "No default constructor found", ex);
	}
	catch (LinkageError err) {
		throw new BeanInstantiationException(clazz, "Unresolvable class definition", err);
	}
}
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
	Assert.notNull(ctor, "Constructor must not be null");
	try {
		ReflectionUtils.makeAccessible(ctor);
		return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ?
				KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args));
	}
	catch (InstantiationException ex) {
		throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
	}
	catch (IllegalAccessException ex) {
		throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
	}
	catch (IllegalArgumentException ex) {
		throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
	}
	catch (InvocationTargetException ex) {
		throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
	}
}

2.4 获取指定类的指定方法

示例:

public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
	try {
		return clazz.getMethod(methodName, paramTypes);
	}
	catch (NoSuchMethodException ex) {
		return findDeclaredMethod(clazz, methodName, paramTypes);
	}
}
public static Method findDeclaredMethodWithMinimalParameters(Class<?> clazz, String methodName)
		throws IllegalArgumentException {

	Method targetMethod = findMethodWithMinimalParameters(clazz.getDeclaredMethods(), methodName);
	if (targetMethod == null && clazz.getSuperclass() != null) {
		targetMethod = findDeclaredMethodWithMinimalParameters(clazz.getSuperclass(), methodName);
	}
	return targetMethod;
}
public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
	try {
		return clazz.getDeclaredMethod(methodName, paramTypes);
	}
	catch (NoSuchMethodException ex) {
		if (clazz.getSuperclass() != null) {
			return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);
		}
		return null;
	}
}

2.4 获取指定方法的参数

示例:

public static PropertyDescriptor findPropertyForMethod(Method method) throws BeansException {
	return findPropertyForMethod(method, method.getDeclaringClass());
}
public static PropertyDescriptor findPropertyForMethod(Method method, Class<?> clazz) throws BeansException {
	Assert.notNull(method, "Method must not be null");
	PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
	for (PropertyDescriptor pd : pds) {
		if (method.equals(pd.getReadMethod()) || method.equals(pd.getWriteMethod())) {
			return pd;
		}
	}
	return null;
}

本文持续更新中。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进步·于辰

谢谢打赏!!很高兴可以帮到你!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值