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 ...
随机推荐
- GitForWindows工具集(GitBash命令行)
1.Git For Windows工具集 Git For Windows专注于提供一套轻量级的本地工具集, 它将Git SCM的完整功能集引入Windows, 同时为Git用户提供适当的用户界面, 方 ...
- Google面试评分卡
Google对工程面试之前,会让面试人员填一张评分卡,以加强面试官对你的理解,大致内容如下: 0 - 对于相关技术领域还不熟悉. 1 - 可以读懂这个领域的基础知识. 2 - 可以实现一些小的改动,清 ...
- js箭头函数 的 (e) => { } 写法笔记
1. (e) => {} 是ES 6 新语法,默认是Es 5.1,因此在这里设置一下就不会提示红色下划线了 2.使用: (e) => {} , 其实就是function (e){} 的缩 ...
- Spring cloud 框架 --- Eureka 心得
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 (1)接触了spring cloud 框架 ,首先要知道Eureka是什 ...
- css 垂直居中技巧
CSS垂直居中技巧,我只会23个,你会几个?自古以来(是有多?~),网页CSS的垂直居中需求始终没有停过,而其困难度也始终没有让人轻松过,经过了每位开发先烈的研究后,据说CSS的垂直居中技巧已达到近十 ...
- Cookie.Session到Token和JWT
一.session和cookie: 现在一般都是session和cookie一起用,一起提.但是他们俩其实不是一定要在一起. session的产生原因是,http协议是无状态的 这就导致了,不同的用户 ...
- .gitignore文件编写规则
1.gitignore说明 在使用git的过程中,一般我们总会有些文件无需纳入git的管理,也不希望它们总出现在未跟踪文件列表,这些文件通常是日志文件.临时文件.编译产生的中间文件.工具自动生成的文件 ...
- css编写规则BEM
简单来说,格式如下: .block { /* styles */ } .block__element { /* styles */ } .block--modifier { /* styles */ ...
- 周末撸了个Excel框架,现已开源,yyds!!
大家好,我是冰河~~ 不管是传统软件企业还是互联网企业,不管是管理软件还是面向C端的互联网应用.都不可避免的会涉及到报表操作,而对于报表业务来说,一个很重要的功能就是将数据导出到Excel. 如果我们 ...
- 深度介绍Flink在字节跳动数据流的实践
本文是字节跳动数据平台开发套件团队在1月9日Flink Forward Asia 2021: Flink Forward 峰会上的演讲分享,将着重分享Flink在字节跳动数据流的实践. 字节跳动数据流 ...