Java Build in Annotation¶
Overview¶
- Information for the compiler: Annotations can be used by the compiler to detect errors or suppress warnings.
- Compile-time and deployment-time processing: Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing: Some annotations are available to be examined at runtime.
API¶
meta annotation
@Target @Retention @Documented @Inherited
Target
PACKAGE: package TYPE: class, interface, enumeration CONSTRUCTOR: METHOD: FIELD: LOCAL_VARABLE:
Retention
SOURCE: CLASS: RUNTIME: for reflection
Target + Retention¶
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = { ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationDemo { String studentName() default ""; // 参数名,参数类型 int age() default 0; int id() default -1; //String indexOf("abc") -1 String[] schools() default {"school1", "school2"}; }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = { ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationDemo02 { String value(); }
import java.util.ArrayList; import java.util.Date; import java.util.List; @AnnotationDemo(age = 19, studentName = "sname", id = 10001, schools = { "school1, school2" }) public class Demo { @Override @AnnotationDemo public String toString() { return ""; } @AnnotationDemo02(value = "aaaa") public void test2() { } @AnnotationDemo02("aaaa") public void test3() { } @Deprecated public static void test001() { System.out.println("test001"); } @SuppressWarnings("all") public static void test002() { List list = new ArrayList(); List list2 = new ArrayList(); } @SuppressWarnings("all") public static void main(String[] args) { Date d = new Date(); d.getDate(); test001(); } }
Reflection Annotation¶
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = {ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Table { String value(); }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = { ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public @interface Field { String columnName(); String type(); int length(); }
package annotation; @Table("tb_student") public class Student { @Field(columnName = "id", type = "int", length = 10) private int id; @Field(columnName = "studentName", type = "String", length = 10) private String studentName; @Field(columnName = "age", type = "int", length = 3) private int age; public SxtStudent() { } public SxtStudent(int id, String studentName, int age) { this.id = id; this.studentName = studentName; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Demo { /** * @param args * @throws SecurityException * @throws NoSuchFieldException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @throws IllegalArgumentException */ @SuppressWarnings("all") public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { try { String path = "annotation/Student"; Class clazz = Class.forName("annotation.Student"); // 获得名字 System.out.println(clazz.getName()); // 获得包名 + 类名 System.out.println(clazz.getSimpleName()); // 获得包名 + 类名 // 获得属性信息 Field[] fields = clazz.getFields(); System.out.println(fields.length); Field[] private_and_all_fields = clazz.getDeclaredFields(); System.out.println(private_and_all_fields.length); for (Field f : private_and_all_fields) { System.out.print(f.getName() + " "); } System.out.print("\n"); Field f = clazz.getDeclaredField("studentName"); System.out.println(f.getName()); // 获得方法 Method[] methods = clazz.getMethods(); for (Method m : methods) { System.out.print(m.getName() + " "); } System.out.print("\n"); Method m = clazz.getDeclaredMethod("getId", null); System.out.println(m.getName()); m = clazz.getDeclaredMethod("setId", int.class);// 如果方法有参数,必须传参数类型 System.out.println(m.getName()); // 获得构造器信息 Constructor[] cons = clazz.getDeclaredConstructors(); for (Constructor c : cons) { System.out.println("构造器: " + c.getName() + " "); } Constructor c = clazz.getDeclaredConstructor(null); System.out.println("构造器: " + c.getName() + " "); c = clazz.getDeclaredConstructor(int.class, String.class, int.class); System.out.println("构造器: " + c.getName() + " "); clazz = Class.forName("annotation.Student"); Student stu = (Student) clazz.newInstance();// 其实调用的无参的构造器 System.out.println(stu); Constructor<Student> cstu = clazz.getConstructor(int.class, String.class, int.class); stu = cstu.newInstance(100, "呵呵", 28); System.out.println(stu); // 反射方法 Method method = clazz.getDeclaredMethod("setId", int.class); method.invoke(stu, 98); System.out.println(stu.getId()); // 反射API操作属性 Field field = clazz.getDeclaredField("studentName"); field.setAccessible(true); field.set(stu, "瓦哈啊哈"); System.out.println(stu.getStudentName()); Annotation[] annotations = clazz.getAnnotations(); for (Annotation a : annotations) { System.out.println(a); } Table st = (Table) clazz.getAnnotation(Table.class); System.out.println(st.value()); Field f0 = clazz.getDeclaredField("studentName"); Field sfield = f0.getAnnotation(Field.class); System.out.println(sfield.columnName() + "--" + sfield.type() + "--" + sfield.length()); // Jvm 创建一个Clas对象 System.out.println(clazz); System.out.println(clazz.hashCode()); Class clazz2 = Class.forName("annotation.SxtStudent"); System.out.println(clazz2); System.out.println(clazz2.hashCode()); // Jvm 创建一个Clas对象 Class strClass = String.class; Class strClass2 = path.getClass(); System.out.println(strClass.hashCode()); System.out.println(strClass2.hashCode()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
Authors¶
- Weiduo Sun -
License¶
This project is licensed under the MIT License - see the LICENSE.md file for details