-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection.java
More file actions
147 lines (123 loc) · 4.62 KB
/
Reflection.java
File metadata and controls
147 lines (123 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package Chapter5;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Scanner;
import javax.xml.crypto.Data;
public class Reflection{
public static void main(String[] args) {
/*
* ���ַ������Class�����
* */
String s=new String();
//1.�õ������Class���͵�ʵ�������������ȡ�������
Class sc=s.getClass();
String name=sc.getName();
//System.out.println(name);
try {
//2.��̬����class.forName()ͨ��������ô����ʵ��,�����������������߽ӿ���
Class cc= Class.forName("java.lang.String");
//newInstance����Ĭ���ι��캯�������û�У����쳣��
Object oc= cc.newInstance();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.���T����������Java���ͣ�**�������δ����һ���࣬����int**����T.class������ƥ��������
//Java8�л������ͣ�byte,int,short,long,float,double,cahr,boolean
Class c1=int.class;
Class c2=String.class;
Class c3=Data.class;
/*
* Ӧ�ã��û����������������������ȫ����Ϣ
* */
//java.lang.reflect������Ҫ�������ࣺField��Method��Constrauctor
System.out.println("please input a class name��");
Scanner in=new Scanner(System.in);
String className= in.next();
try {
Class c11=Class.forName(className);
Class c22=c11.getSuperclass();//�õ�cll�ĸ���
//getModifiers()�õ����η����������α�ʾ�ģ���ҪModifer.toString()ת���ַ���
String modifers=Modifier.toString(c11.getModifiers());
if(modifers.length()>0){
System.out.print(modifers+" class "+className);
}
//�Ƿ��и��ࣨ�ų�Object��
if(c22!=null&&c22!=Object.class){
System.out.print(" extends "+c22);
}
System.out.println("{\n");
//��ӡ���캯��
System.out.println("//���캯��");
printConstructers(c11);
//��ӡ����
System.out.println("//����");
printFields(c11);
//��ӡ����
System.out.println("//����");
printMethods(c11);
System.out.print("}");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void printConstructers(Class cname){
//����cname����������й��캯��������
//Constructor[] cons=cname.getConstructors();//��������
Constructor[] cons=cname.getDeclaredConstructors();//����������
for(Constructor con:cons){
//���η�
System.out.print(" "+Modifier.toString(con.getModifiers())+" "+con.getName()+"(");
//����
//Parameter[] pars=con.getParameters();//���ַ������ó�����IJ��������ˣ�ֻ��һ��Object
Class[] pars= con.getParameterTypes();
for(int i=0;i<pars.length;i++){
System.out.print(pars[i].getName()+" ");
}
System.out.print(");");
System.out.println();
}
System.out.println("\n\n");
}
public static void printFields(Class cName){
Field[] fields=cName.getFields();
for(Field f:fields){
System.out.print(" ");
//���η�
System.out.print(Modifier.toString(f.getModifiers()));
//����
System.out.println(" "+f.getName()+";");
System.out.println();
}
System.out.println("\n\n");
}
public static void printMethods(Class cName){
Method[] methods=cName.getMethods();
for(Method method:methods){
System.out.print(" ");
//���������
System.out.print(Modifier.toString(method.getModifiers())+" "+method.getName());
System.out.print("(");
//����
Class[] params=method.getParameterTypes();
for(int i=0;i<params.length;i++){
if(i!=params.length-1){
System.out.print(Modifier.toString(params[i].getModifiers()));
System.out.print(" "+params[i].getName()+",");
}
}
System.out.print(");");
System.out.println();
}
}
}