1. The concept of Java reflection
Reflection meaning: You can get a running Java object.
2, the function of Java reflection
1) You can determine the class to which the run-time object belongs
2) You can determine the member variables and methods that run-time objects have
3) A method that can even be called to private by reflection
4) Generate dynamic agent
3. Classes that implement Java reflection
1) Class: It represents classes and interfaces in a running Java application
2) Field: Provides information about the properties of a class or interface, as well as its dynamic access rights
3) Constructor: Provides information about a single construction method of a class and access to it
4) Method: Provides information about one of the methods in a class or interface
Note: The class class is the most important feature class in Java reflection, and all information that gets the object (including: Method/property/constructor/access) needs it to implement
4. Steps to write the Java Reflector program:
1) You must first obtain the class object
For example:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
<br> Class c1 = Test.
class
;
Class c2 = Class.forName(“com.reflection.Test”);
Class c3 =
new
Test().getClass();
2
)然后分别调用Class对象中的方法来获取一个类的属性/方法/构造方法的结构
注意:如果要能够正常的获取类中方法/属性/构造方法应该重点掌握如下的反射类
Field
Constructor
Method
示例:此程序例子告诉大家如何操作Class/Field/Constructor/Method等与Java反射相关的类
package
com.reflection;
import
java.lang.reflect.Constructor;
import
java.lang.reflect.Field;
import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
import
java.lang.reflect.Modifier;
public
class
TestReflection {
private String username;
private
String password;
private
int
[] age;
public
void
setUserName(String username) {
this
.username = username;
}
private
void
setPassWord(String password) {
this
.password = password;
}
public
static
void
test01()
throws
ClassNotFoundException {
Class c1 = TestReflection.
class
;
Class c2 = Class.forName(
"com.reflection.TestReflection"
);
//获取指定的包名
String package01 = c1.getPackage().getName();
String package02 = c2.getPackage().getName();
System.out.println(
"package01 = "
+ package01);
System.out.println(
"package02 = "
+ package02);
//获取类的修饰符
int
mod = c1.getModifiers();
String modifier = Modifier.toString(mod);
System.out.println(
"modifier = "
+ modifier);
//获取指定类的完全限定名
String className = c1.getName();
System.out.println(
"className = "
+ className);
//获取指定类的父类
Class superClazz = c1.getSuperclass();
String superClazzName = superClazz.getName();
System.out.println(
"superClazzName = "
+ superClazzName);
//获取实现的接口
Class[] interfaces = c1.getInterfaces();
for
(Class t : interfaces) {
System.out.println(
"interfacesName = "
+ t.getName());
}
//获取指定类的成员变量
Field[] fields = c1.getDeclaredFields();
for
(Field field : fields) {
modifier = Modifier.toString(field.getModifiers());
//获取每个
字段的访问修饰符
Class type = field.getType();
//获取字段的数据类型所对应的
Class对象
String name = field.getName();
//获取字段名
if
(type.isArray()) {
//如果是数组类型则需要特别处理
String arrType = type.getComponentType().getName() +
"[]"
;
System.out.println(
""
+ modifier +
" " + arrType +
" "
+ name +
";"
);
}
else
{
System.out.println(
""
+ modifier +
" "
+ type +
" "
+
name +
";"
);
}
}
//获取类的构造方法
Constructor[] constructors = c1.getDeclaredConstructors();
for
(Constructor constructor : constructors) {
String name = constructor.getName();
//构造方法名
modifier = Modifier.toString(constructor.getModifiers());
//获取访问修饰符
System.out.println(
""
+ modifier +
" "
+ name +
"("
);
Class[] paramTypes = constructor.getParameterTypes();
//获取构造方法中的参数
for
(
int
i =
0
; i < paramTypes.length; i++) {
if
(i >
0
) {
System.out.print(
","
);
}
if
(paramTypes[i].isArray()) {
System.out.println(paramTypes
[i].getComponentType().getName()+
"[]"
);
}
else
{
System.out.print(paramTypes[i].getName());
}
}
System.out.println(
");"
);
}
//获取成员方法
Method[] methods = c1.getDeclaredMethods();
for
(Method method: methods) {
modifier = Modifier.toString(method.getModifiers());
Class returnType = method.getReturnType();
//获取方法的返回类型
if
(returnType.isArray()) {
String arrType = returnType.getComponentType
().getName()+
"[]"
;
System.out.print(
""
+modifier+
" "
+ arrType +
" "
+
method.getName() +
"("
);
}
else
{
System.out.print(
""
+ modifier +
" "
+
returnType.getName() +
" "
+ method.getName() +
"("
);
}
Class[] paramTypes = method.getParameterTypes();
for
(
int
i =
0
; i < paramTypes.length; i++) {
if
(i >
0
) {
System.out.print(
","
);
}
if
(paramTypes[i].isArray()) {
System.out.println(paramTypes
[i].getComponentType().getName()+
"[]"
);
}
else {
System.out.print(paramTypes[i].getName());
}
}
System.out.println(
");"
);
}
}
public
static
void
test02()
throws
InstantiationException,
IllegalAccessException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
//反射调用方法,可以通过Method类的invoke方法实现动态方法的调用
//public Object invoke(Object obj, Object... args)
//第一个参数代表对象
//第二个参数代表执行方法上的参数
//若反射要调用类的某个私有方法,可以在这个私有方法对应的Mehtod对象上先
调用setAccessible(
true
)
Class c1 = TestReflection.
class
;
TestReflection t1 = (TestReflection) c1.newInstance();
//利用反射来创
建类的对象
System.out.println(
"username == "
+ t1.username);
System.out.println(
"password == "
+ t1.password);
Method method = c1.getDeclaredMethod(
"setUserName"
, String.
class
);
method.invoke(t1,
"Java反射的学习"
);
System.out.println(
"username == "
+ t1.username);
method = c1.getDeclaredMethod(
"setPassWord"
, String.
class
);
method.setAccessible(
true
);
method.invoke(t1,
"反射执行某个Private修饰的方法"
);
System.out.println(
"password == "
+ t1.password);
}
public
static
void
main(String[] args)
throws
ClassNotFoundException,
SecurityException, IllegalArgumentException, InstantiationException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// test01();
test02();
}
}
|
Very good examples of Java reflection