动态获取类或者对象的信息(属性和方法),以及动态操作对象的属性和方法的类,动态分析和使用类的信息的类
注:动态是指在java运行状态
反射技术的核心技术是Class对象,每个类在定义以后都有各自的Class对象
1. `package main.java.com.shixinke.java.demo.reflect;` 3. `public class User {` 4. `public String platformNo = "WEB";` 5. `private Long userId;` 6. `private String nickname;` 7. `private String password;` 8. `private Character gender;` 9. `private String email;` 11. `public Long getUserId() {` 12. `return userId;` 13. `}` 15. `public void setUserId(Long userId) {` 16. `this.userId = userId;` 17. `}` 19. `public String getNickname() {` 20. `return nickname;` 21. `}` 23. `public void setNickname(String nickname) {` 24. `this.nickname = nickname;` 25. `}` 27. `public String getPassword() {` 28. `return password;` 29. `}` 31. `public void setPassword(String password) {` 32. `this.password = password;` 33. `}` 35. `public Character getGender() {` 36. `return gender;` 37. `}` 39. `public void setGender(Character gender) {` 40. `this.gender = gender;` 41. `}` 43. `public String getEmail() {` 44. `return email;` 45. `}` 47. `public void setEmail(String email) {` 48. `this.email = email;` 49. `}` 51. `private void setGender(int gender) {` 52. `if (gender == 1) {` 53. `this.gender = 'M';` 54. `} else {` 55. `this.gender = 'F';` 56. `}` 57. `}` 58. `}`
1. `/**` 2. `* 1.通过Class.forName的形式来获取Class对象` 3. `*/` 4. `Class cls = Class.forName("main.java.com.shixinke.java.demo.reflect.User");`
1. `/**` 2. `* 2.通过类名.class来获取Class对象` 3. `*/` 4. `Class clz = User.class;`
1. `/**` 2. `* 3.通过类对象.getClass()方法获取Class对象` 3. `*/` 4. `User user = new User();` 5. `Class clazz = user.getClass();`
1. `System.out.println(cls.getName()); //main.java.com.shixinke.java.demo.reflect.User` 2. `System.out.println(cls.getSuperclass()); //class java.lang.Object` 3. `System.out.println(cls.getCanonicalName()); //main.java.com.shixinke.java.demo.reflect.User`
1. `Field[] fields = cls.getFields();` 2. `for (Field field : fields) {` 3. `System.out.println(field.getName()); //platformNo : 只能获取非私有的属性` 4. `}` 6. `Field[] allFields = cls.getDeclaredFields();` 7. `for (Field field : allFields) {` 8. `System.out.println(field.getName()); //[platformNo userId nickname password gender email]` 9. `}`
1. `/**` 2. `* 1.获取所有非私有方法` 3. `*/` 4. `Method[] methods = cls.getMethods();` 5. `for (Method m : methods) {` 6. `System.out.println(m.getName());` 7. `}` 9. `/**` 10. `* 2.获取所有方法` 11. `*/` 12. `Method[] allMethods = cls.getDeclaredMethods();` 13. `for (Method m : allMethods) {` 14. `System.out.println(m.getName());` 15. `}` 17. `/**` 18. `* 3.获取某个指定方法名的方法对象(必须是非私有方法)` 19. `* 第1个参数为方法名,第2个参数及以后的表示方法中的参数的类型` 20. `*/` 21. `Method setGenderMethod = cls.getMethod("setGender", Character.class);` 22. `System.out.println(setGenderMethod.getParameterCount());` 24. `/**` 25. `* 4.获取某个指定方法名的方法对象(既可以是公有也可以是私有)` 26. `*/` 27. `Method setGenderAllMethod = cls.getDeclaredMethod("setGender", int.class);` 28. `System.out.println(setGenderAllMethod.getParameterCount());`
1. `Method m = cls.getMethod("setGender", Character.class);` 2. `/**` 3. `* 1.获取参数个数` 4. `*/` 5. `System.out.println(m.getParameterCount());` 7. `/**` 8. `* 2.获取参数对象数组` 9. `*/` 10. `for (Parameter p : m.getParameters()) {` 11. `System.out.println(p.getName()); //获取参数名称` 12. `System.out.println(p.getType()); //获取参数的类型` 13. `System.out.println(p.getAnnotations()); //获取参数的注解` 14. `}` 16. `/**` 17. `* 获取返回值类型` 18. `*/` 19. `System.out.println(m.getReturnType());`
1. `/**` 2. `* 1.获取实现的接口数组对象` 3. `*/` 4. `for (Class c : cls.getInterfaces()) {` 5. `System.out.println(c.getName());` 6. `}` 8. `/**` 9. `* 2.获取使用的注解数组对象` 10. `*/` 11. `for (Annotation a : cls.getAnnotations()) {` 12. `System.out.println(a.getClass().getName());` 13. `}`
1. `User user = (User)cls.newInstance();` 2. `user.setEmail("[email protected]");` 3. `System.out.println(user.getEmail()); //[email protected]`
1. `Constructor constructor = cls.getConstructor();` 2. `User user = (User) constructor.newInstance();` 3. `user.setEmail("[email protected]");` 4. `System.out.println(user.getEmail()); //[email protected]`
1. `User user = (User)cls.newInstance();` 2. `user.setEmail("[email protected]");` 3. `System.out.println(user.getEmail()); //[email protected]`