题目01
/* FieldTest获取当前运行时类的属性结构 */
package com.jerry.java; import org.junit.Test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; /** * @author jerry_jy * @create 2022-10-14 9:36 */ public class FieldTest { @Test public void test1() { Class clazz = Person.class; //获取属性结构 //getFields():获取当前运行时类及其父类中声明为public访问权限的属性 Field[] fields = clazz.getFields(); for (Field field : fields) { System.out.println(field);//public int com.jerry.java.Person.age } System.out.println("****************************"); //getDeclaredFields():获取当前运行时类中声明的所有属性。(不包含父类中声明的属性) Field[] fields1 = clazz.getDeclaredFields(); for (Field field : fields1) { System.out.println(field);//private java.lang.String com.jerry.java.Person.name public int com.jerry.java.Person.age } System.out.println("****************************"); } //权限修饰符 数据类型 变量名 @Test public void test2() { Class<Person> clazz = Person.class; Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { //1.权限修饰符 int modifiers = field.getModifiers(); System.out.println(Modifier.toString(modifiers) + "\t"); //2.数据类型 Class<?> type = field.getType(); System.out.println(type.getName() + "\t"); //3.变量名 String fieldName = field.getName(); System.out.println(fieldName + "\t"); } } }
题目02
/* MethodTest获取当前运行时类的属性结构 */
package com.jerry.java; import org.junit.Test; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** * @author jerry_jy * @create 2022-10-14 9:47 */ public class MethodTest { @Test public void test1(){ Class clazz = Person.class; //getMethods():获取当前运行时类及其所有父类中声明为public权限的方法 Method[] methods = clazz.getMethods(); for (Method method : methods) { System.out.println(method); } System.out.println(); //getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法) Method[] declaredMethods = clazz.getDeclaredMethods(); for(Method m : declaredMethods){ System.out.println(m); } } /* @Xxxx 权限修饰符 返回值类型 方法名(参数类型1 形参名1,...) throws XxxException{} */ @Test public void test2(){ Class clazz = Person.class; Method[] declaredMethods = clazz.getDeclaredMethods(); for(Method m : declaredMethods){ //1.获取方法声明的注解 Annotation[] annos = m.getAnnotations(); for(Annotation a : annos){ System.out.println(a); } //2.权限修饰符 System.out.print(Modifier.toString(m.getModifiers()) + "\t"); //3.返回值类型 System.out.print(m.getReturnType().getName() + "\t"); //4.方法名 System.out.print(m.getName()); System.out.print("("); //5.形参列表 Class[] parameterTypes = m.getParameterTypes(); if(!(parameterTypes == null && parameterTypes.length == 0)){ for(int i = 0;i < parameterTypes.length;i++){ if(i == parameterTypes.length - 1){ System.out.print(parameterTypes[i].getName() + " args_" + i); break; } System.out.print(parameterTypes[i].getName() + " args_" + i + ","); } } System.out.print(")"); //6.抛出的异常 Class[] exceptionTypes = m.getExceptionTypes(); if(exceptionTypes.length > 0){ System.out.print("throws "); for(int i = 0;i < exceptionTypes.length;i++){ if(i == exceptionTypes.length - 1){ System.out.print(exceptionTypes[i].getName()); break; } System.out.print(exceptionTypes[i].getName() + ","); } } System.out.println(); } } }
题目03
ClassLoaderTest
package com.jerry.java; import org.junit.Test; import java.io.InputStream; import java.util.Properties; /** * @author jerry_jy * @create 2022-10-13 21:56 */ public class ClassLoaderTest { @Test public void test1() { //对于自定义类,使用系统类加载器进行加载 ClassLoader classLoader = ClassLoaderTest.class.getClassLoader(); System.out.println(classLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2 //调用系统类加载器的getParent():获取扩展类加载器 ClassLoader classLoader1 = classLoader.getParent(); System.out.println(classLoader1);//sun.misc.Launcher$ExtClassLoader@28a418fc //调用扩展类加载器的getParent():无法获取引导类加载器 //引导类加载器主要负责加载java的核心类库,无法加载自定义类的。 ClassLoader classLoader2 = classLoader1.getParent(); System.out.println(classLoader2);//null ClassLoader classLoader3 = String.class.getClassLoader(); System.out.println(classLoader3);//null } /* Properties:用来读取配置文件。 */ @Test public void test2() throws Exception { Properties pros = new Properties(); //此时的文件默认在当前的module下。 //读取配置文件的方式一: // FileInputStream fis = new FileInputStream("jdbc.properties"); // pros.load(fis); //读取配置文件的方式二:使用ClassLoader //配置文件默认识别为:当前module的src下 ClassLoader classLoader = ClassLoaderTest.class.getClassLoader(); InputStream is = classLoader.getResourceAsStream("jdbc.properties"); pros.load(is); String name = pros.getProperty("name"); String password = pros.getProperty("password"); System.out.println("name = " + name + ",password = " + password); } }
题目04
/* 通过反射创建对应的运行时类的对象 */
package com.jerry.java; import org.junit.Test; import java.util.Random; /** * @author jerry_jy * @create 2022-10-13 22:23 */ public class NewInstanceTest { @Test public void test1() throws IllegalAccessException, InstantiationException { Class<Person> clazz = Person.class; /* newInstance():调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。 要想此方法正常的创建运行时类的对象,要求: 1.运行时类必须提供空参的构造器 2.空参的构造器的访问权限得够。通常,设置为public。 在javabean中要求提供一个public的空参构造器。原因: 1.便于通过反射,创建运行时类的对象 2.便于子类继承此运行时类时,默认调用super()时,保证父类有此构造器 */ Person person = clazz.newInstance(); System.out.println(person); } //体会反射的动态性 @Test public void test2() throws Exception { for (int i = 0; i < 100; i++) { int num = new Random().nextInt(3); String classPath = ""; switch (num){ case 0: classPath = "java.util.Date"; break; case 1: classPath = "java.lang.Object"; break; case 2: classPath = "com.jerry.java.Person"; break; } Object object = getInstance(classPath); System.out.println(object); } } public Object getInstance(String classPath) throws Exception { Class<?> clazz = Class.forName(classPath); return clazz.newInstance(); } }
题目05
ReflectionTest
package com.jerry.java; import org.junit.Test; import java.lang.annotation.ElementType; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @author jerry_jy * @create 2022-10-13 21:26 */ public class ReflectionTest { //反射之前,对于Person的操作 @Test public void test1() { //1.创建Person类的对象 Person p1 = new Person("Tom", 12); //2.通过对象,调用其内部的属性、方法 p1.age = 10; System.out.println(p1.toString()); p1.show(); //在Person类外部,不可以通过Person类的对象调用其内部私有结构。 //比如:name、showNation()以及私有的构造器 } //反射之后,对于Person的操作 @Test public void test2() throws Exception { Class<Person> clazz = Person.class; //1.通过反射,创建Person类的对象 Constructor<Person> cons = clazz.getConstructor(String.class, int.class); Person person = cons.newInstance("Tom", 12); System.out.println(person.toString());//Person{name='Tom', age=12} //2.通过反射,调用对象指定的属性、方法 //调用属性 Field age = clazz.getDeclaredField("age"); age.set(person, 10); System.out.println(person.toString());//Person{name='Tom', age=10} //调用方法 Method show = clazz.getDeclaredMethod("show"); show.invoke(person); System.out.println("*******************************"); //通过反射,可以调用Person类的私有结构的。比如:私有的构造器、方法、属性 //调用私有的构造器 Constructor<Person> cons1 = clazz.getDeclaredConstructor(String.class); cons1.setAccessible(true); Person person1 = cons1.newInstance("Jerry"); System.out.println(person1); Field name = clazz.getDeclaredField("name"); name.setAccessible(true); name.set(person1, "Jim"); System.out.println(person1); Method showNation = clazz.getDeclaredMethod("showNation", String.class); showNation.setAccessible(true); String nation = (String) showNation.invoke(person1, "CHINA"); System.out.println(nation); } //获取Class的实例的方式(前三种方式需要掌握) @Test public void test3() throws ClassNotFoundException { //方式一:调用运行时类的属性:.class Class<Person> clazz1 = Person.class; System.out.println(clazz1);//class com.jerry.java.Person //方式二:通过运行时类的对象,调用getClass() Person person = new Person(); Class clazz2 = person.getClass(); System.out.println(clazz2);//Person() class com.jerry.java.Person //方式三:调用Class的静态方法:forName(String classPath) Class<?> clazz3 = Class.forName("com.jerry.java.Person"); System.out.println(clazz3);//class com.jerry.java.Person System.out.println(clazz1 == clazz2);//true System.out.println(clazz1 == clazz3);//true //方式四:使用类的加载器:ClassLoader (了解) ClassLoader classLoader = ReflectionTest.class.getClassLoader(); Class<?> clazz4 = classLoader.loadClass("com.jerry.java.Person"); System.out.println(clazz4);//class com.jerry.java.Person System.out.println(clazz1 == clazz4);//true } //万事万物皆对象?对象.xxx,File,URL,反射,前端、数据库操作 //Class实例可以是哪些结构的说明: @Test public void test4(){ Class c1 = Object.class; Class c2 = Comparable.class; Class c3 = String[].class; Class c4 = int[][].class; Class c5 = ElementType.class; Class c6 = Override.class; Class c7 = int.class; Class c8 = void.class; Class c9 = Class.class; int[] a = new int[10]; int[] b = new int[100]; Class c10 = a.getClass(); Class c11 = b.getClass(); // 只要数组的元素类型与维度一样,就是同一个Class System.out.println(c10 == c11); } }
题目06
其他方法测试
package com.jerry.java; import org.junit.Test; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * @author jerry_jy * @create 2022-10-14 9:54 */ public class OtherTest { /* 获取构造器结构 */ @Test public void test1(){ Class clazz = Person.class; //getConstructors():获取当前运行时类中声明为public的构造器 Constructor[] constructors = clazz.getConstructors(); for(Constructor c : constructors){ System.out.println(c); } System.out.println(); //getDeclaredConstructors():获取当前运行时类中声明的所有的构造器 Constructor[] declaredConstructors = clazz.getDeclaredConstructors(); for(Constructor c : declaredConstructors){ System.out.println(c); } } /* 获取运行时类的父类 */ @Test public void test2(){ Class clazz = Person.class; Class superclass = clazz.getSuperclass(); System.out.println(superclass); } /* 获取运行时类的带泛型的父类 */ @Test public void test3(){ Class clazz = Person.class; Type genericSuperclass = clazz.getGenericSuperclass(); System.out.println(genericSuperclass); } /* 获取运行时类的带泛型的父类的泛型 代码:逻辑性代码 vs 功能性代码 */ @Test public void test4(){ Class clazz = Person.class; Type genericSuperclass = clazz.getGenericSuperclass(); ParameterizedType paramType = (ParameterizedType) genericSuperclass; //获取泛型类型 Type[] actualTypeArguments = paramType.getActualTypeArguments(); // System.out.println(actualTypeArguments[0].getTypeName()); System.out.println(((Class)actualTypeArguments[0]).getName()); } /* 获取运行时类实现的接口 */ @Test public void test5(){ Class clazz = Person.class; Class[] interfaces = clazz.getInterfaces(); for(Class c : interfaces){ System.out.println(c); } System.out.println(); //获取运行时类的父类实现的接口 Class[] interfaces1 = clazz.getSuperclass().getInterfaces(); for(Class c : interfaces1){ System.out.println(c); } } /* 获取运行时类所在的包 */ @Test public void test6(){ Class clazz = Person.class; Package pack = clazz.getPackage(); System.out.println(pack); } /* 获取运行时类声明的注解 */ @Test public void test7(){ Class clazz = Person.class; Annotation[] annotations = clazz.getAnnotations(); for(Annotation annos : annotations){ System.out.println(annos); } } }
题目07
StaticProxyTest
package com.jerry.java; /** * @author jerry_jy * @create 2022-10-14 10:32 */ public class StaticProxyTest { public static void main(String[] args) { //创建被代理类的对象 NikeClothFactory nike = new NikeClothFactory(); //创建代理类的对象 ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike); proxyClothFactory.produceCloth(); } } interface ClothFactory{ void produceCloth(); } //代理类 class ProxyClothFactory implements ClothFactory{ private ClothFactory factory;//用被代理类对象进行实例化 public ProxyClothFactory(ClothFactory factory) { this.factory = factory; } @Override public void produceCloth() { System.out.println("代理工厂做一些准备工作"); factory.produceCloth(); System.out.println("代理工厂做一些后续的收尾工作"); } } //被代理类 class NikeClothFactory implements ClothFactory{ @Override public void produceCloth() { System.out.println("Nike工厂生产一批运动服"); } }
题目08
/* 动态代理 */
package com.jerry.java; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author jerry_jy * @create 2022-10-14 10:37 */ public class ProxyTest { public static void main(String[] args) { SuperMan superMan = new SuperMan(); //proxyInstance:代理类的对象 Human proxyInstance = (Human) ProxyFactory.getProxyInstance(superMan); //当通过代理类对象调用方法时,会自动的调用被代理类中同名的方法 String belief = proxyInstance.getBelief(); System.out.println(belief); proxyInstance.eat("四川麻辣烫"); System.out.println("*****************************"); NikeClothFactory nikeClothFactory = new NikeClothFactory(); ClothFactory proxyClothFactory = (ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory); proxyClothFactory.produceCloth(); } } interface Human{ String getBelief(); void eat(String food); } //被代理类 class SuperMan implements Human{ @Override public String getBelief() { return "I believe I can fly!"; } @Override public void eat(String food) { System.out.println("我喜欢吃" + food); } } class HumanUtil{ public void method1(){ System.out.println("====================通用方法一===================="); } public void method2(){ System.out.println("====================通用方法二===================="); } } /* 要想实现动态代理,需要解决的问题? 问题一:如何根据加载到内存中的被代理类,动态的创建一个代理类及其对象。 问题二:当通过代理类的对象调用方法a时,如何动态的去调用被代理类中的同名方法a。 */ class ProxyFactory{ //调用此方法,返回一个代理类的对象。解决问题一 public static Object getProxyInstance(Object obj) {//obj:被代理类的对象 MyInvocationHandler handler = new MyInvocationHandler(); handler.bind(obj); return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler); } } class MyInvocationHandler implements InvocationHandler{ private Object obj;//需要使用被代理类的对象进行赋值 public void bind(Object obj){ this.obj = obj; } //当我们通过代理类的对象,调用方法a时,就会自动的调用如下的方法:invoke() //将被代理类要执行的方法a的功能就声明在invoke()中 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { HumanUtil util = new HumanUtil(); util.method1(); //method:即为代理类对象调用的方法,此方法也就作为了被代理类对象要调用的方法 //obj:被代理类的对象 Object returnValue = method.invoke(obj, args); util.method2(); //上述方法的返回值就作为当前类中的invoke()的返回值。 return returnValue; } }
题目09
- 描述一下JVM加载class文件的原理机制?
答:JVM中类的装载是由ClassLoader和它的子类来实现的,Java ClassLoader 是一个重要的Java运行时系统组件。它负责在运行时查找和装入类文件的类。
题目10
/* 2. 利用反射和重载完成以下功能 1)创建Student类,类中有属性name和age并封装属性 2)重载Student的构造函数,一个是无参构造并,另一个是带两个参数的有参构造,要求在构造函数打印提示信息 3)创建带main函数的NewInstanceTest类,利用Class类得到Student对象 4)通过上述获取的Class对象分别调用Student有参函数和无参函数 */
package com.jerry.exer; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; /** * @author jerry_jy * @create 2022-10-14 11:00 */ public class Exer1 { public static void main(String[] args) throws Exception { Class<Student> clazz = Student.class; Constructor<Student> cons1 = clazz.getDeclaredConstructor(); cons1.setAccessible(true); Student stu1 = cons1.newInstance(); System.out.println(stu1); Constructor<Student> cons2 = clazz.getDeclaredConstructor(String.class, int.class); cons2.setAccessible(true); Student stu2 = cons2.newInstance("Tom", 22); System.out.println(stu2); } } class Student{ private String name; private int age; public Student() { System.out.println("空参构造器"); } public Student(String name, int age) { this.name = name; this.age = age; System.out.println("带参构造器"); } }
题目11
/* 3. 利用反射的知识完成下面的操作 1)创建Mytxt类,创建myCreate()方法完成创建文件D:\myhello.txt文件的功能。创建带main方法的NewInstanceTest类,通过Class类获取Mytxt对象,调用myCreat() */
package com.jerry.exer; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; /** * @author jerry_jy * @create 2022-10-14 11:08 */ public class Exer2 { public static void main(String[] args) throws Exception { Class<Mytxt> clazz = Mytxt.class; Mytxt mytxt = clazz.newInstance(); Method myCreate = clazz.getDeclaredMethod("myCreate"); myCreate.setAccessible(true); Object o = myCreate.invoke(mytxt); System.out.println(o); } } class Mytxt{ public void myCreate(){ File file = new File("D:\\myhello.txt"); if (!file.exists()){ try { file.createNewFile(); System.out.println("创建成功!"); } catch (IOException e) { e.printStackTrace(); } } } }
题目12
/* 5. 利用通过反射修改私有成员变量 1. 定义PrivateTest类,有私有name属性,并且属性值为hellokitty,只提供name的getName的公有方法 2. 创建带有main方法ReflectTest的类,利用Class类得到私有的name属性 3. 修改私有的name属性值,并调用getName()的方法打印name属性值 */
package com.jerry.exer; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @author jerry_jy * @create 2022-10-14 11:17 */ public class Exer4 { public static void main(String[] args) throws Exception { Class<PrivateTest> clazz = PrivateTest.class; PrivateTest privateTest = clazz.newInstance(); Field name = clazz.getDeclaredField("name"); name.setAccessible(true); name.set(privateTest, "Jerry"); // Object jerry = name.get(privateTest); // System.out.println(jerry); Method getName = clazz.getDeclaredMethod("getName", String.class); getName.setAccessible(true); Object o = getName.invoke(privateTest); System.out.println(o); } } class PrivateTest{ private String name = "HelloKitty"; public String getName() { return name; } }
END