|
@@ -0,0 +1,235 @@
|
|
|
+package com.sf.quanrizhi;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.annotation.ElementType;
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+public class T {
|
|
|
+ @Test
|
|
|
+ public void t1() throws ClassNotFoundException {
|
|
|
+ // 1
|
|
|
+ Class<User> userClass = User.class;
|
|
|
+
|
|
|
+ //2
|
|
|
+ User user = new User();
|
|
|
+ Class<? extends User> aClass = user.getClass();
|
|
|
+ //3
|
|
|
+ Class<?> aClass1 = Class.forName("com.sf.quanrizhi.User");
|
|
|
+
|
|
|
+ //4
|
|
|
+ ClassLoader classLoader = this.getClass().getClassLoader();
|
|
|
+ Class<?> aClass2 = classLoader.loadClass("com.sf.quanrizhi.User");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
|
|
+ Class<?> clazz = Class.forName("com.sf.quanrizhi.Student");
|
|
|
+ Object o = clazz.newInstance();
|
|
|
+ Student student = null;
|
|
|
+ if(o instanceof Student){
|
|
|
+ student = (Student) o;
|
|
|
+ }
|
|
|
+ System.out.println(student);
|
|
|
+ String str = "com.sf.reflectiontest.Student";
|
|
|
+// Class<?> clazz = Class.forName(str);
|
|
|
+// Object o = clazz.newInstance();
|
|
|
+ System.out.println((Student)o);
|
|
|
+ String name = clazz.getName();
|
|
|
+ System.out.println(name);
|
|
|
+ System.out.println(clazz.getSuperclass());
|
|
|
+ System.out.println(Arrays.toString(clazz.getInterfaces()));
|
|
|
+ ClassLoader classLoader = clazz.getClassLoader();
|
|
|
+ System.out.println(classLoader);
|
|
|
+ System.out.println(Arrays.toString(clazz.getConstructors()));
|
|
|
+ System.out.println(Arrays.toString(clazz.getDeclaredMethods()));
|
|
|
+ System.out.println(Arrays.toString(clazz.getMethods()));
|
|
|
+ System.out.println(clazz.getMethod("setId",Integer.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t3(){
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ //总结:
|
|
|
+ /**
|
|
|
+ * getField("pName"); 获取属性 包含父类 (public)
|
|
|
+ * getFields():获得某个类的所有的公共(public)的字段,包括父类中的字段。
|
|
|
+ * getDeclaredFields():获得当前类的所有声明的字段,不包含父类 即包括public、private和proteced,
|
|
|
+ *
|
|
|
+ * getConstructors() 获取当前类的构造器(public)
|
|
|
+ * getDeclaredConstructors() 获取当前类的构造器(所有声明的)
|
|
|
+ *
|
|
|
+ * getMethods是获取类中所有公共方法,包括继承自父类的
|
|
|
+ * getDeclaredMethods是获取类中自己声明的方法,即自己声明的任何权限的方法,包括私有方法 不包含父类方法
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test01(){
|
|
|
+ ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
|
|
+ System.out.println("systemClassLoader = " + systemClassLoader);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test02()throws Exception{
|
|
|
+ ClassLoader c1 = String.class.getClassLoader();
|
|
|
+ System.out.println("加载String类的类加载器:" + c1);
|
|
|
+
|
|
|
+ ClassLoader c2 = T.class.getClassLoader();
|
|
|
+ System.out.println("加载当前类的类加载器:" + c2);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test03(){
|
|
|
+ ClassLoader c1 = T.class.getClassLoader();
|
|
|
+ System.out.println("加载当前类的类加载器c1=" + c1);
|
|
|
+
|
|
|
+ ClassLoader c2 = c1.getParent();
|
|
|
+ System.out.println("c1.parent = " + c2);
|
|
|
+
|
|
|
+ ClassLoader c3 = c2.getParent();
|
|
|
+ System.out.println("c2.parent = " + c3);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用ClassLoader获取流
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test04() throws IOException {
|
|
|
+ InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.load(resourceAsStream);
|
|
|
+ String name = properties.getProperty("name");
|
|
|
+ String password = properties.getProperty("password");
|
|
|
+ System.out.println(name);
|
|
|
+ System.out.println(password);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1() throws Exception{
|
|
|
+// LcClass obj = new lLClass();//编译期间无法创建
|
|
|
+
|
|
|
+ Class<?> clazz = Class.forName("com.lc.demo.LcClass");
|
|
|
+ //clazz代表com.lc.demo.LcClass类型
|
|
|
+ //clazz.newInstance()创建的就是LcClass的对象
|
|
|
+ Object obj = clazz.newInstance();
|
|
|
+ System.out.println(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test2()throws Exception{
|
|
|
+ Class<?> clazz = Class.forName("com.lc.demo.LcDemo");
|
|
|
+ //java.lang.InstantiationException: com.lc.demo.LcDemo
|
|
|
+ //Caused by: java.lang.NoSuchMethodException: com.lc.demo.LcDemo.<init>()
|
|
|
+ //即说明lcDemo没有无参构造,就没有无参实例初始化方法<init>
|
|
|
+ Object o = clazz.newInstance();
|
|
|
+ System.out.println(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test3()throws Exception{
|
|
|
+ //(1)获取Class对象
|
|
|
+ Class<?> clazz = Class.forName("com.lc.demo.LcDemo");
|
|
|
+ /*
|
|
|
+ * 获取lcDemo类型中的有参构造
|
|
|
+ * 如果构造器有多个,我们通常是根据形参【类型】列表来获取指定的一个构造器的
|
|
|
+ * 例如:public LcDemo(String title, int num)
|
|
|
+ */
|
|
|
+ //(2)获取构造器对象
|
|
|
+ Constructor<?> constructor = clazz.getDeclaredConstructor(String.class,int.class);
|
|
|
+
|
|
|
+ //(3)创建实例对象
|
|
|
+ // T newInstance(Object... initargs) 这个Object...是在创建对象时,给有参构造的实参列表
|
|
|
+ Object obj = constructor.newInstance("爱扣钉",2022);
|
|
|
+ System.out.println(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过反射的形式对当前Student类进行赋值
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t5() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
|
|
|
+ // 获取student对象的class对象
|
|
|
+ Class<?> clazz = Class.forName("com.sf.quanrizhi.Student");
|
|
|
+ //获取对象属性
|
|
|
+ Field idFiled = clazz.getDeclaredField("id");
|
|
|
+ //取消java语言访问权限
|
|
|
+ idFiled.setAccessible(true);
|
|
|
+ //创建student的实例对象
|
|
|
+ Object stu = clazz.newInstance();
|
|
|
+ //获取属性值
|
|
|
+ Object o = idFiled.get(stu);
|
|
|
+ System.out.println(o);
|
|
|
+
|
|
|
+ //操作当前属性值
|
|
|
+ idFiled.set(stu,120);
|
|
|
+
|
|
|
+ System.out.println(idFiled.get(stu));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过反射的形式去操作方法
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t7() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
|
|
|
+ Class<?> clazz = Class.forName("com.sf.quanrizhi.Student");
|
|
|
+ Method setNameMethod = clazz.getDeclaredMethod("setName", String.class);
|
|
|
+ //实例化对象
|
|
|
+ Object obj = clazz.newInstance();
|
|
|
+
|
|
|
+ Object objectMethod = setNameMethod.invoke(obj, "zhangsan");
|
|
|
+ System.out.println(objectMethod); //null
|
|
|
+ //掉用方法获取设置值 获取getName方法
|
|
|
+ Method getName = clazz.getDeclaredMethod("getName");
|
|
|
+ Object invoke = getName.invoke(obj);
|
|
|
+ System.out.println(invoke);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t8() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
|
+// Student.printInfo();
|
|
|
+ Class<?> clazz = Class.forName("com.sf.quanrizhi.Student");
|
|
|
+ Method printInfo = clazz.getMethod("printInfo",String.class);
|
|
|
+ printInfo.invoke(null,"zs");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t9() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
|
|
+ InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("user.properties");
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.load(resourceAsStream);
|
|
|
+ String className = properties.getProperty("className");
|
|
|
+ String methodName = properties.getProperty("methodName");
|
|
|
+ Class<?> clazz = Class.forName(className);
|
|
|
+ Object o = clazz.newInstance();
|
|
|
+ Method method = clazz.getMethod(methodName);
|
|
|
+ Object invoke = method.invoke(o);
|
|
|
+ System.out.println(invoke);
|
|
|
+ }
|
|
|
+}
|