07-06
07

利用反射机制:分析类能力

功能:这个程序将提示用户输入一个类名。然后,输入类中所有的方法和构造器的签名,以及全部域名。

package reflection;

import java.util.Scanner;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.lang.reflect.Method;
import java.lang.reflect.Field;

public class ReflectionTest {
    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(e.g java.util.Date)");
            //输入一个类名
            name = in.next();
        }
        try {
            Class cl = Class.forName(name); //类类型对象
            Class supercl = cl.getSuperclass(); //取得父类
            System.out.print("class " + name);
            if (supercl != null && supercl != Object.class) { //如果有父类的话
                System.out.print("extends" + supercl.getName());
            }
            System.out.print("\n{\n"); //回车:\n
            printConstructors(cl);
            printMethods(cl);
            
            System.out.println();
            printFields(cl);
            System.out.print("}");

        } catch (java.lang.ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.exit(0);

    }

    public static void printConstructors(Class cl) { //打印构造器
        Constructor[] constructor = cl.getDeclaredConstructors(); //取得全部本类构造
        //for(Constructor c:constructor)
        for (int i = 0; i < constructor.length; i++) { //遍历
            String name = constructor[i].getName();
            System.out.print(" " +
                             Modifier.toString(constructor[i].getModifiers())); //取得修饰符
            System.out.print(" " + name + "("); //构造器名

            Class[] paramTypes = constructor[i].getParameterTypes(); //参数

            for (int j = 0; j < paramTypes.length; j++) {
                if (j > 0) {
                    System.out.print(","); //如果参数大于1
                }
                System.out.print(paramTypes[j].getName());
            }
            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(" " + Modifier.toString(m.getModifiers())); //修饰符
            System.out.print(" " + retType.getName() + " " + name + "("); //方法名

            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(");");
        }
    }

    public static void printFields(Class cl) { //  方法属性/域
        Field[] field = cl.getDeclaredFields();

        for (Field f : field) {
            Class type = f.getType();
            String name = f.getName();
            System.out.print(" " + Modifier.toString(f.getModifiers())); //修饰符
            System.out.println(" " + type.getName() + " " + name + ";"); //属性名
        }
    }
}


加入用户输入的是:
引用内容 引用内容
java.lang.Boolean


输出结果为:
引用内容 引用内容

class java.lang.Boolean
{
public java.lang.Boolean(java.lang.String);
public java.lang.Boolean(boolean);
public int hashCode();
public volatile int compareTo(java.lang.Object);
public int compareTo(java.lang.Boolean);
public static boolean getBoolean(java.lang.String);
public boolean equals(java.lang.Object);
public java.lang.String toString();
public static java.lang.String toString(boolean);
public static java.lang.Boolean valueOf(boolean);
public static java.lang.Boolean valueOf(java.lang.String);
public boolean booleanValue();
public static boolean parseBoolean(java.lang.String);
private static boolean toBoolean(java.lang.String);

public static final java.lang.Boolean TRUE;
public static final java.lang.Boolean FALSE;
public static final java.lang.Class TYPE;
private final boolean value;
private static final long serialVersionUID;
}


源于P184页

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Java核心技术
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1475
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