|
@@ -1,385 +0,0 @@
|
|
|
-package com.sf.quanrizhi.day01;
|
|
|
-
|
|
|
-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.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-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.day01.User");
|
|
|
-
|
|
|
- //4
|
|
|
- ClassLoader classLoader = this.getClass().getClassLoader();
|
|
|
- Class<?> aClass2 = classLoader.loadClass("com.sf.quanrizhi.day01.User");
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void t2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
|
|
- Class<?> clazz = Class.forName("com.sf.quanrizhi.day01.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.day01.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.day01.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.day01.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);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void t10(){
|
|
|
- Class<Student1> student1Class = Student1.class;
|
|
|
- Table annotation = student1Class.getAnnotation(Table.class);
|
|
|
- String tableName = "";
|
|
|
- if(annotation !=null){
|
|
|
- tableName = annotation.value();
|
|
|
- }
|
|
|
- Field[] declaredFields = student1Class.getDeclaredFields();
|
|
|
- String[] columns= new String[declaredFields.length];
|
|
|
- int index = 0;
|
|
|
- for (Field declaredField : declaredFields) {
|
|
|
- Column annotation1 = declaredField.getAnnotation(Column.class);
|
|
|
- if(annotation1 != null){
|
|
|
- columns[index++]= annotation1.columnName();
|
|
|
- }
|
|
|
- }
|
|
|
- String sql = "SELECT ";
|
|
|
- for (int i = 0;i<index;i++){
|
|
|
- sql += columns[i];
|
|
|
- if(i<index-1){
|
|
|
- sql+=",";
|
|
|
- }
|
|
|
- }
|
|
|
- sql += " FROM "+tableName;
|
|
|
- System.out.println(sql);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过反射
|
|
|
- * 像List<String> 集合中添加整型int
|
|
|
- *
|
|
|
- */
|
|
|
- @Test
|
|
|
- public void t11() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
|
- //创建list集合
|
|
|
- List<String> list = new ArrayList<>();
|
|
|
- //应该获取Arraylist 的class对象
|
|
|
- Class<? extends List> listClass = list.getClass();
|
|
|
- Method add = listClass.getMethod("add", Object.class);
|
|
|
- add.invoke(list,1);
|
|
|
- add.invoke(list,"hello");
|
|
|
- add.invoke(list,true);
|
|
|
- System.out.println(list);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Test
|
|
|
- public void t12(){
|
|
|
- //启动一个线程 并且输出一句话
|
|
|
- new Thread(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- System.out.println("开启一个新的线程!!");
|
|
|
- }
|
|
|
- }).start();
|
|
|
-
|
|
|
- new Thread(() -> System.out.println("开启一个新的线程!!")).start();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 筛选出所有名称包含手机的商品
|
|
|
- */
|
|
|
- @Test
|
|
|
- public void t13(){
|
|
|
- List<Product> productsWithName = findProductsWithName(Data.products);
|
|
|
- System.out.println(productsWithName);
|
|
|
-
|
|
|
- // 筛选出价格大于1000的商品
|
|
|
- List<Product> productsWithPrice = findProductsWithPrice(Data.products);
|
|
|
- System.out.println(productsWithPrice);
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Product> findProductsWithName(List<Product> products){
|
|
|
- List<Product> list = new ArrayList<>();
|
|
|
- for (Product p : products) {
|
|
|
- if(p.getName().contains("手机")){
|
|
|
- list.add(p);
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Product> findProductsWithPrice(List<Product> products){
|
|
|
- List<Product> list = new ArrayList<>();
|
|
|
- for (Product product : products) {
|
|
|
- if(product.getPrice() > 1000){
|
|
|
- list.add(product);
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Test
|
|
|
- public void t14(){
|
|
|
- // 筛选出名称种包含手机的商品
|
|
|
- List<Product> productsWithCondition = findProductsWithCondition(Data.products, new MyPredicate() {
|
|
|
- @Override
|
|
|
- public boolean test(Product product) {
|
|
|
- return product.getName().contains("手机");
|
|
|
- }
|
|
|
- });
|
|
|
- System.out.println(productsWithCondition);
|
|
|
-
|
|
|
- List<Product> productsWithCondition1 = findProductsWithCondition(Data.products, new MyPredicate() {
|
|
|
- @Override
|
|
|
- public boolean test(Product product) {
|
|
|
- return product.getPrice() > 1000;
|
|
|
- }
|
|
|
- });
|
|
|
- System.out.println(productsWithCondition1);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Product> findProductsWithCondition(List<Product> products ,MyPredicate condition){
|
|
|
- List<Product> list = new ArrayList<>();
|
|
|
- for (Product product : products) {
|
|
|
- if(condition.test(product)){
|
|
|
- list.add(product);
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Test
|
|
|
- public void t16(){
|
|
|
- findPhoneByCondition(Data.products,(product ->
|
|
|
- product.getName().contains("手机")
|
|
|
- ));
|
|
|
-
|
|
|
- // 价格
|
|
|
- findPhoneByCondition(Data.products,(product -> product.getPrice()>1000));
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Product> findPhoneByCondition(List<Product> products,MyPredicate myPredicate){
|
|
|
- List<Product> list = new ArrayList<>();
|
|
|
- for (Product product : products) {
|
|
|
- if(myPredicate.test(product)){
|
|
|
- list.add(product);
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
-}
|