当前位置:网站首页>Annotation and reflection
Annotation and reflection
2022-07-26 14:10:00 【Look at the bugs】
Get the reflection object
package TestReflected;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println(" This man is "+person.name);
// Mode one
Class C1=person.getClass();
System.out.println(C1.hashCode());
// Mode two
Class C2 = Class.forName("TestReflected.Test02");
System.out.println(C2.hashCode());
// Mode three : By class name .class get
Class C3=Test02.class;
System.out.println(C3.hashCode());
// Mode 4
Class C4 = Integer.TYPE;
System.out.println(C4);
// Get the parent type
Class C6 = C1.getSuperclass();
System.out.println(C6);
}
}
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=" Student ";
}
}
class Teacher extends Person{
public Teacher() {
this.name=" teacher ";
}
}
All types of Class object
package TestReflected;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
public class Test03 {
public static void main(String[] args) {
Class C1=Object.class; // class
Class C2=Comparable.class; // Interface
Class C3=String[].class;
Class C4=int[][].class;
Class C5= Annotation.class;
Class C6= ElementType.class; // Enumeration type
Class C7=Integer.class; // Basic data type
Class C8 =void.class; //void
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);
// As long as the element type is the same as the dimension , It's the same Class
int[] a =new int[10];
int[]b=new int[100];
System.out.println(a.hashCode());
System.out.println(b.hashCode());
}
}
Class initialization
package TestReflected;
// When does the test class initialize
public class Test04 {
static {
System.out.println("main Class loaded ");
}
public static void main(String[] args) throws ClassNotFoundException {
//1, Active citation
// Son son =new Son();
// main Class loaded
// The parent class is loaded
// Subclasses are loaded
// Reflection also produces active references
// Class.forName("TestReflected.Son");
// main Class loaded
// The parent class is loaded
// Subclasses are loaded
// Methods that do not produce a reference to a class
// System.out.println(Son.b);
//main Class loaded
// The parent class is loaded
//2
// Array will not load class
Son[] array=new Son[5];
}
}
class Father{
static int b=2;
static {
System.out.println(" The parent class is loaded ");
}
}
class Son extends Father{
static {
System.out.println(" Subclasses are loaded ");
m=300;
}
static int m=100;
static final int M=1;
}
Class loader
Class loading function : take class File bytecode content loaded into memory , And convert these static data into the runtime data structure of the method area , Then generate a... Representing this class in the heap java.lang.Class object , As an access to the class data in the method area ;
Class cache : The standard JavaSE The class loader can find classes as required , But once a class is loaded into the classloader , It will keep loading ( cache ) A span , however jvm The garbage collection mechanism can recycle these Class object ;
package TestReflected;
public class Test05 {
public static void main(String[] args) throws Exception {
// Get the loader of the system class
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);
// Get the system classloader's parent classloader --> Extend the classloader
ClassLoader parent=systemClassLoader.getParent();
System.out.println(parent);
// Get the parent loader of the extension class loader --> Root loader
ClassLoader parent1= parent.getParent();
System.out.println(parent1);
// Test which loader is loading the current class
ClassLoader classLoader= Class.forName("TestReflected.Test05").getClassLoader();
System.out.println(classLoader);
// Test which class the current class is loaded
ClassLoader classLoader2 =Class.forName("java.lang.Object").getClassLoader();
System.out.println(classLoader2);
// How to get the path that the system class loader can load
System.out.println(System.getProperty("java.class.path"));
}
}
Get the runtime structure of the class
边栏推荐
- win10安装Dexdump并脱壳
- First knowledge of opencv4.x --- image perspective transformation
- Learning basic knowledge of Android security
- .net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手
- Subscription and publication of messages
- 『SignalR』.NET使用 SignalR 进行实时通信初体验
- Flink SQL(三) 连接到外部系统System和JDBC
- Plato farm is expected to further expand its ecosystem through elephant swap
- JS submit the form to determine whether the user name and password are empty
- 『BaGet』带你一分钟搭建自己的私有NuGet服务器
猜你喜欢
随机推荐
『云原生』KubeSphere可插拔组件之DevOps系统
在检测分割中一些轻量级网络模型(自己学习的笔记分享)
【数学建模】常用基本模型总结
Technology sharing | gtid that needs to be configured carefully_ mode
[GYCTF2020]FlaskApp
Solve the problem that JUnit of idea console cannot be input with scanner
UDP多线程在线聊天
~6. ccf 2021-09-1 数组推导
win10安装Dexdump并脱壳
Docker swarm cluster builds highly available MySQL active and standby
Prediction and value evaluation of technology fusion relationship based on multiple features
OLAP (business) - transaction analysis (query)
大小端模式
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Latest battle report: Ten certifications and five best practices
Go multithread communication, control coordination and main thread shutdown (sync.waitgroup)
最新战报:十项认证,五项最佳实践
Add a display horizontal line between idea methods
C语言贪吃蛇-链表和指针练习
Research on prediction of user churn in online health community based on user portrait









