java反射原理
动态代理:
【
如何为java对象创建一个代理对象?
java.lang.reflect.Proxy类:提供了为对象产生代理对象的方法
1 2 3 4
| public static Object newProxyInstance(ClasLoader loader, Class<?>[] interfaces, InvocationHandler h) 参数一:用于指定用哪个类加载器,去加载生成的代理类 参数二:指定接口,这些接口用于指定生成的代理长什么,也就是有哪些方法 参数三:用来指定生成的代理对象要干什么事情
|
反射就是能够获取到一个类的全部信息
获取class对象:
1 2 3
| Class.forName("全类名"); 类名.class 对象.getclass();
|
三个阶段:
第一个阶段:
使用的是Class.forName(“全类名”);获取字节码文件,这是未写入内存
用的是类名.class
用的是对象.getclass();
获取class对象后才能获取构造的方法
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
|
Class clazz= Class.forName("MyReflect2.Student");
Constructor con46=clazz.getDeclaredConstructor(String.class,int.class); int modifiers = con46.getModifiers(); System.out.println(modifiers); Parameter[] parameters= con46.getParameters(); for (Parameter p:parameters) { System.out.println(p); }
|
利用反射获取成员变量
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
| package MyReflect3;
import java.lang.reflect.Field;
public class MyReflectDemo { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class clazz=Class.forName("MyReflect3.Student"); Field[] fields=clazz.getDeclaredFields();
for (Field f:fields){ System.out.println(f); }
Field gender = clazz.getDeclaredField("name"); System.out.println(gender);
int zhuangtai =gender.getModifiers(); System.out.println(zhuangtai);
String n = gender.getName(); System.out.println(n); Class type = gender.getType(); System.out.println(type);
Student s = new Student("zhangsan",23,"nan"); gender.setAccessible(true); Object value = gender.get(s); System.out.println(value);
gender.set(s,"lisi"); System.out.println(s); } }
|
利用反射获取成员方法:
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
| package MyReflect4;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class MyReflectDemo { public static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class clazz = Class.forName("MyReflect4.Student");
Method[] methods1=clazz.getDeclaredMethods(); for (Method m:methods1){ System.out.println(m); }
Method m=clazz.getDeclaredMethod("eat", String.class); m.setAccessible(true); System.out.println(m);
int modifiers= m.getModifiers(); System.out.println(modifiers);
String name= m.getName(); System.out.println(name);
Class[] parameterTypes= m.getParameterTypes(); for (Class c:parameterTypes){ System.out.println(c); } Class returnType= m.getReturnType(); System.out.println(returnType); Class[] exceptionTypes= m.getExceptionTypes(); for (Class e:exceptionTypes){ System.out.println(e); } Student s=new Student(); Object o=m.invoke(s,"apple"); System.out.println(o); } }
|
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
| package MyReflect4;
public class Student { private String name; private int age;
public Student() { }
public Student(String name, int age) { this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } public void sleep(){ System.out.println("睡觉"); } private void eat(String something){ System.out.println("再吃"+something); } private void eat(String something,int age){ System.out.println("再吃"+something); }
public String toString() { return "Student{name = " + name + ", age = " + age + "}"; } }
|
Author:
odiws
Permalink:
http://odiws.github.io/2024/11/14/java%E5%8F%8D%E5%B0%84%E5%8E%9F%E7%90%86/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?