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 = newTest().getClass();2)然后分别调用Class对象中的方法来获取一个类的属性/方法/构造方法的结构注意:如果要能够正常的获取类中方法/属性/构造方法应该重点掌握如下的反射类FieldConstructorMethod示例:此程序例子告诉大家如何操作Class/Field/Constructor/Method等与Java反射相关的类packagecom.reflection;importjava.lang.reflect.Constructor;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.lang.reflect.Modifier;publicclassTestReflection {private String username;privateString password;privateint[] age;publicvoidsetUserName(String username) {this.username = username;}privatevoidsetPassWord(String password) {this.password = password;}publicstaticvoidtest01() throwsClassNotFoundException {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);//获取类的修饰符intmod = 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(inti = 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(inti = 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(");");}}publicstaticvoidtest02() throwsInstantiationException,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);}publicstaticvoidmain(String[] args) throwsClassNotFoundException,SecurityException, IllegalArgumentException, InstantiationException,IllegalAccessException, NoSuchMethodException, InvocationTargetException {// test01();test02();}} |
Very good examples of Java reflection