package com.sadhu; import java.util.*; import java.lang.reflect.*; /** 反射---输入一个类,打印出类中所有元素 reflect包中有三个描述类元素的类:Field、Method、Constructor */ public class Sample { public static void main(String[] args) { String name;//收集用户输入的类 if(args.length > 0) { name = args[0]; } else { Scanner in = new Scanner(System.in); System.out.println("Enter class name!"); name = in.next(); } try { Class cl = Class.forName(name);//获得用户输入类的Class类型 Class supercl = cl.getSuperclass();//获得用户输入类的超类 String modifiers = Modifier.toString(cl.getModifiers());//获得修饰符数值后用Modifier类解释出来。 if(modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print("class " + name); if(supercl != null)//如果想排除掉顶级根:&& supercl != Object.class { System.out.print(" extends "+supercl.getName()); } System.out.print("\n{\n"); printConstructors(cl);//获得构造函数 System.out.println(); printMethods(cl);//获得方法 System.out.println(); printFields(cl);//获得字段 System.out.println("}"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.exit(0); } 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+"("); Class[] paramTypes = c.getParameterTypes();//获得参数类型列表 for(int j = 0;j < paramTypes.length; j++) { System.out.print(paramTypes[j].getName()); if( j < paramTypes.length-1) { System.out.print(", "); } } System.out.println(");"); } } 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(" "); String modifiers = Modifier.toString(m.getModifiers()); if(modifiers.length() > 0) { System.out.print(modifiers+" "); } System.out.print(retType.getName()+" "+name+"("); Class[] paramTypes = m.getParameterTypes(); for(int j = 0;j < paramTypes.length; j++) { System.out.print(paramTypes[j].getName()); if( j < paramTypes.length-1) { System.out.print(", "); } } System.out.println(");"); } } 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+";"); } } } /** 输出类似结果: public final class java.util.Scanner extends java.lang.Object { private java.util.Scanner(java.nio.file.Path, java.nio.charset.Charset); public java.util.Scanner(java.io.InputStream, java.lang.String); public java.util.Scanner(java.io.InputStream); public java.util.Scanner(java.nio.file.Path); private java.util.Scanner(java.lang.Readable, java.util.regex.Pattern); private java.util.Scanner(java.io.File, java.nio.charset.CharsetDecoder); public java.util.Scanner(java.io.File, java.lang.String); public java.util.Scanner(java.io.File); public java.util.Scanner(java.nio.file.Path, java.lang.String); public java.util.Scanner(java.nio.channels.ReadableByteChannel, java.lang.Stri ng); public java.util.Scanner(java.lang.Readable); public java.util.Scanner(java.nio.channels.ReadableByteChannel); public java.util.Scanner(java.lang.String); public void remove(); public java.lang.String toString(); public boolean hasNext(); public boolean hasNext(java.lang.String); public boolean hasNext(java.util.regex.Pattern); public java.lang.String next(); public volatile java.lang.Object next(); public java.lang.String next(java.util.regex.Pattern); public java.lang.String next(java.lang.String); public void close(); public java.util.Scanner reset(); public double nextDouble(); public int nextInt(int); public int nextInt(); public java.util.regex.MatchResult match(); public java.util.Scanner skip(java.util.regex.Pattern); public java.util.Scanner skip(java.lang.String); private void ensureOpen(); public java.util.Locale locale(); private static java.nio.charset.Charset toCharset(java.lang.String); private static java.util.regex.Pattern boolPattern(); private void buildFloatAndDecimalPattern(); private java.lang.String buildIntegerPatternString(); private void cacheResult(); private void cacheResult(java.lang.String); private void clearCaches(); private java.util.regex.Pattern decimalPattern(); public java.util.regex.Pattern delimiter(); public java.lang.String findInLine(java.util.regex.Pattern); public java.lang.String findInLine(java.lang.String); private java.lang.String findPatternInBuffer(java.util.regex.Pattern, int); public java.lang.String findWithinHorizon(java.util.regex.Pattern, int); public java.lang.String findWithinHorizon(java.lang.String, int); private java.util.regex.Pattern floatPattern(); private java.lang.String getCachedResult(); private java.lang.String getCompleteTokenInBuffer(java.util.regex.Pattern); public boolean hasNextBigDecimal(); public boolean hasNextBigInteger(); public boolean hasNextBigInteger(int); public boolean hasNextBoolean(); public boolean hasNextByte(int); public boolean hasNextByte(); public boolean hasNextDouble(); public boolean hasNextFloat(); public boolean hasNextInt(int); public boolean hasNextInt(); public boolean hasNextLine(); public boolean hasNextLong(int); public boolean hasNextLong(); public boolean hasNextShort(); public boolean hasNextShort(int); private boolean hasTokenInBuffer(); private java.util.regex.Pattern integerPattern(); public java.io.IOException ioException(); private static java.util.regex.Pattern linePattern(); private static java.lang.Readable makeReadable(java.nio.channels.ReadableByteC hannel, java.nio.charset.CharsetDecoder); private static java.lang.Readable makeReadable(java.io.InputStream, java.nio.c harset.Charset); private static java.lang.Readable makeReadable(java.nio.channels.ReadableByteC hannel); private boolean makeSpace(); private java.lang.String matchPatternInBuffer(java.util.regex.Pattern); public java.math.BigDecimal nextBigDecimal(); public java.math.BigInteger nextBigInteger(); public java.math.BigInteger nextBigInteger(int); public boolean nextBoolean(); public byte nextByte(); public byte nextByte(int); public float nextFloat(); public java.lang.String nextLine(); public long nextLong(int); public long nextLong(); public short nextShort(int); public short nextShort(); private java.lang.String processFloatToken(java.lang.String); private java.lang.String processIntegerToken(java.lang.String); public int radix(); private void readInput(); private boolean revertState(boolean); private void revertState(); private void saveState(); private static java.util.regex.Pattern separatorPattern(); private void setRadix(int); private void throwFor(); private static java.nio.charset.CharsetDecoder toDecoder(java.lang.String); private void translateSavedIndexes(int); public java.util.Scanner useDelimiter(java.lang.String); public java.util.Scanner useDelimiter(java.util.regex.Pattern); public java.util.Scanner useLocale(java.util.Locale); public java.util.Scanner useRadix(int); private void useTypeCache(); private java.nio.CharBuffer buf; private static final int BUFFER_SIZE; private int position; private java.util.regex.Matcher matcher; private java.util.regex.Pattern delimPattern; private java.util.regex.Pattern hasNextPattern; private int hasNextPosition; private java.lang.String hasNextResult; private java.lang.Readable source; private boolean sourceClosed; private boolean needInput; private boolean skipped; private int savedScannerPosition; private java.lang.Object typeCache; private boolean matchValid; private boolean closed; private int radix; private int defaultRadix; private java.util.Locale locale; private sun.misc.LRUCache patternCache; private java.io.IOException lastException; private static java.util.regex.Pattern WHITESPACE_PATTERN; private static java.util.regex.Pattern FIND_ANY_PATTERN; private static java.util.regex.Pattern NON_ASCII_DIGIT; private java.lang.String groupSeparator; private java.lang.String decimalSeparator; private java.lang.String nanString; private java.lang.String infinityString; private java.lang.String positivePrefix; private java.lang.String negativePrefix; private java.lang.String positiveSuffix; private java.lang.String negativeSuffix; private static volatile java.util.regex.Pattern boolPattern; private static final java.lang.String BOOLEAN_PATTERN; private java.util.regex.Pattern integerPattern; private java.lang.String digits; private java.lang.String non0Digit; private int SIMPLE_GROUP_INDEX; private static volatile java.util.regex.Pattern separatorPattern; private static volatile java.util.regex.Pattern linePattern; private static final java.lang.String LINE_SEPARATOR_PATTERN; private static final java.lang.String LINE_PATTERN; private java.util.regex.Pattern floatPattern; private java.util.regex.Pattern decimalPattern; static final boolean $assertionsDisabled; } */
反射---输入一个类,打印出类中所有元素
时间: 2024-09-30 21:09:24