zoukankan      html  css  js  c++  java
  • 反射

    Field[] getFields()

    Field[] getDeclaredFields()

    getFields 返回一个包含Field对象的数组,这些对象记录了这个类或其他超类的公有域。

    getDeclaredFields 返回包含Field对象的数组,记录了这个类的全部域。

    如果类没有域,或者Class对象描述的是基本类型或数组类型,这将返回一个长度为0的数组。

    Method[] getMethods()

    Method[] getDeclareMethods() 返回包含Mehod对象的数组,getMethods将返回所有的公有方法,包括从超类继承的公有方法;

    Constructor[] getConstructors() 返回这个类或接口的全部方法,但不包括有超类继承了的方法。

    Constructor[] getDeclaredConstructors() 返回包含Constructor对象的数组,其中包含了Class对象所描述的类的所有公有构造器(getConstructors)或所有构造器(getDeclaredConstructors).

    ---------------------------------

    java.lang.reflect Constructor

    Class getDeclaringClass()  返回一个用于描述类中定义的构造器,方法或域的Class对象

    Class[] getExceptionTypes()  (在Constructor和Method类中)返回一个用于描述方法抛出的异常类型的Class对象数组

    int getModifiers()  返回一个用于描述构造器,方法或域的修饰符的整形数值。使用Modifier类中的这个方法可以分析这个返回值

    String getName()  返回一个用于描述构造器,方法或域名的字符串

    Class[] getParameterType()  返回一个用于描述参数类型的Class对象数组

    Class getReturnType()  返回一个用于描述返回类型的Class对象

    ---------------------------------

    java.lang.reflect.Modifier

    static String toString(int modifiers)

    返回对应modifiers中位设置的修饰符的字符串表示

    static boolean isAbstract(int modifiers)

    static boolean isFinal(int modifiers)

    static boolean isInterface(int modifiers) 

    static boolean isNative(int modifiers)

    static boolean isPrivate(int modifiers)

    static boolean isProtected(int modifiers)

    static boolean isPublic(int modifiers)

    static boolean isStatic(int modifiers)

    static boolean isStrict(int modifiers)

    static boolean isSynchronized(int modifiers)

    static boolean isVolatile(int modifiers)

    这些方法将检测方法名中对应的修饰符在modifiers值中的位。

    ---------------------------------

      1 package reflection;
      2 
      3 import java.lang.reflect.Constructor;
      4 import java.lang.reflect.Field;
      5 import java.lang.reflect.Method;
      6 import java.lang.reflect.Modifier;
      7 import java.util.Scanner;
      8 
      9 public class ReflectionTest {
     10 
     11     public static void main(String[] args) {
     12         //read class  name from command line args or user input
     13         String name;
     14         if(args.length > 0) {
     15             name = args[0];
     16         }
     17         else {
     18             Scanner in = new Scanner(System.in);
     19             System.out.println("Enter class name (e.g. java.util.Date):");
     20             name = in.next();
     21         }
     22         
     23         try{
     24             //print class name and superclass name (if != Object)
     25             Class c1 = Class.forName(name);
     26             Class superc1 = c1.getSuperclass();
     27             String modifiers = Modifier.toString(c1.getModifiers());
     28             if(modifiers.length() > 0 ){
     29                 System.out.println(modifiers + " ");                
     30             }
     31             System.out.println("class " + name);
     32             if(superc1 != null && superc1 != Object.class)
     33                 System.out.println(" extens " + superc1.getName());
     34             System.out.print("
    {
    ");
     35             printConstructors(c1);
     36             System.out.println();
     37             printMethods(c1);
     38             System.out.println();
     39             printFields(c1);
     40             System.out.println("}");
     41         }
     42         catch (ClassNotFoundException e) {
     43             e.printStackTrace();
     44         }
     45         System.exit(0);
     46     }
     47 
     48     /**
     49      * Prints all constructors of a class
     50      * @param c1 a class
     51      */
     52     public static void printConstructors(Class c1) {
     53         Constructor[] constructors = c1.getDeclaredConstructors();
     54         for(Constructor c : constructors) {
     55             String name = c.getName();
     56             System.out.print(" ");
     57             String modifiers = Modifier.toString(c.getModifiers());
     58             if(modifiers.length()>0) System.out.print(modifiers +" ");
     59             System.out.print(name + "(");
     60             
     61             Class[] paramTypes = c.getParameterTypes();
     62             for(int j = 0; j < paramTypes.length; j++) {
     63                 if(j > 0){
     64                     System.out.print(", ");
     65                     System.out.print(paramTypes[j].getName());
     66                 }
     67             }
     68             System.out.println(");");
     69         }
     70     }
     71     
     72     /**
     73      * Prints all methods of a class
     74      * @param c1 a class
     75      */
     76     public static void printMethods(Class c1) {
     77         Method[] methods = c1.getDeclaredMethods();
     78         for(Method m : methods) {
     79             Class retType = m.getReturnType();
     80             String name = m.getName();
     81             
     82             System.out.print("  ");
     83             // print modifiers, return type and method name
     84             String modifiers = Modifier.toString(m.getModifiers());
     85             if(modifiers.length()>0) {
     86                 System.out.print(modifiers + " ");
     87             }
     88             System.out.print(retType.getName() + " " + name + "(");
     89             
     90             // print parameter types
     91             Class[] paramTypes = m.getParameterTypes();
     92             for(int j = 0; j<paramTypes.length; j++) {
     93                 if(j > 0) System.out.print(", ");
     94                 System.out.print(paramTypes[j].getName());
     95             }
     96             System.out.println(");");
     97         }
     98     }
     99     
    100     /**
    101      *  Prints all fields of a class
    102      *  @param c1 a class 
    103      */
    104     public static void printFields(Class c1) {
    105         Field[] field = c1.getDeclaredFields();
    106         
    107         for (Field f : field) {
    108             Class type = f.getType();
    109             String name = f.getName();
    110             System.out.print("  ");
    111             String modifies  = Modifier.toString(f.getModifiers());
    112             if(modifies.length() > 0) {
    113                 System.out.print(modifies + " ");
    114             }
    115             System.out.println(type.getName() + " " + name + ";");
    116         }
    117     }
    118 }
  • 相关阅读:
    最后完整的excel.java文件
    *将制定类型的List写入Excel中
    初始化Excel表格
    单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等
    生成写入excel
    activity_daochu.xml代码
    设计了activity_daochu.xml
    个人作业第二阶段
    阅读笔记一-1软件=程序+软件工程
    每日总结
  • 原文地址:https://www.cnblogs.com/linst/p/4973081.html
Copyright © 2011-2022 走看看