Annotations are not the program itself. They can be read during program compilation, class loading and runtime, and execute the corresponding processing. The format of the annotation is "@annotation name (parameter value)", which can be attached to packages, classes, methods and fields, and the annotation is accessed through the reflection mechanism.
1.2 Built-in annotation
@Override: Restrict subclasses to override methods
This annotation indicates that the method of the parent class is overridden. When a class overrides a parent class method, ensure that the subclass actually overrides the parent class method to avoid low-level errors
This An annotation indicates that a property, method, or class is obsolete (a program element that programmers are discouraged from using, usually because it is dangerous or because a better alternative exists). When other programs use the obsolete property, method or class, the compiler will give a warning (strikethrough).
The classes, methods and properties affected by this annotation will suppress the display of compiler warnings, and its parameters are mainly for Warning instructions and cancellation (unchecked), etc.
@SuppressWarnings("取消此类的所有警告")
public class BuiltAnnotation {
@SuppressWarnings("取消此属性的警告")
private String username;
@SuppressWarnings("取消此方法的警告")
public static void main(String[] args) {
// ...
}
}
Copy after login
1.3 Meta-annotation (meta-annotation)
The role of meta-annotation is to annotate other annotations. Java defines 4 standard meta-annotation types, which are used to provide support for other annotations. The scope and type are described, and other annotations can be customized through meta-annotations.
@Target: Describe the scope of use of annotations
For example, @Target(ElementType.METHOD) indicates that it acts on a method, @Target(ElementType.TYPE) indicates that it acts on Class or interface etc.
@Document: Indicates that this annotation will be included in javadoc
##@Iherited: An annotation that defines whether a subclass can inherit the parent class definition.
@Inherited is only useful for annotations of the @Target(ElementType.TYPE) type, and can only be inheritance of class, and is invalid for inheritance of interface: 1.4 Custom annotations
@Report(type = 1, value = "test")
public class CustomerAnnotation {
@Report(type = 1, value = "test")
public void testCustomerAnnotation() {
System.out.println("测试自定义注解");
}
}
Copy after login
2. Reflection2.1 Reflection and Reflection mechanism
Reflection
Reflection (reflection) means that the program can get all the information of an object during runtime.
Reflection mechanism
The reflection mechanism means that when the program is running, it obtains the content information of any class through the Reflection API, and can directly operate the internal properties and methods of any object. . 2.2 How to obtain the Class class and common methodsThe java.lang.Class class is the core class that implements reflection. After the class is loaded, a class will be generated in the method area of the heap memory. Class object (a class has only one Class object), this object contains the complete structural information of the class, and the structure of the class can be seen through this object.
public class ClassOperateGenerics {
public Map<String, String> list() {
System.out.println("返回值是泛型");
return new HashMap<>();
}
public void test(Map<String, User> map, List<Integer> list) {
System.out.println("参数是泛型");
}
public static void main(String[] args) throws NoSuchMethodException {
/**
* 获取方法参数的泛型
*/
Method method = ClassOperateGenerics.class.getMethod("test", Map.class, List.class);
// 获取所有方法参数的泛型
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
// java.util.Map<java.lang.String, com.loner.mj.reflection.User>
System.out.println(genericParameterType);
if (genericParameterType instanceof ParameterizedType) {
// 获取所有泛型的真实参数
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
// String, User, Integer
System.out.println(actualTypeArgument);
}
}
}
/**
* 获取方法返回值的泛型
*/
Method list = ClassOperateGenerics.class.getMethod("list", null);
// 获取方法返回值的泛型
Type genericReturnType = list.getGenericReturnType();
if (genericReturnType instanceof ParameterizedType) {
// 获取所有泛型的真实参数
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
}
Copy after login
反射操作注解
public class ClassOperateAnnotation {
public static void main(String[] args) throws NoSuchFieldException {
Class<People> peopleClass = People.class;
// 获取类的所有注解
Annotation[] declaredAnnotations = peopleClass.getDeclaredAnnotations();
for (Annotation declaredAnnotation : declaredAnnotations) {
System.out.println(declaredAnnotation);
}
// 获取类的注解的值
Table declaredAnnotation = peopleClass.getDeclaredAnnotation(Table.class);
System.out.println(declaredAnnotation.value());
// 获取属性的注解
Field name = peopleClass.getDeclaredField("name");
Fields annotation = name.getAnnotation(Fields.class);
System.out.println(annotation.name());
}
}
Copy after login
The above is the detailed content of How to use annotations and reflection in Java. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn