当前位置:网站首页>2022-08-10 Group 4 Self-cultivation class study notes (every day)
2022-08-10 Group 4 Self-cultivation class study notes (every day)
2022-08-11 07:29:00 【bodybuilding class】
Annotations and reflections
Annotation: Annotation, Java annotation, a mechanism introduced by JDK5.
Classes, methods, variables, parameters, and packages in Java can be marked
Meta-annotations: annotations specially added to annotations
We found that there can be methods in annotations,
1. The format of the definition method: String name();
2. There can be a default value or not. If there is no default value, you must fill in the corresponding value when using it.
If you need a default value, usedefault specifies the default value.
3. If you want to use it without specifying a specific name,
If you don't learn reflection, annotations are useless!!!
In the entire annotation system of java, there are 3 very important backbone classes,
1. Annotation interface, which defines some commonly used methods
2. ElementType enumeration
It is used to specify annotationstype.Speaking of people, where should I use my annotations???
3. RetentionPolicy enumeration
It is used to specify the annotation policy.The scope of annotations specified by different types of policies is different.
(1) SOURCE, the annotation only exists during the compiler processing. After the compilation is processed, the annotation is useless.
(2) CLASS, the annotation is still valid in the .class file.
(3) RUNTIME, it does not work at compile time, it is read by JVM only at run time.
Java's own annotations, 10.4 annotations java.lang.annotation
6 annotations in java.lang
Annotations that act on the code
1. @Override, check whether the method is an overriding method.If the parent class is returned, or there is no such method in the referenced interface, an error will be reported
2. @Deprecated, marked method, obsolete method.
3. @SuppressWarnings editor to ignore warnings
4. @SafeVarargs, JDK7 supports ignoring any warnings generated by methods or constructors that use parameters as generic variables
5. @FunctionalInterface, JDK8 starts to support,Indicates that an interface is a functional interface
6. @Repeatable, which is supported by JDK8, indicates that an annotation can be used multiple times on the same declaration
all: ignore all warnings
boxing: ignore boxing and unpackingBox warnings
rawtypes: use generation without specifying data types
unchecked: ignore warnings without type checking
unused: ignore warnings when unused
meta-annotations:
1. @Retention: Identifies the scope of this annotation
2. @Documented: Marks whether this annotation is included in the user document
3. @Target: What information can be modified by this annotation
4. @Inherited: If a class usesWith the @Inherited annotation, its subclasses will also inherit this annotation
Create objects using reflection
Several ways to create objects with reflection
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");/** Get the constructor of the class* */Constructor cons = pClass.getDeclaredConstructor(int.class, String.class, String.class);/** Let the constructor execute* */Person person=(Person) cons.newInstance(1,"Xiao Ming","Male");System.out.println(person);/** An object of this class can be instantiated directly with bytecode* Bytecode.newInstance() is actually calling the parameterless constructor of the class to instantiate an object* If you use this method, you must ensure that there must be an empty parameter constructor in the class* */Person person2 = (Person)pClass.newInstance();System.out.println(person2)
Reflecting to assign values to object properties
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");Person person = (Person)pClass.newInstance();System.out.println(person);/** Complete the assignment of object properties through the field object* */Field id = pClass.getDeclaredField("id");Field lastname = pClass.getDeclaredField("lastname");Field gender = pClass.getDeclaredField("gender");Field firstname = pClass.getDeclaredField("firstname");// Private modified properties cannot be accessed directly if you want to use the properties firstid.setAccessible(true);lastname.setAccessible(true);gender.setAccessible(true);firstname.setAccessible(true);// assign a value to the propertyid.set(person,1);lastname.set(person,"Xiao Ming");gender.set(person,"male");// Static member variables can be assigned without an objectfirstname.set(null,"king");System.out.println(person)
Reflection call object method
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");Constructor cons = pClass.getDeclaredConstructor(int.class, String.class, String.class);Person person = (Person)cons.newInstance(1, "Xiao Ming", "Male");// 1 Execute a method with no parameters and no return value// Get the Method object of the method to callMethod showNameMethod = pClass.getDeclaredMethod("showName");// Let the method call the invoke method, which means let the current method execute// If it is an instance method, an object must be required when the method is executed// If the method requires parameters to execute, then the actual parameters are also passed inshowNameMethod.invoke(person);// 2 If executing a method with parameters and return value// Then you need to pass in the actual parameters when callingMethod sumMethod = pClass.getDeclaredMethod("sum", int.class, double.class);// It can be accessed when setting the method, so that the method is private and the method is not available.sumMethod.setAccessible(true);double res=(double)sumMethod.invoke(person,1,3.14);System.out.println(res);// 3 execute a static method// Static methods can be called with objects or with class names// So you can not pass in the object when executing the static methodMethod setFirstname = pClass.getDeclaredMethod("setFirstname", String.class);setFirstname.invoke(null,"Li");System.out.println(person);
Violent Injection
color.setAccessible(true);color.set(dog,"black");System.out.println(dog.getColor());
Singleton Pattern
Constructor Privatization
// singleton mode// 1. Constructor privatizationConstructor declaredConstructor1 = clazz.getDeclaredConstructor(String.class);declaredConstructor1.setAccessible(true);Dog dog1 = declaredConstructor1.newInstance("Xiaoqiang");System.out.println(dog1.getName());
边栏推荐
猜你喜欢
导航定位中的坐标系
Taobao sku API interface (PHP example)
技能在赛题解析:交换机防环路设置
Get Pinduoduo product information operation details
使用Keras构建GAN,以Mnist为例
已解决EROR 1064 (42000): You have an error in. your SOL syntax. check the manual that corresponds to yo
radix-4 FFT principle and C language code implementation
Daily sql: request for friend application pass rate
Daily sql-statistics of the number of professionals (including the number of professionals is 0)
ssh服务攻防与加固
随机推荐
maxwell 概念
unable to extend table xxx by 1024 in tablespace xxxx
Redis源码-String:Redis String命令、Redis String存储原理、Redis字符串三种编码类型、Redis String SDS源码解析、Redis String应用场景
Taobao API interface reference
Strongly recommend an easy-to-use API interface
基于FPGA的FIR滤波器的实现(5)— 并行结构FIR滤波器的FPGA代码实现
prometheus学习4Grafana监控mysql&blackbox了解
prometheus学习5altermanager
概念名词解释
Discourse's Close Topic and Reopen Topic
Amazon API interface Daquan
How to choose professional, safe and high-performance remote control software
Tidb二进制集群搭建
Edge 提供了标签分组功能
How do you optimize the performance of your Unity project?
每日sql -用户两天留存率
Spatial Pyramid Pooling -Spatial Pyramid Pooling (including source code)
JD.com product details API call example explanation
ROS 话题通信理论模型
每日sql-求2016年成功的投资总和