当前位置:网站首页>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());

原网站

版权声明
本文为[bodybuilding class]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110544079685.html