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 ...
随机推荐
- Shell调试Debug的三种方式
Shell脚本进行Debug调试的三种方法如下: 1.在调用脚本的时候开启deubg sh -x shell.sh 2.在脚本文件首行开启deubg #!/bin/bash -x 3. 使用set开启 ...
- C/C++ Qt 运用JSON解析库 [基础篇]
JSON是一种简单的轻量级数据交换格式,Qt库为JSON的相关操作提供了完整的类支持,使用JSON解析文件之前需要先通过TextStream流将文件读入到字符串变量内,然后再通过QJsonDocume ...
- Python 使用timeit模块计算时间复杂度时系统报“invalid syntax”错误
最近在看算法相关的文档 在时间复杂度环节 遇到一个实例: 导入timeit模块后,通过Timer定时器计算两种不同处理方法的时间复杂度 错误代码及报错如下图所示: 仔细查阅 发现from__main_ ...
- Selenium_获取元素文本、属性值、尺寸(8)
from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get(" ...
- python 面向对象:封装---对象的属性可以是另一个类创建的对象
# 对象封装:对象的属性可以是另一个类创建的对象 # 案例需求: # 1.士兵许三多有一把AK47 # 2.士兵用枪射击标靶 # 3.枪能装填和发射子弹 class Gun: # 分析: # 枪的属性 ...
- [ Flask ] myblog_flask问题集(RESTfull风格)
VUE问题 前端VUE怎么捕获所有404NOT FOUND的路由呢? [ 解决方案 ] vue-router路由守卫,参考文档:动态路由匹配 对于路由.../edit/<id>,自己能编辑 ...
- vue3.0+vite+ts项目搭建--vite.config.ts配置(三)
vite.config.ts配置 配置路径处理模块 安装ts的类型声明文件 yarn add @types/node -D 通过配置alias来定义路径的别名 resolve: { alias: { ...
- iView 用renderContent自定义树组件
iview的树组件在有默认选中状态的时候默认选中状态的样式改变有bug,默认选中的样式不好看,鉴于此,有renderContent来改造iview的树组件, 效果如图 代码如下 <templat ...
- 一个小程序:Instrumentation的使用
本来是想练习Matrix的,没想到写了一个自定义View,监听它的ASWD键后,不知道该如何按下ASWD(手机上一般都没实体按键了).于是: 一个自定义View: public class MyVie ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...