原创:转载需注明原创地址 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的部分方法类的更多相关文章

  1. php操作oracle的方法类集全

    在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...

  2. C#导出数据到Excel通用的方法类

    导出数据到Excel通用的方法类,请应对需求自行修改. 资源下载列表 using System.Data; using System.IO; namespace IM.Common.Tools { p ...

  3. idea live template高级知识, 进阶(给方法,类,js方法添加注释)

    为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了. 实现的效果如下: 给Java中的method添加方法: /** * * @Method : addMenu * ...

  4. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

  5. DataTable和DataRow利用反射直接转换为Model对象的扩展方法类

    DataTable和DataRow利用反射直接转换为Model对象的扩展方法类   /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...

  6. Eclipse查看方法/类调用的方法

    1.(首推)双击选中该方法/类,[Ctrl]+[Alt]+[H](Open Call Hierarchy) 2.(次推)选中该方法/类,[Ctrl]+[Shift]+[G](References) 3 ...

  7. day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)

    1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...

  8. jQuery $.fn.extend()方法类插件

    一.为JQuery原型扩展新的属性和方法,然后在JQuery的实例对象上调用 在 jQuery 中,我们可以使用$.fn.extend()方法来定义一个方法类插件.方法类插件就是首先你使用 jQuer ...

  9. C# 访问MongoDB 通用方法类

    using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...

随机推荐

  1. HTML网页设计基础笔记 • 【第2章 排列页面内容】

    全部章节   >>>> 本章目录 2.1 音频标签和视频标签 2.1.1 音频标签 2.1.2 视频标签 2.2 列表.div 以及 span 标签 2.2.1 列表标签 2. ...

  2. ElasticSearch启动失败:master not discovered or elected yet, an election requires at least X nodes with ids from [XXXXX]

    这个问题困扰过我一段时间,它其实是说未能选举出一个主节点,最后在官网教程中找到答案. 首先是启动(链接): 总结下来就是,如果ES集群是第一次启动,那么需要 cluster.initial_maste ...

  3. Zookeeper基础教程(三):Zookeeper连接使用—zkCli

    上一篇介绍Zookeeper的安装,并介绍了使用ZooInspector连接Zookeeper,这里主要介绍以命令行的形式介绍Zookeeper 假如我们已经安装了Zookeeper集群,集群中的安装 ...

  4. oceanbase数据库比赛总结

    前言 ob数据库大赛由蚂蚁金服的oceanbase团队组织,今年是第一届,宣传很广,比赛十月份开始,但早在上半年就看见大量的宣传了,比赛也是相当的卷.我们进了复赛之后感觉要卷进决赛需要付出的时间精力都 ...

  5. SpringBoot 之 扩展 SpringMVC

    增加自定义视图解析器: # src/main/java/com/wu/config/MyMvcConfig.java @Configuration // 标注这个类是一个配置类 public clas ...

  6. 怎样从 bat 批处理文件调用 PowerShell 脚本

    https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file https: ...

  7. nvm切换node版本出现乱码 exit status 1:

    nvm切换nodejs版本出现exit status 1:乱码 跟着网上的教程一步一步做,还是出现问题.浪费一下午的时间 最后发现却因为我没用CMD管理员权限运行 扑街 解决方法: 用管理员身份运行就 ...

  8. markdownFormat

      对文档编辑主要还是用wps,因为以前毕业论文都是用的它来编排(刚开始用wps毕业论文的时候真的是用的想吐,感觉非常不好用,而且功能太多但对于自己需要的功能又偏偏找不到),用过几次后还觉得用它编辑文 ...

  9. 508. Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  10. golang中为何在同一个goroutine中使用无缓冲通道会导致死锁

    package main import "fmt" func main() { /* 以下程序会导致死锁 c := make(chan int) c <- 10 n1 := ...