JAVA的RTTI 和Reflect

打印一个类信息:

import java.lang.reflect.*;
import java.util.*;


public class ReflectTest {
  public static void main(String[] args) throws Exception{
    String cln;
    if(args.length > 0) 
      cln = args[0];
    else 
      cln = "java.util.Date";
     
    Class cl = Class.forName(cln);
    Class supercl = cl.getSuperclass();
    String modifiers = Modifier.toString(cl.getModifiers());
    if(modifiers.length()> 0 ) System.out.println(modifiers + " ");
    System.out.print("class "+ cl.getSimpleName());
    if(supercl != null && supercl!= Object.class) {
      System.out.println(" extends " + supercl.getSimpleName()+"{");
    }
    
    printConstructors(cl);
    printFields(cl);
    printMethods(cl);
  }

  private static void printConstructors(Class cl) {
    Constructor[] constructors = cl.getDeclaredConstructors();
    
    for(Constructor constructor: constructors) {
      String modifiers = Modifier.toString(constructor.getModifiers());
      String conName = constructor.getName();
      Class[] params = constructor.getParameterTypes();
      StringBuilder paramStringBuilder = new StringBuilder();

      for(int i =0; i< params.length -1; i++) {
        paramStringBuilder.append(params[i].getTypeName());
        paramStringBuilder.append(", ");
      }
      
      if(params.length > 1) {
        paramStringBuilder.append(params[params.length -1].getTypeName());
      }

      System.out.println(modifiers + " " + conName + "(" + paramStringBuilder.toString() + ");");
    }
  }

  private static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for(Method method: methods) {
      String modifiers = Modifier.toString(method.getModifiers());
      String methodName = method.getName();
      Class[] params = method.getParameterTypes();
      String returnType = method.getReturnType().getSimpleName();

      StringBuilder methodParamTypes = new StringBuilder();

      for(int i =0; i< params.length -1; i++) {
        methodParamTypes.append(params[i].getTypeName());
        methodParamTypes.append(", ");
      }

      if(params.length >1) {
        methodParamTypes.append(params[params.length-1].getTypeName());
      }

      System.out.println(modifiers+ " " + returnType+ " " +methodName + "(" + methodParamTypes.toString() + ");");
    }
  }

  private static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for(Field field: fields) {
      String modifiers = Modifier.toString(field.getModifiers());
      Class type = field.getType();
      System.out.println(modifiers + " " + type.getSimpleName() + " " + field.getName());
    }
  }
}

以上代码使用了一些关键的函数,分别是:
getDeclaredConstructors
getDeclaredMethods
getDeclaredFields


对象的访问符打印:
String modifiers = Modifier.toString(Class|Constructor|Method|Field cl.getModifiers());

对象的访问

可以使用操作将对象的方法全部设置为公共方法:

    for(Method method: methods) {
      method.setAccessible(true);
      method.invoke(object, args);
    }

note:以上代码是没有意义的,因为每个成员方法所需的参数不同

同理, 对于成员也可以使用该方法将所有成员设置为公共。

你可能感兴趣的:(JAVA的RTTI 和Reflect)