/*
*MIT License
*
*Copyright (c) 2019 chenshuai cs4380@163.com
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
package com.cs.cslc.common.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import java.lang.reflect.*;
/**
* ReflectionUtils .
* 反射工具类.
* 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
*
* @author cs4380 https://gitee.com/xhbug_cs4380 cs4380@163.com
* @version 1.0
* @since 2019-10-02 11:50
*/
@SuppressWarnings("rawtypes")
public class ReflectionUtils {
private static final String SETTER_PREFIX = "set";
private static final String GETTER_PREFIX = "get";
private static final String CGLIB_CLASS_SEPARATOR = "$$";
private static Logger logger = LoggerFactory.getLogger(ReflectionUtils.class);
/**
* 调用Getter方法.
* 支持多级,如:对象名.对象名.方法
*/
public static Object invokeGetter(Object obj, String propertyName) {
Object object = obj;
for (String name : StringUtils.split(propertyName, ".")) {
String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name);
object = invokeMethod(object, getterMethodName, new Class[]{}, new Object[]{});
}
return object;
}
/**
* 调用Setter方法, 仅匹配方法名。
* 支持多级,如:对象名.对象名.方法
*/
public static void invokeSetter(Object obj, String propertyName, Object value) {
Object object = obj;
String[] names = StringUtils.split(propertyName, ".");
for (int i = 0; i < names.length; i++) {
if (i < names.length - 1) {
String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]);
object = invokeMethod(object, getterMethodName, new Class[]{}, new Object[]{});
} else {
String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]);
invokeMethodByName(object, setterMethodName, new Object[]{value});
}
}
}
/**
* 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
*/
public static Object getFieldValue(final Object obj, final String fieldName) {
Field field = getAccessibleField(obj, fieldName);
if (field == null) {
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
}
Object result = null;
try {
result = field.get(obj);
} catch (IllegalAccessException e) {
logger.error("不可能抛出的异常{}", e.getMessage());
}
return result;
}
/**
* 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
*/
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
Field field = getAccessibleField(obj, fieldName);
if (field == null) {
logger.error("Could not find field [" + fieldName + "] on target [" + obj + "]");
return;
}
try {
field.set(obj, convert(value, field.getType()));
} catch (IllegalAccessException e) {
logger.error("不可能抛出的异常:{}", e.getMessage());
}
}
public static Object convert(Object object, Class<?> type) {
if (object instanceof Number) {
Number number = (Number) object;
if (type.equals(byte.class) || type.equals(Byte.class)) {
return number.byteValue();
}
if (type.equals(short.class) || type.equals(Short.class)) {
return number.shortValue();
}
if (type.equals(int.class) || type.equals(Integer.class)) {
return number.intValue();
}
if (type.equals(long.class) || type.equals(Long.class)) {
return number.longValue();
}
if (type.equals(float.class) || type.equals(Float.class)) {
return number.floatValue();
}
if (type.equals(double.class) || type.equals(Double.class)) {
return number.doubleValue();
}
}
if (type.equals(String.class)) {
return object == null ? "" : object.toString();
}
return object;
}
/**
* 直接调用对象方法, 无视private/protected修饰符.
* 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用.
* 同时匹配方法名+参数类型,
*/
public static Object invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes,
final Object[] args) {
Method method = getAccessibleMethod(obj, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]");
}
try {
return method.invoke(obj, args);
} catch (Exception e) {
throw convertReflectionExceptionToUnchecked(e);
}
}
/**
* 直接调用对象方法, 无视private/protected修饰符,
* 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用.
* 只匹配函数名,如果有多个同名函数调用第一个。
*/
public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) {
Method method = getAccessibleMethodByName(obj, methodName);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]");
}
try {
return method.invoke(obj, args);
} catch (Exception e) {
e.printStackTrace();
throw convertReflectionExceptionToUnchecked(e);
}
}
/**
* 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
* <p>
* 如向上转型到Object仍无法找到, 返回null.
*/
public static Field getAccessibleField(final Object obj, final String fieldName) {
Validate.notNull(obj, "object can't be null");
Validate.notBlank(fieldName, "fieldName can't be blank");
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一款基于Spring Boot框架构建的cslc-admin前后端分离单体架构设计源码。项目包含339个文件,涵盖了145个Java源文件、54个JavaScript文件、31个Vue组件文件、22个XML配置文件、21个SVG矢量图形文件、7个图片文件、7个模板文件、6个Markdown文件、6个SCSS样式文件、5个CSS样式文件,主要使用Java、JavaScript、Vue、CSS和HTML等语言开发。后端采用Spring Boot、Spring、Spring MVC、MyBatis和Shiro等主流技术,前端则基于vue、vuex、element-ui和axios等技术构建,实现了基础权限管理功能。
资源推荐
资源详情
资源评论
收起资源包目录
基于Spring Boot的cslc-admin前后端分离单体架构设计源码 (461个子文件)
commitlint.config.cjs 4KB
.eslintrc.cjs 3KB
.prettierrc.cjs 2KB
.stylelintrc.cjs 1KB
commit-msg 38B
bootstrap.min.css 118KB
layui.css 30KB
layer.css 14KB
laydate.css 8KB
bootstrap-table.min.css 5KB
.env.development 486B
.editorconfig 487B
glyphicons-halflings-regular.eot 20KB
.eslintignore 116B
401.gif 159KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
.gitignore 175B
.gitignore 80B
index.html 2KB
index.html 1KB
favicon.ico 49KB
ReflectionUtils.java 15KB
BaseUserBiz.java 12KB
GeneratorUtils.java 12KB
BaseBusinessBiz.java 10KB
UserService.java 10KB
TokenAuthFilter.java 8KB
SysRoleAuthorizationBiz.java 8KB
SysMenuBiz.java 7KB
BaseDeptBiz.java 7KB
OssController.java 7KB
BaseUserController.java 7KB
BeanUtil.java 6KB
SysDictTypeBiz.java 6KB
SysDictValueBiz.java 6KB
BaseContextHandler.java 6KB
BaseController.java 6KB
HttpUtils.java 6KB
BaseUser.java 5KB
SysMenuButtonBiz.java 5KB
VerifyCodeUtils.java 5KB
ShiroDatabaseRealm.java 5KB
JWTUtil.java 5KB
ShiroConfig.java 5KB
SysMenuButton.java 5KB
SysMenu.java 5KB
SysRoleAuthorizationController.java 5KB
DateTimeUtils.java 4KB
AuthService.java 4KB
BaseUserRoleBiz.java 4KB
BaseDept.java 4KB
BaseDeptController.java 4KB
AuthController.java 4KB
FileUtil.java 4KB
SysDictValue.java 4KB
LocalStorageService.java 4KB
SysDictType.java 4KB
QcloudCloudStorageService.java 4KB
TreeUtil.java 4KB
GlobalExceptionHandler.java 4KB
SysTenant.java 4KB
CloudStorageService.java 4KB
BaseDeptUserBiz.java 4KB
QiniuCloudStorageService.java 4KB
SysRole.java 3KB
MenuTreeDTO.java 3KB
DateTimeSerializerConfig.java 3KB
AuthenticationExceptionController.java 3KB
SysMenuTreeVO.java 3KB
CloudStorageConfig.java 3KB
GeneratorRest.java 3KB
GeneratorService.java 3KB
ParamQuery.java 3KB
AliyunCloudStorageService.java 3KB
MyMetaObjectHandler.java 3KB
SysDictTypeController.java 3KB
Sha256Util.java 3KB
SysRoleAuthorization.java 3KB
SysDictTypeTreeVO.java 3KB
ListUtils.java 3KB
SysDictValueController.java 3KB
BizBootstrap.java 3KB
TableResult.java 3KB
StringEscapeEditor.java 2KB
BaseDeptTreeVO.java 2KB
UserInfoDTO.java 2KB
SysTenantBiz.java 2KB
BaseDeptUser.java 2KB
CommonConstant.java 2KB
BaseUserRole.java 2KB
TreeNodeVO.java 2KB
ApiRateLimitAspect.java 2KB
SysMenuController.java 2KB
BaseResponse.java 2KB
EmailServiceImpl.java 2KB
OSSFactory.java 2KB
SysRoleAuthorizationMapper.java 2KB
AuthenticationEnums.java 2KB
共 461 条
- 1
- 2
- 3
- 4
- 5
资源评论
xyq2024
- 粉丝: 2437
- 资源: 5457
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功