07-06
07
利用反射机制:分析类能力
作者:Java伴侣 日期:2007-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 + ";"); //属性名
}
}
}
加入用户输入的是:
输出结果为:
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页
复制内容到剪贴板 程序代码
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页
评论: 0 | 引用: 0 | 查看次数: 1477
发表评论