ClassUtils 常用方法总结
判断是否为转型
isAssignable(Class<?> cls, Class<?> toClass)等等
@Test
public void testIsAssignable(){
System.out.println("判断是否可以转型.");
System.out.println(ClassUtils.isAssignable(Date.class, Object.class)); //= true
System.out.println(ClassUtils.isAssignable( 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 Object.class, Date.class)); //=false
}
判断是否为内部类
isInnerClass(Class<?> cls)
@Test
public void testIsInnerClass(){
System.out.println(ClassUtils.isInnerClass(Entry.class)); // = ture
System.out.println(ClassUtils.isInnerClass(Object.class)); //= false
}
判读是否为基础类型或者包装类型
isPrimitiveOrWrapper(Class<?> type)
isPrimitiveWrapper(Class<?> type)
@Test
public void testIsPrimitiveWrapper(){
// wrapper 类有(Boolean, Byte, Character, Short, Integer, Long, Double, Float).
System.out.println(ClassUtils.isPrimitiveWrapper(Integer.class)); // = ture
System.out.println(ClassUtils.isPrimitiveWrapper(Object.class)); // = false
System.out.println(ClassUtils.isPrimitiveWrapper(String.class)); // = false
System.out.println(ClassUtils.isPrimitiveWrapper(int.class)); // = false
System.out.println(ClassUtils.isPrimitiveOrWrapper(Integer.class)); // = ture
System.out.println(ClassUtils.isPrimitiveOrWrapper(Object.class)); // = false
System.out.println(ClassUtils.isPrimitiveOrWrapper(String.class)); // = false
System.out.println(ClassUtils.isPrimitiveOrWrapper(int.class)); // = true
}
基础类型和包装类型转换
wrapperToPrimitive(Class<?> cls)
@Test
public void testWrapperToPrimitive(){
System.out.println(ClassUtils.wrapperToPrimitive(Integer.class));// = int
}
获取类名和 class 类互转
convertClassesToClassNames(List<Class<?>> classes)
convertClassNamesToClasses(List<String> classNames)
对象转 Class 对象
toClass(Object... array)
@Test
public void testToClass(){
//类转化为 class 对象
Class[] classAry = ClassUtils.toClass(new String(),new Object());
System.out.println(ArrayUtils.toString(classAry));
// = {class java.lang.String,class java.lang.Object}
}
获取缩略名
getAbbreviatedName(Class<?> cls,int len)
getAbbreviatedName(String className,int len)
@Test
public void testGetAbbreviatedName(){
//获取类的缩略名称
String abbreviatedName1 = ClassUtils.getAbbreviatedName(String.class, 5);
String abbreviatedName2 = ClassUtils.getAbbreviatedName(String.class, 10);
String abbreviatedName3 = ClassUtils.getAbbreviatedName(String.class, 3);
String abbreviatedName4 = ClassUtils.getAbbreviatedName(String.class, 20);
System.out.println(abbreviatedName1);
System.out.println(abbreviatedName2);
System.out.println(abbreviatedName3);
System.out.println(abbreviatedName4);
}
获得所有实现的接口
getAllInterfaces(Class<?> cls)
@Test
public void testGetAllInterfaces(){
//获得所有实现的接口
List<Class<?>> classAry = ClassUtils.getAllInterfaces(String.class);
System.out.println(ArrayUtils.toString(classAry));
}
获取所有父类
getAllSuperclasses(Class<?> cls)
@Test
public void testGetAllSuperclasses(){
//获得所有的父类
List<Class<?>> classArray = ClassUtils.getAllSuperclasses(String.class);
System.out.println(ArrayUtils.toString(classArray));
//[class java.lang.Object]
}
评论