Java反射练习(一)

package com.slliver.reflection;

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier;

/** * 利用反射打印一个类的全部信息,构造方法、方法、属性 * * @author slliver * @date 2017-04-13 21:12 */ public class ReflectionTest {

1public static void main(String\[\] args) { 2 3 String name = "com.slliver.reflection.Student"; 4 5 try { 6 Class classzz = Class.forName(name); 7 // 当前类的父类 8 Class superClass = classzz.getSuperclass(); 9 // 获取当前类的修饰符 10 String modifiers = Modifier.toString(classzz.getModifiers()); 11 if (modifiers.length() > 0) { 12 System.out.println("当前类修饰符 == >>> " + modifiers); 13 } 14 if (superClass != null && superClass != Object.class) { 15 System.out.println("当前类的父类 == >>> " + superClass.getName()); 16 } 17 18 System.out.print("\\n{\\n"); 19 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类构造方法的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 20 printConstructors(classzz); 21 System.out.println(); 22 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类中方法的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 23 printMethods(classzz); 24 System.out.println(); 25 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类中属性的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 26 printFields(classzz); 27 System.out.print("}"); 28 } catch (ClassNotFoundException e) { 29 e.printStackTrace(); 30 } 31 32} 33 34/\*\* 35 \* 当前类构造方法信息,构造方法的修饰符,构造方法参数类型和参数名称 36 \*/ 37private static void printConstructors(Class classzz) { 38 // 返回所有构造器 39 Constructor\[\] constructors = classzz.getDeclaredConstructors(); 40 // 当前类所有构造方法 41 Constructor\[\] currentConstructors = classzz.getConstructors(); 42 for (Constructor constructor : currentConstructors) { 43 String name = constructor.getName(); 44 System.out.print(" "); 45 String modifiers = Modifier.toString(constructor.getModifiers()); 46 if (modifiers.length() > 0) { 47 System.out.print(modifiers + " "); 48 } 49 50 System.out.print(name + "("); 51 // 构造方法参数类型 52 Class\[\] paramTypes = constructor.getParameterTypes(); 53 for (int j = 0; j < paramTypes.length; j++) { 54 if (j > 0) { 55 System.out.print(", "); 56 } 57 // 构造方法参数类型和参数名字 58 System.out.print(paramTypes\[j\].getName()); 59 } 60 System.out.println(");"); 61 } 62} 63 64/\*\* 65 \* 当前类中方法信息 66 \*/ 67private static void printMethods(Class classzz) { 68 // 返回所有的共有方法,包括从超类(父类)继承的共有方法 69 Method\[\] methods = classzz.getMethods(); 70 // 返回当前类的全部方法 71 Method\[\] currentMethods = classzz.getDeclaredMethods(); 72 for (Method method : currentMethods) { 73 // 方法返回值 74 Class returnType = method.getReturnType(); 75 // 方法名 76 String methodName = method.getName(); 77 System.out.print(" "); 78 // 方法的修饰符 79 String modifiers = Modifier.toString(method.getModifiers()); 80 if (modifiers.length() > 0) { 81 System.out.print(modifiers + " "); 82 } 83 System.out.print(returnType.getName() + " " + methodName + " ("); 84 // 方法参数类型 85 Class\[\] paramTypes = method.getParameterTypes(); 86 87 for (int j = 0; j < paramTypes.length; j++) { 88 if (j > 0) { 89 System.out.print(", "); 90 } 91 // 构造方法参数类型和参数名字 92 System.out.print(paramTypes\[j\].getName()); 93 } 94 System.out.println(");"); 95 } 96} 97 98 99/\*\* 100 \* 当前类中属性信息 101 \*/ 102private static void printFields(Class classzz) { 103 // 返回当前类所有公有域(public修饰的属性) 104 Field\[\] publicFields = classzz.getFields(); 105 // 返回当前类所有属性 106 Field\[\] fields = classzz.getDeclaredFields(); 107 for (Field field : fields) { 108 // 属性类型 109 Class type = field.getType(); 110 // 属性名 111 String name = field.getName(); 112 System.out.print(" "); 113 // 属性修饰符 114 String modifiers = Modifier.toString(field.getModifiers()); 115 if (modifiers.length() > 0) { 116 System.out.print(modifiers + " "); 117 } 118 System.out.print(type.getName() + " " + name + ";\\n"); 119 } 120}

}

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )