0
点赞
收藏
分享

微信扫一扫

【commons-beanutils专题】001- MethodUtils 专题


【commons-beanutils专题】001- MethodUtils 专题

文章目录

  • ​​【commons-beanutils专题】001- MethodUtils 专题​​
  • ​​一、准备​​
  • ​​0、MethodUtils 主要作用​​
  • ​​1、引入 commons-beanutils 依赖​​
  • ​​2、pom.xml 文件​​
  • ​​3、实体类父类​​
  • ​​4、实体类子类​​
  • ​​二、缓存​​
  • ​​0、前置代码​​
  • ​​1、设置是否应该缓存方法以获得更高的性能,默认为true​​
  • ​​2、清空方法缓存​​
  • ​​三、执行方法​​
  • ​​1、执行方法:单个参数 + 自动获取类型​​
  • ​​2、执行方法:多个参数 + 自动获取类型​​
  • ​​3、执行方法:单个参数 + 手动设置类型​​
  • ​​四、精确执行方法​​
  • ​​1、精确执行方法,此处我们精确调用有 1 个参数的方法 + 自动获取类型​​
  • ​​2、精确执行方法:此处我们精确调用有 2 个参数的方法 + 自动获取类型​​
  • ​​3、精确执行方法:此处我们精确调用有 2 个参数的方法 + 手动设置类型​​
  • ​​五、精确指定静态方法​​
  • ​​1、精确执行静态方法:单个参数 + 自动获取类型​​
  • ​​2、精确执行静态方法:多个参数 + 自动获取类型​​
  • ​​3、精确执行静态方法:多个参数 + 手动设置类型​​
  • ​​4、精确执行静态方法:无参数​​
  • ​​六、获取可访问方法​​
  • ​​1、获取一个可访问的方法:类名,方法名,参数类型​​
  • ​​2、获取一个可访问的方法:类名,方法名,参数类型列表​​
  • ​​七、获取指定方法的可访问方法​​
  • ​​1、获取指定方法的可访问方法:找不到这个方法返回空(测试1)​​
  • ​​2、获取指定方法的可访问方法:找不到这个方法返回空(测试2)​​
  • ​​3、获取指定方法的可访问方法:找不到这个方法返回空(测试3)​​
  • ​​4、获取指定方法的可访问方法:类 + 方法​​
  • ​​5、获取与指定方法名称相同且与参数类型列表匹配的可访问方法​​
  • ​​八、确定一个类型是否可以用作方法调用中的参数​​
  • ​​九、基本类型 & 包装类型​​
  • ​​1、获取一个基本类型的包装类型​​
  • ​​2、获得包装类的基本数据类型​​
  • ​​3、如果是简单数据类型则返回对应的包装类,否则返回本身​​
  • ​​十、完整代码​​

一、准备

0、MethodUtils 主要作用

主要用于通过反射技术操作对象的方法:执行方法、精确执行方法、精确执行静态方法、获取可访问方法等;
普通和精确的区别:普通的会找到兼容类型的方法,精确的只会匹配到确切的参数类型的方法。

1、引入 commons-beanutils 依赖

<!--引入依赖commons-beanutils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>

2、pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zibo</groupId>
<artifactId>zibo2022</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zibo2022</name>
<description>zibo2022</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--引入依赖commons-beanutils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>

3、实体类父类

package com.zibo.zibo2022.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author zibo
* @date 2022/7/12 0012 21:17
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class God {

/**
* 数字
*/
private Integer num;
}

4、实体类子类

package com.zibo.zibo2022.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
* @author zibo
* @date 2022/7/12 0012 20:09
*/
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class People extends God {

/**
* 名字
*/
private String name;

/**
* 年龄
*/
private Integer age;

/**
* 地址
*/
private String address;

/**
* 两个参数的 setName 方法
* @param name1 名字1
* @param name2 名字2
*/
public void setName(String name1, String name2) {
this.name = name1 + name2;
}

/**
* 单参数静态方法:say
* @param word
*/
public static void say(String word) {
System.out.println(word);
System.out.println("单参数静态方法:say");
}

/**
* 多参数静态方法:say
* @param word1
* @param word2
*/
public static void say(String word1, String word2) {
System.out.println(word1 + word2);
System.out.println("多参数静态方法:say");
}

/**
* 无参数静态方法:say
*/
public static void say() {
System.out.println("无参数静态方法:say");
}

}

二、缓存

0、前置代码

People people = new People("zibo", 18, "beijing");

1、设置是否应该缓存方法以获得更高的性能,默认为true

// 1、设置是否应该缓存方法以获得更高的性能,默认为true。
MethodUtils.setCacheMethods(true);

2、清空方法缓存

// 2、清空方法缓存
MethodUtils.clearCache();

三、执行方法

1、执行方法:单个参数 + 自动获取类型

