原创:转载需注明原创地址 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. 图像数据到网格数据-2——改进的SMC算法的实现

    概要 本篇接上一篇继续介绍网格生成算法,同时不少内容继承自上篇.上篇介绍了经典的三维图像网格生成算法MarchingCubes,并且基于其思想和三角形表实现了对样例数据的网格构建.本篇继续探讨网格生成 ...

  2. css 基础 字体和文本样式

    字体样式处理font-size:30px:// 设置字体大小 font-weight:700://设置粗体 font-sytle:italic; //设置文字斜体 font-family: 斜体,宋体 ...

  3. sqlsugar freesql hisql 三个ORM框架性能测试对比

    hisql与目前比较流行的ORM框架性能测试对比 总体测试结果 插入记录数 hisql(耗时) sqlsugar(耗时) freesql(耗时) 5条 0.0107秒 0.0312秒 0.02675秒 ...

  4. Flask_请求钩子(七)

    在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: 在请求开始时,建立数据库连接: 在请求开始时,根据需求进行权限校验: 在请求结束时,指定数据的交互格式: 为了让每个视图函数避免编 ...

  5. 一文了解Flink State Backends

    原文链接: 一文了解Flink State Backends 当我们使用Flink进行流式计算时,通常会产生各种形式的中间结果,我们称之为State.有状态产生,就必然涉及到状态的存储,那么Flink ...

  6. efcore使用ShardingCore实现分表分库下的多租户

    efcore使用ShardingCore实现分表分库下的多租户 介绍 本期主角:ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业 ...

  7. Sentry 开发者贡献指南 - Django Rest Framework(Serializers)

    Serializer 用于获取复杂的 python 模型并将它们转换为 json.序列化程序还可用于在验证传入数据后将 json 反序列化回 Python 模型. 在 Sentry,我们有两种不同类型 ...

  8. 18个示例详解 Spring 事务传播机制(附测试源码)

    什么是事务传播机制 事务的传播机制,顾名思义就是多个事务方法之间调用,事务如何在这些方法之间传播. 举个例子,方法 A 是一个事务的方法,方法 A 执行的时候调用了方法 B,此时方法 B 有无事务以及 ...

  9. 触发器中获取sql

    CREATE trigger 触发器名 on 表名 for update,delete as set nocount on create table #t(EvebtType varchar(60), ...

  10. Centos下安装Spark

    (注:由于第一次安装操作失误,所以重新安装了,因此截图为第一次的截图,命令为第二次安装的命令) (注:图是本人安装所截图,本人安装参考网址:https://www.cnblogs.com/shaosk ...