1.源码
package test5;
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;
/**
* 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(");");
}
}
}
2.输入
public class java.util.Date
3.输出
{
接口打印
public java.util.Date(java.lang.String);
public java.util.Date(int , int , int , int , int , int);
public java.util.Date(int , int , int , int , int);
public java.util.Date();
public java.util.Date(long);
public java.util.Date(int , int , int);
方法打印
public retype:boolean equals(/param:java.lang.Object);
public retype:java.lang.String toString(/);
public retype:int hashCode(/);
public retype:java.lang.Object clone(/);
public volatile retype:int compareTo(/param:java.lang.Object);
public retype:int compareTo(/param:java.util.Date);
public static retype:java.util.Date from(/param:java.time.Instant);
private retype:void readObject(/param:java.io.ObjectInputStream);
private retype:void writeObject(/param:java.io.ObjectOutputStream);
private final retype:sun.util.calendar.BaseCalendar
D
a
t
e
n
o
r
m
a
l
i
z
e
(
/
p
a
r
a
m
:
s
u
n
.
u
t
i
l
.
c
a
l
e
n
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
i
c
f
i
n
a
l
r
e
t
y
p
e
:
l
o
n
g
g
e
t
M
i
l
l
i
s
O
f
(
/
p
a
r
a
m
:
j
a
v
a
.
u
t
i
l
.
D
a
t
e
)
;
p
u
b
l
i
c
r
e
t
y
p
e
:
v
o
i
d
s
e
t
D
a
t
e
(
/
p
a
r
a
m
:
i
n
t
)
;
p
r
i
v
a
t
e
f
i
n
a
l
r
e
t
y
p
e
:
s
u
n
.
u
t
i
l
.
c
a
l
e
n
d
a
r
.
B
a
s
e
C
a
l
e
n
d
a
r
Date normalize(/); public retype:boolean before(/param:java.util.Date); public retype:boolean after(/param:java.util.Date); public static retype:long parse(/param:java.lang.String); public retype:long getTime(/); public retype:int getYear(/); public retype:int getSeconds(/); public retype:java.time.Instant toInstant(/); public static retype:long UTC(/param:int , param:int , param:int , param:int , param:int , param:int); public retype:void setTime(/param:long); public retype:int getMonth(/); static final retype:long getMillisOf(/param:java.util.Date); public retype:void setDate(/param:int); private final retype:sun.util.calendar.BaseCalendar
Datenormalize(/);publicretype:booleanbefore(/param:java.util.Date);publicretype:booleanafter(/param:java.util.Date);publicstaticretype:longparse(/param:java.lang.String);publicretype:longgetTime(/);publicretype:intgetYear(/);publicretype:intgetSeconds(/);publicretype:java.time.InstanttoInstant(/);publicstaticretype:longUTC(/param:int,param:int,param:int,param:int,param:int,param:int);publicretype:voidsetTime(/param:long);publicretype:intgetMonth(/);staticfinalretype:longgetMillisOf(/param:java.util.Date);publicretype:voidsetDate(/param:int);privatefinalretype:sun.util.calendar.BaseCalendarDate getCalendarDate(/);
public retype:void setHours(/param:int);
public retype:int getHours(/);
public retype:int getMinutes(/);
public retype:void setMinutes(/param:int);
public retype:void setSeconds(/param:int);
public retype:void setMonth(/param:int);
public retype:void setYear(/param:int);
private static final retype:sun.util.calendar.BaseCalendar getCalendarSystem(/param:int);
private static final retype:sun.util.calendar.BaseCalendar getCalendarSystem(/param:long);
private static final retype:sun.util.calendar.BaseCalendar getCalendarSystem(/param:sun.util.calendar.BaseCalendar$Date);
private final retype:long getTimeImpl(/);
private static final retype:java.lang.StringBuilder convertToAbbr(/param:java.lang.StringBuilder , param:java.lang.String);
private static final synchronized retype:sun.util.calendar.BaseCalendar getJulianCalendar(/);
public retype:int getDate(/);
public retype:int getDay(/);
public retype:java.lang.String toLocaleString(/);
public retype:java.lang.String toGMTString(/);
public retype:int getTimezoneOffset(/);
调用域
modifiers :private static final name: sun.util.calendar.BaseCalendar gcal;
modifiers :private static name: sun.util.calendar.BaseCalendar jcal;
modifiers :private transient name: long fastTime;
modifiers :private transient name: sun.util.calendar.BaseCalendar$Date cdate;
modifiers :private static name: int defaultCenturyStart;
modifiers :private static final name: long serialVersionUID;
modifiers :private static final name: [Ljava.lang.String; wtb;
modifiers :private static final name: [I ttb;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)