重点
1.控制域:modifiers :
如 public static final
2.参数:param
java.lang.Object
输出样例
enter class name(e.g.java.util.Date):
java.lang.Double
public final class java.lang.Double extendsjava.lang.Number
{
接口打印
public java.lang.Double(double);
public java.lang.Double(java.lang.String);
方法打印
public retype:boolean equals(/param:java.lang.Object);
public static retype:java.lang.String toString(/param:double);
public retype:java.lang.String toString(/);
public retype:int hashCode(/);
public static retype:int hashCode(/param:double);
public static retype:double min(/param:double , param:double);
public static retype:double max(/param:double , param:double);
public static native retype:long doubleToRawLongBits(/param:double);
public static retype:long doubleToLongBits(/param:double);
public static native retype:double longBitsToDouble(/param:long);
public volatile retype:int compareTo(/param:java.lang.Object);
public retype:int compareTo(/param:java.lang.Double);
public retype:byte byteValue(/);
public retype:short shortValue(/);
public retype:int intValue(/);
public retype:long longValue(/);
public retype:float floatValue(/);
public retype:double doubleValue(/);
public static retype:java.lang.Double valueOf(/param:java.lang.String);
public static retype:java.lang.Double valueOf(/param:double);
public static retype:java.lang.String toHexString(/param:double);
public static retype:int compare(/param:double , param:double);
public static retype:boolean isNaN(/param:double);
public retype:boolean isNaN(/);
public static retype:boolean isFinite(/param:double);
public static retype:boolean isInfinite(/param:double);
public retype:boolean isInfinite(/);
public static retype:double sum(/param:double , param:double);
public static retype:double parseDouble(/param:java.lang.String);
调用域
modifiers :public static final name: double POSITIVE_INFINITY;
modifiers :public static final name: double NEGATIVE_INFINITY;
modifiers :public static final name: double NaN;
modifiers :public static final name: double MAX_VALUE;
modifiers :public static final name: double MIN_NORMAL;
modifiers :public static final name: double MIN_VALUE;
modifiers :public static final name: int MAX_EXPONENT;
modifiers :public static final name: int MIN_EXPONENT;
modifiers :public static final name: int SIZE;
modifiers :public static final name: int BYTES;
modifiers :public static final name: java.lang.Class TYPE;
modifiers :private final name: double value;
modifiers :private static final name: long serialVersionUID;
}
源代码
package com.hostman.corejava;
import java.awt.print.Paper;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
import experiment1.class1;
/**
* print all feature of a class
* @author douer
*
*/
public class ReflectionTest {
public static void main(String[] args)
{
// TODO Auto-generated method stub
//read class name from command line args or user input
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();//name获取输入的包名
}
try
{
//print class name and superclass name
/*
* 可以使用Modifier类中的isPublic、isPrivate或isFinal判断方法或构造器
* 是否是public、private或final。我们需要做的全部工作就是调用Modifier类的相应方法,并对返回的整型数值进行分析,
* 另外,还可以利用Modifier.toString方法将修饰法打印出来。
*/
//name的包名
Class cl=Class.forName(name);
Class superc1=cl.getSuperclass();//父类
String modifiers=Modifier.toString(cl.getModifiers());
//输出控制范围如public,private
if(modifiers.length()>0)System.out.print(modifiers+" ");
//输出类的名字
System.out.print("class "+name);
//c1有超类---------------------------
if(superc1!=null&&superc1!=Object.class)
System.out.print(" extends"+superc1.getName());
System.out.print("\n{\n");
//调用接口打印
System.out.println("接口打印");
printConstructor(cl);
System.out.println();
//调用方法打印
System.out.println("方法打印");
printMethods(cl);
System.out.println();
//调用域
System.out.println("调用域");
printFields(cl);
System.out.println("}");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
System.exit(0);
}
/**
* print all fields of a class
* 如:私有变量
* @param cl
*/
private static void printFields(Class cl) {
// TODO Auto-generated method stub
Field[] fields=cl.getDeclaredFields();
for(Field f:fields)
{
Class type=f.getType();
String name=f.getName();
System.out.print(" ");
//打印域的控制范围
String modifiers=Modifier.toString(f.getModifiers());
if(modifiers.length()>0)System.out.print("modifiers :"+modifiers+" ");
System.out.println("name: "+type.getName()+" "+name+";");
}
}
private static void printMethods(Class cl)
{
// TODO Auto-generated method stub
Method[] methods=cl.getDeclaredMethods();
for(Method m:methods)
{
//获取方法返回的类型 如int
Class retType=m.getReturnType();
String name=m.getName();//方法的名字
System.out.print(" ");
//print modifier,return type method name
//
String modifiers=Modifier.toString(m.getModifiers());
if(modifiers.length()>0)System.out.print(modifiers+" ");//public static
System.out.print(" retype:"+retType.getName()+" "+name+"(/");//返回类型
//print 参数parameter
Class[] paramTypes=m.getParameterTypes();
//依次打印参数
for(int j=0;j<paramTypes.length;j++)
{
if(j>0)System.out.print(" , ");
System.out.print("param:"+paramTypes[j].getName());
//param:double , param:double
}
System.out.println(");");//打印完了要换行
}
}
//test::java.lang.Double
/**
* print cl all constructor
*
*/
public static void printConstructor(Class cl) {
//所有接口计入数组
Constructor[] constructors=cl.getDeclaredConstructors();
/*getModifiers的方法,它将返回一个整型数值,
*用不同的位开关描述public和static这样的修饰符使用状况。
*/
for(Constructor c:constructors)
{
String name=c.getName();
System.out.print(" ");
//modifiers表示控制域如: public
String modifiers=Modifier.toString(c.getModifiers());
if(modifiers.length()>0)System.out.print(modifiers+" ");
//打印包名
System.out.print(name+"(");
//print parameter types
Class[] paramTypes=c.getParameterTypes();//获取参数类型
for(int j=0;j<paramTypes.length;j++)
{
if(j>0)System.out.print(" , ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
}