注解
注解入门

1 2 3 4 5 6 7 8
| public class Demo01_Annotation extends Object { @Override public String toString() { return super.toString(); } }
|
内置注解

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Demo01_Annotation extends Object { @Override public String toString() { return super.toString(); } @Deprecated public static void test() { System.out.println("Deprecated"); } @SuppressWarnings("all") public void test01(){ List<String> list = new ArrayList<String>(); } public static void main(String[] args) { test(); } }
|
自定义注解,元注解

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @MyAnnotation public class Demo02_MetaAnnotation { @MyAnnotation public void test() { } }
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited @interface MyAnnotation { }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Demo03_CustomAnnotation { @MyAnnotation2(name = "张三") public void test() { } } @Target(value = {ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation2 { String name() default ""; int age() default 0; int id() default -1; String[] schools() default {"西部开源","清华大学"};
|
反射机制
Java反射机制概念
静态 & 动态语言

反射机制概念

反射机制研究与应用

反射机制优缺点


实现
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
| public class Demo04_Reflection { public static void main(String[] args) throws ClassNotFoundException { Class<?> c = Class.forName("cn.doris.reflection.User"); System.out.println(c); Class<?> c1 = Class.forName("cn.doris.reflection.User"); Class<?> c2 = Class.forName("cn.doris.reflection.User"); Class<?> c3 = Class.forName("cn.doris.reflection.User"); Class<?> c4 = Class.forName("cn.doris.reflection.User");
System.out.println(c1.hashCode()); System.out.println(c2.hashCode()); System.out.println(c3.hashCode()); System.out.println(c4.hashCode()); } }
class User { private String name; private int id; private int age; public User() { } public User(String name, int id, int age) { this.name = name; this.id = id; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
理解Class类并获取Class实例
class类介绍



获取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
| public class Demo05_CreateClass { public static void main(String[] args) throws ClassNotFoundException { Person person = new Student(); System.out.println("这个人是:"+person.name); Class c1 = person.getClass(); System.out.println(c1.hashCode()); Class c2 = Class.forName("cn.doris.reflection.Student"); System.out.println(c2.hashCode()); Class c3 = Student.class; System.out.println(c3.hashCode()); Class c4 = Integer.TYPE; System.out.println(c4); Class c5 = c1.getSuperclass(); System.out.println(c5); } } class Person { String name; public Person() { } public Person(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name=" + name + '}'; } } class Student extends Person { public Student() { this.name = "学生"; } } class Teacher extends Person { public Teacher() { this.name = "老师"; } }
|
哪些类型可以有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
| public class Demo06_AllTypeClass { public static void main(String[] args) { Class c1 = Object.class; Class c2 = Comparable.class; Class c3 = String[].class; Class c4 = int[][].class; Class c5 = Override.class; Class c6 = ElementType.class; Class c7 = Integer.class; Class c8 = void.class; Class c9 = Class.class; System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4); System.out.println(c5); System.out.println(c6); System.out.println(c7); System.out.println(c8); System.out.println(c9); int[] a = new int[10]; int[] b = new int[100]; System.out.println(a.getClass().hashCode()); System.out.println(b.getClass().hashCode()); } }
|
类的加载与ClassLoader
Java内存分析

类的加载


深刻理解类加载:https://blog.csdn.net/m0_38075425/article/details/81627349
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
| public class Demo07_ClassLoader { public static void main(String[] args) { A a = new A(); System.out.println(A.m);
} } class A { static { System.out.println("A类静态代码块初始化"); m = 300; } static int m = 100; public A() { System.out.println("A类无参构造初始化"); } }
|
分析上面代码

程序自上往下执行
什么时候会发生类初始化

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
| public class Demo08_ActiveReference { static { System.out.println("Main类被加载"); } public static void main(String[] args) throws ClassNotFoundException { } } class Father { static final int b = 2; static { System.out.println("父类被加载"); } } class Son extends Father { static { System.out.println("子类被加载"); m = 100; } static int m = 300; static final int a = 1; }
|
类加载器的作用


ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();//获取系统类的加载器
ClassLoader parent = systemClassLoader.getParent();//获取系统类加载器的父类加载器—>扩展类加载器 jre1.8.0_91\lib\ext
ClassLoader parent1 = parent.getParent();//获取扩展类加载器父类加载器—>根加载器(c/c++) jre1.8.0_91\lib\rt.jar
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
| public class Demo09_ClassLoader1 { public static void main(String[] args) throws ClassNotFoundException { ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); System.out.println(systemClassLoader); ClassLoader parent = systemClassLoader.getParent(); System.out.println(parent); ClassLoader parent1 = parent.getParent(); System.out.println(parent1); ClassLoader classLoader = Class.forName("cn.doris.reflection.Demo09_ClassLoader1").getClassLoader(); System.out.println(classLoader); classLoader = Class.forName("java.lang.Object").getClassLoader(); System.out.println(classLoader); System.out.println(System.getProperty("java.class.path"));
} }
|
创建运行时类的对象
获取运行类的完整结构

方法:
Class c1 = Class.forName(“cn.doris.reflection.User”); //获取当前对象的Class
//获得类的名字
c1.getName();// 获得包名 + 类名
c1.getSimpleName();// 获得类名
//获得类的属性
c1.getFields();//只能找到public属性
c1.getDeclaredFields();//找到全部的属性
c1.getDeclaredField(“name”); //获得指定属性的值
//获得类的方法
c1.getMethods(); //获得本类及父类的全部public方法
c1.getDeclaredMethods(); //获得本类的所有方法
c1.getMethod(“getName”, null);//获得指定的方法
//获得类的构造器
c1.getConstructors();
c1.getDeclaredConstructors();
c1.getDeclaredConstructor(String.class, int.class, int.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 50 51 52 53 54
| public class Demo10_ClassInfo { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException { Class c1 = Class.forName("cn.doris.reflection.User"); User user = new User(); c1 = user.getClass(); System.out.println(c1.getName()); System.out.println(c1.getSimpleName()); System.out.println("======================="); Field[] fields = c1.getFields(); for (Field field : fields) { System.out.println("getFields:" + field); } fields = c1.getDeclaredFields(); for (Field field : fields) { System.out.println("getDeclaredFields:" + field); } Field name = c1.getDeclaredField("name"); System.out.println(name); System.out.println("======================="); Method[] methods = c1.getMethods(); for (Method method : methods) { System.out.println("getMethods:" + method); } methods = c1.getDeclaredMethods(); for (Method method : methods) { System.out.println("getDeclaredMethods:" + method); } System.out.println("======================="); Method getName = c1.getMethod("getName", null); Method setName = c1.getMethod("setName", String.class); System.out.println(getName); System.out.println(setName); System.out.println("======================="); Constructor[] constructors = c1.getConstructors(); for (Constructor constructor : constructors) { System.out.println("getConstructors:" + constructor); } constructors = c1.getDeclaredConstructors(); for (Constructor constructor : constructors) { System.out.println("getDeclaredConstructors:" + constructor); } Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class); System.out.println("指定构造器" + declaredConstructor); } }
|

调用运行时类的指定结构
有Class对象,能做什么

方法



//获得Class对象
Class c1 = Class.forName(“cn.doris.reflection.User”);
//本质上调用了类的无参构造器
User user = (User) c1.newInstance();
//构造器创建对象
Constructor constructor=c1.getDeclaredConstructor(String.class, int.class, int.class);
User user1 = (User) constructor.newInstance(“长歌”,001,17);
//invoke:激活
// (对象,”方法值”)
setName.invoke(user2, “doris”);
//设置安全检测
name.setAccessible(true);
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
| public class Demo11_DynamicCreateObject { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { Class c1 = Class.forName("cn.doris.reflection.User");
User user2 = (User) c1.newInstance(); Method setName = c1.getDeclaredMethod("setName", String.class); setName.invoke(user2, "doris"); System.out.println(user2.getName()); User user3 = (User) c1.newInstance(); Field name = c1.getDeclaredField("name"); name.setAccessible(true); name.set(user3, "doris2"); System.out.println(user3.getName()); } }
|
性能检测:
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
| public class Demo12_Performance { public static void test01() { User user = new User(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { user.getName(); } long endTime = System.currentTimeMillis(); System.out.println("普通方式执行10亿次:" + (endTime - startTime) + "ms"); } public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { getName.invoke(user,null); } long endTime = System.currentTimeMillis(); System.out.println("反射方式执行10亿次:" + (endTime - startTime) + "ms"); } public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); getName.setAccessible(true); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { getName.invoke(user,null); } long endTime = System.currentTimeMillis(); System.out.println("反射方式执行10亿次,关闭检测:" + (endTime - startTime) + "ms"); } public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { test01(); test02(); test03(); } }
|
反射操作泛型

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
| public class Demo12_Performance { public static void test01() { User user = new User(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { user.getName(); } long endTime = System.currentTimeMillis(); System.out.println("普通方式执行10亿次:" + (endTime - startTime) + "ms"); } public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { getName.invoke(user,null); } long endTime = System.currentTimeMillis(); System.out.println("反射方式执行10亿次:" + (endTime - startTime) + "ms"); } public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); getName.setAccessible(true); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { getName.invoke(user,null); } long endTime = System.currentTimeMillis(); System.out.println("反射方式执行10亿次,关闭检测:" + (endTime - startTime) + "ms"); } public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { test01(); test02(); test03(); } }
|
反射操作注解


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 73 74 75 76 77
| public class Demo14_ORM { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("cn.doris.reflection.Student2"); Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } TableDoris tableDoris = (TableDoris) c1.getAnnotation(TableDoris.class); String value = tableDoris.value(); System.out.println(value); Field name = c1.getDeclaredField("name"); FiledDoris annotation = name.getAnnotation(FiledDoris.class); System.out.println(annotation.columnName()); System.out.println(annotation.type()); System.out.println(annotation.length()); } } @TableDoris("db_student") class Student2 { @FiledDoris(columnName = "db_id", type = "int", length = 10) private int id; @FiledDoris(columnName = "db_age", type = "int", length = 3) private int age; @FiledDoris(columnName = "db_name", type = "varchar", length = 200) private String name; public Student2() { } public Student2(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } @Override public String toString() { return "Student2{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableDoris { String value(); }
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FiledDoris { String columnName(); String type(); int length(); }
|