2021年02月20日反射

 

☆20201110反射(tomcat)   //tomcat如何反射

 

java核心卷反射练习

 

 

 

代码:练习 java核心卷答应

  1 package mianshi_0219;
  2 
  3 
  4 import java.lang.reflect.Constructor;
  5 import java.lang.reflect.Field;
  6 import java.lang.reflect.Method;
  7 import java.lang.reflect.Modifier;
  8 import java.util.Scanner;
  9 
 10 /**
 11  * 这个项目用反射打印一个类的所有信息
 12  * @author Administrator
 13  *
 14  */
 15 public class ReflectionTest {
 16     public static void main(String[] args) {
 17         //read class name from command line args or user input 从命令行参数和人们输入的,打印类名
 18         String name;
 19         if(args.length > 0){
 20             name = args[0];
 21         }else {
 22             Scanner in = new Scanner(System.in);
 23             System.out.println("Enter class name(e.g. java.util.Date):");
 24             name = in.next();
 25         }
 26 
 27         try {
 28             Class cl = Class.forName(name);
 29 
 30             Class supercl = cl.getSuperclass();
 31             String modeifiers = Modifier.toString(cl.getModifiers());
 32             if(modeifiers.length() > 0 ){
 33                 System.out.print(modeifiers + " ");
 34             }
 35             System.out.print("class " + name);
 36             if (supercl != null && supercl != Object.class) {
 37                 System.out.print(" extends " + supercl.getName());
 38             }
 39 
 40             System.out.print("\n{\n");
 41             printConstructors(cl);
 42             System.out.println();
 43             printMethods(cl);
 44             System.out.println();
 45             printFields(cl);
 46             System.out.println("}");
 47         } catch (ClassNotFoundException e) {
 48             e.printStackTrace();
 49         }
 50         System.exit(0);
 51     }
 52 /**
 53  * prints all constructor of a class 打印类的所有构造器
 54  * @param cl
 55  */
 56     public static void printConstructors(Class cl) {
 57         Constructor[] constructors = cl.getDeclaredConstructors();
 58         for (Constructor c : constructors) {
 59             String name = c.getName();
 60             System.out.print(" ");
 61             String modifiers = Modifier.toString(c.getModifiers());
 62             if (modifiers.length() > 0) {
 63                 System.out.print(modifiers + " ");
 64             }
 65             System.out.print(name + "(");
 66 
 67             Class[] paramTypes = c.getParameterTypes();
 68             for (int j = 0; j < paramTypes.length; j++) {
 69                 if (j > 0) {
 70                     System.out.print(", ");
 71                 }
 72                 System.out.print(paramTypes[j].getName());
 73             }
 74             System.out.println(");");
 75         }
 76     }
 77 
 78 /**
 79  * 打印类的所有方法
 80  * @param cl
 81  */
 82     public static void printMethods(Class cl)
 83     {
 84         Method[] methods = cl.getDeclaredMethods() ;
 85         for (Method m : methods)
 86         {
 87             Class retType = m.getReturnType();
 88             String name = m. getName();
 89             System.out.print(" ");
 90 
 91             String modifiers = Modifier.toString(m.getModifiers());
 92             if (modifiers.length() > 0) {
 93                 System.out.print(modifiers + " ");
 94             }
 95                     System. out.print(retType. getName() + " " + name + "(");
 96 
 97             Class[] paramTypes = m.getParameterTypes();
 98             for (int j = 0; j < paramTypes.length; j++)
 99             {
100                 if (j > 0) {
101                     System.out.print(" , ");
102                 }
103                 System.out.print(paramTypes[j].getName());
104             }
105             System.out.println(") ;");
106         }
107     }
108 
109 /**
110  * prints all fields of a class
111  * @param cl
112  */
113     public static void printFields(Class cl) {
114         Field[] fields = cl.getDeclaredFields();
115         for (Field f : fields) {
116             Class type = f.getType();
117             String name = f.getName();
118             System.out.print(" ");
119             String modifiers = Modifier.toString(f.getModifiers());
120             if (modifiers.length() > 0) {
121                 System.out.print(modifiers + " ");
122             }
123             System.out.println(type.getName() + " " + name + ";");
124         }
125     }
126 }

 

posted @ 2021-02-22 21:53  八佰山兵上北坡  阅读(39)  评论(0编辑  收藏  举报