SpringBeanUtils的部分方法类
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/12060553.html
SpringBeanUtils的部分方法类:
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier; /**
* TODO
* SpringBeanUtils的部分方法类
*/
public class SpringBeanUtils { /**
* 实例化一个class
* @param clazz
* @param <T>
* @return
*/
public static <T> T instantiate(Class<T> clazz) {
//Assert.notNull(clazz, "not null");
if (clazz.isInterface()) {
System.out.println("no interface");
} else {
try {
return clazz.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
System.out.println("constructor not accesbile");
} catch (InstantiationException e) {
e.printStackTrace();
System.out.println("not abstract class");
}
}
return null;
} /**
* 实例化一个class
* @param clazz
* @param <T>
* @return
*/
public static <T> T stanciateClass(Class<T> clazz) {
//Assert.notNull(clazz, "not null");
if (clazz.isInterface()) {
System.out.println("not interface");
} else {
try {
return instantiateClass(clazz.getDeclaredConstructor());
} catch (NoSuchMethodException e) {
System.out.println("no default constructors");
} catch (LinkageError e) {
System.out.println("unresolvable class definition");
}
} return null;
} /**
* 通过构造器和参数实例化类
* @param ctor
* @param args
* @param <T>
* @return
*/
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) {
//Assert.notNull(ctor, "not null");
try {
makeAccessible(ctor);
/**
* 未做kotin检测
*/
return ctor.newInstance(args);
} catch (Exception e) {
e.printStackTrace();
} return null;
} /**
* 设置构造器可访问
* @param ctor
*/
public static void makeAccessible(Constructor<?> ctor) {
if ((!Modifier.isPublic(ctor.getModifiers())) || (!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
ctor.setAccessible(true);
}
} /**
* 查找方法
* @param clazz
* @param methodName
* @param paramTypes
* @return
*/
public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
try {
/**
* 获取该类和所有父类的public方法
*/
return clazz.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException e) {
/**
* 获取指定的声明过的方法
*/
return findDeclaredMethod(clazz, methodName, paramTypes);
}
} /**
* 获取指定的声明过的方法
* @param clazz
* @param methodName
* @param paramTypes
* @return
*/
public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes){
try {
return clazz.getDeclaredMethod(methodName, paramTypes);
} catch (NoSuchMethodException e) {
return clazz.getSuperclass() != null ? findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes) : null;
}
} /**
* findMethodWithMinimalParameters
* 暂时不清楚这个方法干什么用的
* @param methods
* @param methodName
* @return
*/
public static Method findMethodWithMinimalParameters(Method[] methods, String methodName){
/**
* 目标方法
*/
Method targetMethod = null;
/**
* 找到的最小参数方法的数量
*/
int numMethodsFoundWithCurrentMinimumArgs = 0;
/**
* 方法数组
*/
Method[] methodsArr = methods;
int methodsSize = methods.length; for (int i = 0; i < methodsSize; ++i) {
Method method = methodsArr[i];
if (method.getName().equals(methodName)){
int numParams = method.getParameterCount();
if (targetMethod != null && numParams >= targetMethod.getParameterCount()) {
if (!method.isBridge() && targetMethod.getParameterCount() == numParams) {
if (targetMethod.isBridge()) {
targetMethod = method;
} else {
++numMethodsFoundWithCurrentMinimumArgs;
}
}
} else {
targetMethod = method;
numMethodsFoundWithCurrentMinimumArgs = 1;
}
}
} if (numMethodsFoundWithCurrentMinimumArgs > 1) {
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.");
} else {
return targetMethod;
}
} }
SpringBeanUtils的部分方法类的更多相关文章
- php操作oracle的方法类集全
在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...
- C#导出数据到Excel通用的方法类
导出数据到Excel通用的方法类,请应对需求自行修改. 资源下载列表 using System.Data; using System.IO; namespace IM.Common.Tools { p ...
- idea live template高级知识, 进阶(给方法,类,js方法添加注释)
为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了. 实现的效果如下: 给Java中的method添加方法: /** * * @Method : addMenu * ...
- IOS上传图片方法类
IOS上传图片方法类 iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...
- DataTable和DataRow利用反射直接转换为Model对象的扩展方法类
DataTable和DataRow利用反射直接转换为Model对象的扩展方法类 /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...
- Eclipse查看方法/类调用的方法
1.(首推)双击选中该方法/类,[Ctrl]+[Alt]+[H](Open Call Hierarchy) 2.(次推)选中该方法/类,[Ctrl]+[Shift]+[G](References) 3 ...
- day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...
- jQuery $.fn.extend()方法类插件
一.为JQuery原型扩展新的属性和方法,然后在JQuery的实例对象上调用 在 jQuery 中,我们可以使用$.fn.extend()方法来定义一个方法类插件.方法类插件就是首先你使用 jQuer ...
- C# 访问MongoDB 通用方法类
using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...
随机推荐
- JS中常见的几种控制台台报错
Error 控制台报错 EvalError 全局错误RangeError 引用错ReferenceError 参数错误SyntaxError 语法错误TypeError 类型错误URIError 编码 ...
- Java初学者作业——为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转。
返回本章节 返回作业目录 需求说明: 为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转. 实现思路: 定义mainMenu方法,用于显示主菜单. 主菜单主要负责显示4个选项,分别是 ...
- pod存在,但是deployment和statefulset不存在
pod存在,但是deployment和statefulset不存在 这样的话,可以看一下是不是ReplicaSet, kubectl get ReplicaSet -n iot
- Swoole 协程的并发调用及使用示例
示例一: 利用通道pop会自动挂起当前协程,等待生产者推送数据的特性,实现并发调用,并在协程完成后组合结果集. $serv = new Swoole\Http\Server("127.0.0 ...
- Spring @Valid 和 @Validated 的区别和使用
两者区别 @Valid @Validated 标准 标准JSR-303规范 增强JSR-303规范 包 javax.validation org.springframework.validation ...
- Selenium_使用switch_to.frame处理网页框架切换(13)
与在新窗口打开一个网页后需要切换窗口才能定位元素一样,在iframe标签中的元素也不能直接定位,需要切换到对应的iframe框架中才能进行元素定位. 完成网页框架切换操作需要用selenium中的两个 ...
- js 模块化 -- 基本的导出与引入class模块
1.目录结构 2.类语法与导出 class food { } //定义常量 let c = "苹果"; //正确的函数写法 food.prototype.getfood = fun ...
- Servlet初级学习加入数据库操作(三)
源代码地址(访问密码:7567):https://url56.ctfile.com/f/34653256-527822631-2e255a CRUD 增删改查 准备添加的操作 点击添加之后,出现新的页 ...
- Maven+ajax+SSM实现查询
2.尚硅谷_SSM高级整合_使用ajax操作实现页面的查询功能 16.尚硅谷_SSM高级整合_查询_返回分页的json数据.avi 在上一章节的操作中我们是将PageInfo对象存储在request域 ...
- layui type:2 iframe子页面向父页面传值
需求: 选择子页面表格中的radio或者双击该行,得到的该行数据传到父页面,由父页面渲染. 网上的各种方法都用了,父页面就是获取不到子页面传的值,过了一晚上,睡了一觉,柳暗花明又一村. layui t ...