内置注解

元注解

自定义注解

使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口

分析:

  • @interface用来声明一个注解,格式:public @ interface注解名{定义内容}
  • 其中的每一个方法实际上是声明了一个配置参数
  • 方法的名称就是参数的名称反射

java Reflection

反射得类方法:


  1. public class Test03 {
  2. public static void main(String[] args) throws Exception{
  3. //通过反射获取类的class对象
  4. Class c1 = Class.forName("com.dong.annotation.User");
  5. System.out.println(c1);
  6. //一个类在内存中只有一个class对象
  7. //一个类被加载后,类的整个结构都会被封装在class对象中
  8. }
  9. }
  10. class User{
  11. private String name;
  12. private int id;
  13. private int age;
  14. public User(String name, int id, int age) {
  15. this.name = name;
  16. this.id = id;
  17. this.age = age;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public int getId() {
  26. return id;
  27. }
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31. public int getAge() {
  32. return age;
  33. }
  34. public void setAge(int age) {
  35. this.age = age;
  36. }
  37. @Override
  38. public String toString() {
  39. return "User{" +
  40. "name='" + name + '\'' +
  41. ", id=" + id +
  42. ", age=" + age +
  43. '}';
  44. }
  45. }
  1. public class Test04 {
  2. public static void main(String[] args) {
  3. Person person=new Student();
  4. System.out.println("这个人是"+person.name);
  5. //方式一:通过对象获得
  6. Class c1 = person.getClass();
  7. System.out.println(c1);
  8. System.out.println(c1.hashCode());
  9. //方拾二:forName获得
  10. try {
  11. Class c2 = Class.forName("com.dong.annotation.Student");
  12. System.out.println(c2);
  13. } catch (ClassNotFoundException e) {
  14. e.printStackTrace();
  15. }
  16. //方式三:通过类名.class获得
  17. Class c3 = Student.class;
  18. System.out.println(c3);
  19. //方式四:基本内置类型里的包装类有一个type属性
  20. Class c4 = Integer.TYPE;
  21. System.out.println(c4);
  22. //方式五:获得父类类型
  23. Class c5 = c1.getSuperclass();
  24. System.out.println(c5);
  25. }
  26. }
  27. class Person{
  28. String name;
  29. public Person() {
  30. }
  31. public Person(String name) {
  32. this.name = name;
  33. }
  34. @Override
  35. public String toString() {
  36. return "Person{" +
  37. "name='" + name + '\'' +
  38. '}';
  39. }
  40. }
  41. class Student extends Person{
  42. public Student() {
  43. }
  44. public Student(String name) {
  45. this.name="赵东";
  46. }
  47. }
  48. class Teacher extends Person{
  49. public Teacher(String name) {
  50. this.name=name;
  51. }
  52. }

JVM

  1. public static void main(String[] args) throws Exception {
  2. //通过反射获取类的class对象
  3. Class c1 = Class.forName("com.dong.annotation.User");
  4. //获取构造方法
  5. Constructor[] constructors = c1.getConstructors();//非私有外所有构造方法
  6. Constructor constructor = c1.getConstructor(String.class,Integer.class,Integer.class);//指定构造方法
  7. Constructor[] declaredConstructors = c1.getDeclaredConstructors();//所有构造方法
  8. //获取普通方法
  9. Method[] methods = c1.getMethods();//非私有普通方法
  10. Method[] declaredMethods = c1.getDeclaredMethods();//所有普通方法
  11. Method declaredMethod = c1.getDeclaredMethod("study",String.class);//指定普通方法
  12. //获取属性Field
  13. Field[] fields = c1.getDeclaredFields();//全部属性
  14. c1.getFields();//非私有属性
  15. c1.getField("");//指定属性
  16. c1.getField("").get("");//属性名 --属性类型String.class --获取属性值
  17. }
  18. }
  1. //动态的创建对象,通过反射
  2. public class Test08 {
  3. public static void main(String[] args) throws Exception {
  4. //获得class对象
  5. Class c1 = Class.forName("com.dong.annotation.User");
  6. //构造一个对象
  7. User user=(User)c1.newInstance();//调用了类的无参构造器
  8. //通过构造器创建对象
  9. Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
  10. User user1 =(User) declaredConstructor.newInstance("赵东", 001, 18);
  11. //通过反射调用普通方法
  12. Method setName = c1.getDeclaredMethod("setName", String.class);
  13. setName.invoke(user,"赵东");//设置name参数
  14. //invoke 激活的意思
  15. //通过反射操作属性
  16. User user2=(User)c1.newInstance();
  17. Field name = c1.getDeclaredField("name");
  18. name.setAccessible(true);//设置是否取消私有检测
  19. name.set(user2,"赵东2");//设置name属性
  20. }
  21. }

反射操作泛型

通过反射获取注解信息

  1. public class Test10 {
  2. public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
  3. Class c1 = Class.forName("com.dong.annotation.Student2");
  4. //通过反射获得注解
  5. Annotation[] annotations = c1.getAnnotations();
  6. for (Annotation annotation : annotations) {
  7. System.out.println(annotation);
  8. }
  9. //获得注解的value值
  10. TableNote tableNote = (TableNote) c1.getAnnotation(TableNote.class);
  11. System.out.println(tableNote.value());//获得并打印注解的值
  12. //获得类指定的注解
  13. Field name = c1.getDeclaredField("name");//要获得的类型
  14. FieldNote annotation = name.getAnnotation(FieldNote.class);//获取值的方法
  15. System.out.println(annotation.columnName()+annotation.type()+annotation.length());
  16. }
  17. }
  18. @TableNote("db_student")
  19. class Student2{
  20. @FieldNote(columnName = "db_id",type = "int",length = 10)
  21. private int id;
  22. @FieldNote(columnName = "db_age",type = "int",length = 10)
  23. private int age;
  24. @FieldNote(columnName = "db_name",type = "String",length = 3)
  25. private String name;
  26. public Student2() {
  27. }
  28. public Student2(int id, int age, String name) {
  29. this.id = id;
  30. this.age = age;
  31. this.name = name;
  32. }
  33. public int getId() {
  34. return id;
  35. }
  36. public void setId(int id) {
  37. this.id = id;
  38. }
  39. public int getAge() {
  40. return age;
  41. }
  42. public void setAge(int age) {
  43. this.age = age;
  44. }
  45. public String getName() {
  46. return name;
  47. }
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51. @Override
  52. public String toString() {
  53. return "Student2{" +
  54. "id=" + id +
  55. ", age=" + age +
  56. ", name='" + name + '\'' +
  57. '}';
  58. }
  59. }
  60. //类注解
  61. @Target(ElementType.TYPE)
  62. @Retention(RetentionPolicy.RUNTIME)
  63. @interface TableNote{
  64. String value();
  65. }
  66. //属性的注解
  67. @Target(ElementType.FIELD)
  68. @Retention(RetentionPolicy.RUNTIME)
  69. @interface FieldNote{
  70. String columnName();
  71. String type();
  72. int length();
  73. }