Java: Reflection in jdk 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * * 历史版本: JDK 17.01 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuReflection.java * Interface * Record * Annotation * Enum * */ package CoreJava.twelfth; import java.util.*; import java.lang.reflect.*; /** * * @version 1.0 * @author geovindu * */ public class DuReflection { /** * Prints all constructors of a class * @param cl a class */ public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (Constructor c : constructors) { String name = c.getName(); System.out.print( " " ); String modifiers = Modifier.toString(c.getModifiers()); if (modifiers.length() > 0 ) System.out.print(modifiers + " " ); System.out.print(name + "(" ); // print parameter types Class[] paramTypes = c.getParameterTypes(); for ( int j = 0 ; j < paramTypes.length; j++) { if (j > 0 ) System.out.print( ", " ); System.out.print(paramTypes[j].getName()); } System.out.println( ");" ); } } /** * Prints all methods of a class * @param cl a class */ public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods(); for (Method m : methods) { Class retType = m.getReturnType(); String name = m.getName(); System.out.print( " " ); // print modifiers, return type and method name String modifiers = Modifier.toString(m.getModifiers()); if (modifiers.length() > 0 ) System.out.print(modifiers + " " ); System.out.print(retType.getName() + " " + name + "(" ); // print parameter types Class[] paramTypes = m.getParameterTypes(); for ( int j = 0 ; j < paramTypes.length; j++) { if (j > 0 ) System.out.print( ", " ); System.out.print(paramTypes[j].getName()); } System.out.println( ");" ); } } /** * Prints all fields of a class * @param cl a class */ public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (Field f : fields) { Class type = f.getType(); String name = f.getName(); System.out.print( " " ); String modifiers = Modifier.toString(f.getModifiers()); if (modifiers.length() > 0 ) System.out.print(modifiers + " " ); System.out.println(type.getName() + " " + name + ";" ); } } /** * Prints all permitted subtypes of a sealed class * @param cl a class */ public static void printPermittedSubclasses(Class cl) { if (cl.isSealed()) { Class<?>[] permittedSubclasses = cl.getPermittedSubclasses(); for ( int i = 0 ; i < permittedSubclasses.length; i++) { if (i == 0 ) System.out.print( " permits " ); else System.out.print( ", " ); System.out.print(permittedSubclasses[i].getName()); } } } /** * Prints all directly implemented interfaces of a class * @param cl a class */ public static void printInterfaces(Class cl) { Class<?>[] interfaces = cl.getInterfaces(); for ( int i = 0 ; i < interfaces.length; i++) { if (i == 0 ) System.out.print(cl.isInterface() ? " extends " : " implements " ); else System.out.print( ", " ); System.out.print(interfaces[i].getName()); } } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public static void main(String[] args) throws ReflectiveOperationException { try { String name; if (args.length > 0 ) name = args[ 0 ]; else { var in = new Scanner(System.in); System.out.println( "Enter class name (e.g. java.util.Date): " ); name = in.next(); } // print class modifiers, name, and superclass name (if != Object) Class cl = Class.forName(name); String modifiers = Modifier.toString(cl.getModifiers()); if (modifiers.length() > 0 ) System.out.print(modifiers + " " ); if (cl.isSealed()) System.out.print( "sealed " ); if (cl.isEnum()) System.out.print( "enum " + name); else if (cl.isRecord()) System.out.print( "record " + name); else if (cl.isInterface()) System.out.print( "interface " + name); else System.out.print( "class " + name); Class supercl = cl.getSuperclass(); if (supercl != null && supercl != Object. class ) System.out.print( " extends " + supercl.getName()); DuReflection.printInterfaces(cl); DuReflection.printPermittedSubclasses(cl); System.out.print( "\n{\n" ); DuReflection.printConstructors(cl); System.out.println(); DuReflection.printMethods(cl); System.out.println(); DuReflection.printFields(cl); System.out.println( "}" ); } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | Enter class name (e.g. java.util.Date): java.util.Date public class java.util.Date implements java.io.Serializable, java.lang.Cloneable, java.lang.Comparable { public java.util.Date(java.lang.String); public java.util.Date( int , int , int , int , int , int ); public java.util.Date( int , int , int , int , int ); public java.util.Date(); public java.util.Date( long ); public java.util.Date( int , int , int ); public boolean equals(java.lang.Object); public java.lang.String toString(); public int hashCode(); public java.lang.Object clone(); public volatile int compareTo(java.lang.Object); public int compareTo(java.util.Date); public static java.util.Date from(java.time.Instant); private void readObject(java.io.ObjectInputStream); private void writeObject(java.io.ObjectOutputStream); private final sun.util.calendar.BaseCalendar$Date normalize(sun.util.calendar.BaseCalendar$Date); private final sun.util.calendar.BaseCalendar$Date normalize(); public boolean before(java.util.Date); public boolean after(java.util.Date); public static long parse(java.lang.String); public long getTime(); public int getYear(); public int getSeconds(); public java.time.Instant toInstant(); public static long UTC( int , int , int , int , int , int ); public void setTime( long ); public int getMonth(); static final long getMillisOf(java.util.Date); public void setDate( int ); private final sun.util.calendar.BaseCalendar$Date getCalendarDate(); public void setHours( int ); public int getHours(); public int getMinutes(); public void setMinutes( int ); public void setSeconds( int ); public void setMonth( int ); public void setYear( int ); private static final sun.util.calendar.BaseCalendar getCalendarSystem( int ); private static final sun.util.calendar.BaseCalendar getCalendarSystem( long ); private static final sun.util.calendar.BaseCalendar getCalendarSystem(sun.util.calendar.BaseCalendar$Date); private final long getTimeImpl(); private static final java.lang.StringBuilder convertToAbbr(java.lang.StringBuilder, java.lang.String); private static final synchronized sun.util.calendar.BaseCalendar getJulianCalendar(); public int getDate(); public int getDay(); public java.lang.String toLocaleString(); public java.lang.String toGMTString(); public int getTimezoneOffset(); private static final sun.util.calendar.BaseCalendar gcal; private static sun.util.calendar.BaseCalendar jcal; private transient long fastTime; private transient sun.util.calendar.BaseCalendar$Date cdate; private static int defaultCenturyStart; private static final long serialVersionUID; private static final [Ljava.lang.String; wtb; private static final [I ttb; } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-10-31 sql distinct