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

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

1、概述

有时候,我们需要在项目中使用反射功能,如果使用最原始的方法来开发,代码量会非常多,而且很麻烦,它需要处理一大堆异常以及访问权限等问题。

此类封装了许多支持反射的方法。

2、方法摘要

2.1 获取方法

示例:

public static Method findMethod(Class<?> clazz, String name) {
	return findMethod(clazz, name, new Class<?>[0]);
}

public static Method findMethod(Class<?> clazz, String name, @Nullable Class<?>... paramTypes) {
	Assert.notNull(clazz, "Class must not be null");
	Assert.notNull(name, "Method name must not be null");
	Class<?> searchType = clazz;
	while (searchType != null) {
		Method[] methods = (searchType.isInterface() ? searchType.getMethods() : getDeclaredMethods(searchType));
		for (Method method : methods) {
			if (name.equals(method.getName()) &&
					(paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
				return method;
			}
		}
		searchType = searchType.getSuperclass();
	}
	return null;
}

2.2 获取字段

示例:

public static Field findField(Class<?> clazz, String name) {
	return findField(clazz, name, null);
}

public static Field findField(Class<?> clazz, @Nullable String name, @Nullable Class<?> type) {
	Assert.notNull(clazz, "Class must not be null");
	Assert.isTrue(name != null || type != null, "Either name or type of the field must be specified");
	Class<?> searchType = clazz;
	while (Object.class != searchType && searchType != null) {
		Field[] fields = getDeclaredFields(searchType);
		for (Field field : fields) {
			if ((name == null || name.equals(field.getName())) &&
					(type == null || type.equals(field.getType()))) {
				return field;
			}
		}
		searchType = searchType.getSuperclass();
	}
	return null;
}

2.3 执行方法

示例:

public static Object invokeMethod(Method method, @Nullable Object target) {
	return invokeMethod(method, target, new Object[0]);
}

public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) {
	try {
		return method.invoke(target, args);
	}
	catch (Exception ex) {
		handleReflectionException(ex);
	}
	throw new IllegalStateException("Should never get here");
}

2.4 判断字段是否常量

示例:

public static boolean isPublicStaticFinal(Field field) {
	int modifiers = field.getModifiers();
	return (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers));
}

2.5 判断方法是否是equals()

示例:

public static boolean isEqualsMethod(@Nullable Method method) {
	if (method == null || !method.getName().equals("equals")) {
		return false;
	}
	Class<?>[] paramTypes = method.getParameterTypes();
	return (paramTypes.length == 1 && paramTypes[0] == Object.class);
}

本文持续更新中。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

进步·于辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值