// 3、执行方法:单个参数 + 自动获取类型
try {
// 参数:对象,方法名,参数
MethodUtils.invokeMethod(people, "setName", "大哥");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥

2、执行方法:多个参数 + 自动获取类型

// 4、执行方法:多个参数 + 自动获取类型
Object[] args1 = new Object[] {26};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeMethod(people, "setAge", args1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("age: " + people.getAge()); // age: 26

3、执行方法:单个参数 + 手动设置类型

// 5、执行方法:单个参数 + 手动设置类型
Object[] args2 = new Object[] {"河南省"};
Class<?>[] types = new Class[] {String.class};
try {
// 参数:对象,方法名,参数,参数类型
MethodUtils.invokeMethod(people, "setAddress", args2, types);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("address: " + people.getAddress()); // address: 河南省

四、精确执行方法

执行无参方法,参数传 null 即可,参考精【确执行静态方法】!

1、精确执行方法,此处我们精确调用有 1 个参数的方法 + 自动获取类型

// 6、精确执行方法,此处我们精确调用有 1 个参数的方法 + 自动获取类型
try {
// 参数:对象,方法名,参数
MethodUtils.invokeExactMethod(people, "setName", "大哥刘备");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥刘备

2、精确执行方法:此处我们精确调用有 2 个参数的方法 + 自动获取类型

// 6、精确执行方法:此处我们精确调用有 2 个参数的方法 + 自动获取类型
Object[] args3 = new Object[] {"大哥的名字叫做", "刘备"};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeExactMethod(people, "setName", args3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥的名字叫做刘备

3、精确执行方法:此处我们精确调用有 2 个参数的方法 + 手动设置类型

// 7、精确执行方法:此处我们精确调用有 2 个参数的方法 + 手动设置类型
Class<?>[] types1 = new Class[] {String.class, String.class};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeExactMethod(people, "setName", args3, types1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥的名字叫做刘备

五、精确指定静态方法

1、精确执行静态方法:单个参数 + 自动获取类型

// 8、精确执行静态方法:单个参数 + 自动获取类型
try {
// 参数:类名,方法名,参数
MethodUtils.invokeStaticMethod(People.class, "say", "大哥,快来啦!");
// 大哥,快来啦!
// 单参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

2、精确执行静态方法:多个参数 + 自动获取类型

// 9、精确执行静态方法:多个参数 + 自动获取类型
Object[] args4 = new Object[] {"大哥,快来啦!", "大哥,快来啦!"};
try {
// 参数:类名,方法名,参数列表,参数类型列表
MethodUtils.invokeStaticMethod(People.class, "say", args4);
// 大哥,快来啦!大哥,快来啦!
// 多参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

3、精确执行静态方法:多个参数 + 手动设置类型

// 10、精确执行静态方法:多个参数 + 手动设置类型
Object[] args5 = new Object[] {"大哥,快来啦!", "大哥,快来啦!"};
Class<?>[] types2 = new Class[] {String.class, String.class};
try {
// 参数:类名,方法名,参数列表,参数类型列表
MethodUtils.invokeStaticMethod(People.class, "say", args5, types2);
// 大哥,快来啦!大哥,快来啦!
// 多参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

4、精确执行静态方法:无参数

try {
// 参数:类名,方法名,参数
// 参数设置为 null 可调用无参方法
MethodUtils.invokeStaticMethod(People.class, "say", null);
// 无参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

六、获取可访问方法

1、获取一个可访问的方法:类名,方法名,参数类型

// 12、获取一个可访问的方法:类名,方法名,参数类型
Method method = MethodUtils.getAccessibleMethod(People.class, "setName", String.class);
try {
method.invoke(people, "二哥关羽");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽

2、获取一个可访问的方法:类名,方法名,参数类型列表

// 13、获取一个可访问的方法:类名,方法名,参数类型列表
Class<?>[] types3 = new Class[] {String.class, String.class};
Method method1 = MethodUtils.getAccessibleMethod(People.class, "setName", types3);
try {
method1.invoke(people, "二哥关羽", "三哥张飞");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽三哥张飞

七、获取指定方法的可访问方法

1、获取指定方法的可访问方法:找不到这个方法返回空(测试1)

// 14、获取指定方法的可访问方法:找不到这个方法返回空(测试1)
try {
Method setName = People.class.getDeclaredMethod("setName", String.class);
Method accessibleMethod = MethodUtils.getAccessibleMethod(setName);
accessibleMethod.invoke(people, "四哥赵云");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 四哥赵云

2、获取指定方法的可访问方法:找不到这个方法返回空(测试2)

// 15、获取指定方法的可访问方法:找不到这个方法返回空(测试2)
// 备注:如果不用工具,此时没有这个方法,会被捕捉到 NoSuchMethodException 异常,搞不懂为何还需要 getAccessibleMethod 这个方法!
try {
Method setName = People.class.getDeclaredMethod("setNumber", Integer.class);
// java.lang.NoSuchMethodException: com.zibo.zibo2022.entity.People.setNumber(java.lang.Integer)
Method accessibleMethod = MethodUtils.getAccessibleMethod(setName);
System.out.println(accessibleMethod);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

3、获取指定方法的可访问方法:找不到这个方法返回空(测试3)

// 16、获取指定方法的可访问方法:找不到这个方法返回空(测试3)
// 使用工具获取方法:我知道了,因为使用工具获取方法,如果没有也不会报错,因此使用这个方法来判断是否有此方法!
Method method2 = MethodUtils.getAccessibleMethod(People.class, "setNumber", types3);
Method method3 = MethodUtils.getAccessibleMethod(method2);
System.out.println(method3); // null

4、获取指定方法的可访问方法:类 + 方法

// 17、获取指定方法的可访问方法:类 + 方法
Method method4 = MethodUtils.getAccessibleMethod(People.class, method2);
System.out.println(method4); // null

5、获取与指定方法名称相同且与参数类型列表匹配的可访问方法

// 18、获取与指定方法名称相同且与参数类型列表匹配的可访问方法
Method method5 = MethodUtils.getMatchingAccessibleMethod(People.class, "setName", types3);
try {
method5.invoke(people, "二哥关羽", "三哥张飞");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽三哥张飞

八、确定一个类型是否可以用作方法调用中的参数

我想实际使用中没这么简单!

// 19、确定一个类型是否可以用作方法调用中的参数。
boolean isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, Integer.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, String.class);
System.out.println(isAssignableFrom); // true
isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, Object.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(Integer.class, Object.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(Integer.class, Long.class);
System.out.println(isAssignableFrom); // false

九、基本类型 & 包装类型

1、获取一个基本类型的包装类型

// 20、获取一个基本类型的包装类型
Class<?> primitiveType = MethodUtils.getPrimitiveWrapper(int.class);
System.out.println(primitiveType); // class java.lang.Integer

2、获得包装类的基本数据类型

// 21、获得包装类的基本数据类型
Class<?> primitiveType2 = MethodUtils.getPrimitiveType(Integer.class);
System.out.println(primitiveType2); // int

3、如果是简单数据类型则返回对应的包装类,否则返回本身

// 22、如果是简单数据类型则返回对应的包装类,否则返回本身
Class<?> primitiveType3 = MethodUtils.toNonPrimitiveClass(int.class);
System.out.println(primitiveType3); // class java.lang.Integer
Class<?> primitiveType4 = MethodUtils.toNonPrimitiveClass(Integer.class);
System.out.println(primitiveType4); // class java.lang.Integer

十、完整代码

package com.zibo.zibo2022.main;

import com.zibo.zibo2022.entity.People;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.MethodUtils;

/**
* @author zibo
* @date 2022/7/12 0012 20:11
*/
public class Main {

public static void main(String[] args) {
People people = new People("zibo", 18, "beijing");
// 1、设置是否应该缓存方法以获得更高的性能,默认为true。
MethodUtils.setCacheMethods(true);
// 2、清空方法缓存
MethodUtils.clearCache();

// 3、执行方法:单个参数 + 自动获取类型
try {
// 参数:对象,方法名,参数
MethodUtils.invokeMethod(people, "setName", "大哥");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥

// 4、执行方法:多个参数 + 自动获取类型
Object[] args1 = new Object[] {26};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeMethod(people, "setAge", args1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("age: " + people.getAge()); // age: 26

// 5、执行方法:单个参数 + 手动设置类型
Object[] args2 = new Object[] {"河南省"};
Class<?>[] types = new Class[] {String.class};
try {
// 参数:对象,方法名,参数,参数类型
MethodUtils.invokeMethod(people, "setAddress", args2, types);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("address: " + people.getAddress()); // address: 河南省

// 6、精确执行方法,此处我们精确调用有 1 个参数的方法 + 自动获取类型
try {
// 参数:对象,方法名,参数
MethodUtils.invokeExactMethod(people, "setName", "大哥刘备");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥刘备

// 6、精确执行方法:此处我们精确调用有 2 个参数的方法 + 自动获取类型
Object[] args3 = new Object[] {"大哥的名字叫做", "刘备"};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeExactMethod(people, "setName", args3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥的名字叫做刘备

// 7、精确执行方法:此处我们精确调用有 2 个参数的方法 + 手动设置类型
Class<?>[] types1 = new Class[] {String.class, String.class};
try {
// 参数:对象,方法名,参数列表,参数类型列表
MethodUtils.invokeExactMethod(people, "setName", args3, types1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 大哥的名字叫做刘备

// 8、精确执行静态方法:单个参数 + 自动获取类型
try {
// 参数:类名,方法名,参数
MethodUtils.invokeStaticMethod(People.class, "say", "大哥,快来啦!");
// 大哥,快来啦!
// 单参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

// 9、精确执行静态方法:多个参数 + 自动获取类型
Object[] args4 = new Object[] {"大哥,快来啦!", "大哥,快来啦!"};
try {
// 参数:类名,方法名,参数列表,参数类型列表
MethodUtils.invokeStaticMethod(People.class, "say", args4);
// 大哥,快来啦!大哥,快来啦!
// 多参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

// 10、精确执行静态方法:多个参数 + 手动设置类型
Object[] args5 = new Object[] {"大哥,快来啦!", "大哥,快来啦!"};
Class<?>[] types2 = new Class[] {String.class, String.class};
try {
// 参数:类名,方法名,参数列表,参数类型列表
MethodUtils.invokeStaticMethod(People.class, "say", args5, types2);
// 大哥,快来啦!大哥,快来啦!
// 多参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}

// 11、精确执行静态方法:无参数
try {
// 参数:类名,方法名,参数
// 参数设置为 null 可调用无参方法
MethodUtils.invokeStaticMethod(People.class, "say", null);
// 无参数静态方法:say
} catch (Exception e) {
e.printStackTrace();
}


// 12、获取一个可访问的方法:类名,方法名,参数类型
Method method = MethodUtils.getAccessibleMethod(People.class, "setName", String.class);
try {
method.invoke(people, "二哥关羽");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽

// 13、获取一个可访问的方法:类名,方法名,参数类型列表
Class<?>[] types3 = new Class[] {String.class, String.class};
Method method1 = MethodUtils.getAccessibleMethod(People.class, "setName", types3);
try {
method1.invoke(people, "二哥关羽", "三哥张飞");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽三哥张飞

// 14、获取指定方法的可访问方法:找不到这个方法返回空(测试1)
try {
Method setName = People.class.getDeclaredMethod("setName", String.class);
Method accessibleMethod = MethodUtils.getAccessibleMethod(setName);
accessibleMethod.invoke(people, "四哥赵云");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 四哥赵云

// 15、获取指定方法的可访问方法:找不到这个方法返回空(测试2)
// 备注:如果不用工具,此时没有这个方法,会被捕捉到 NoSuchMethodException 异常,搞不懂为何还需要 getAccessibleMethod 这个方法!
try {
Method setName = People.class.getDeclaredMethod("setNumber", Integer.class);
// java.lang.NoSuchMethodException: com.zibo.zibo2022.entity.People.setNumber(java.lang.Integer)
Method accessibleMethod = MethodUtils.getAccessibleMethod(setName);
System.out.println(accessibleMethod);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

// 16、获取指定方法的可访问方法:找不到这个方法返回空(测试3)
// 使用工具获取方法:我知道了,因为使用工具获取方法,如果没有也不会报错,因此使用这个方法来判断是否有此方法!
Method method2 = MethodUtils.getAccessibleMethod(People.class, "setNumber", types3);
Method method3 = MethodUtils.getAccessibleMethod(method2);
System.out.println(method3); // null

// 17、获取指定方法的可访问方法:类 + 方法
Method method4 = MethodUtils.getAccessibleMethod(People.class, method2);
System.out.println(method4); // null

// 18、获取与指定方法名称相同且与参数类型列表匹配的可访问方法
Method method5 = MethodUtils.getMatchingAccessibleMethod(People.class, "setName", types3);
try {
method5.invoke(people, "二哥关羽", "三哥张飞");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("name: " + people.getName()); // name: 二哥关羽三哥张飞

// 19、确定一个类型是否可以用作方法调用中的参数。
boolean isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, Integer.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, String.class);
System.out.println(isAssignableFrom); // true
isAssignableFrom = MethodUtils.isAssignmentCompatible(String.class, Object.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(Integer.class, Object.class);
System.out.println(isAssignableFrom); // false
isAssignableFrom = MethodUtils.isAssignmentCompatible(Integer.class, Long.class);
System.out.println(isAssignableFrom); // false

// 20、获取一个基本类型的包装类型
Class<?> primitiveType = MethodUtils.getPrimitiveWrapper(int.class);
System.out.println(primitiveType); // class java.lang.Integer

// 21、获得包装类的基本数据类型
Class<?> primitiveType2 = MethodUtils.getPrimitiveType(Integer.class);
System.out.println(primitiveType2); // int

// 22、如果是简单数据类型则返回对应的包装类,否则返回本身
Class<?> primitiveType3 = MethodUtils.toNonPrimitiveClass(int.class);
System.out.println(primitiveType3); // class java.lang.Integer
Class<?> primitiveType4 = MethodUtils.toNonPrimitiveClass(Integer.class);
System.out.println(primitiveType4); // class java.lang.Integer

}

}


举报

相关推荐

0 条评论