注解与反射

Posted on 2020-06-06 16:27  codexiaochen  阅读(19)  评论(0)    收藏  举报

注解和反射

注解(Annotation)

1、什么是注解

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

2、内置注解

@Deprecated 过时

用于过时的类、方法、成员变量等

@Override

覆盖父类方法

@SuppressWarning

阻止警告

@FunctionaInterface

指定接口必须为函数式接口

@SafeVarargs

一直"堆污染警告"

3、元注解

通过@interface自定义注解

 import java.lang.annotation.*;
 
 public class Test01 {
     @myAnnotation
     public static void main(String[] args) {
 
    }
 }
 
 //Target表示我们的注解可作用在哪些地方
 @Target(value = ElementType.METHOD)
 //Retention表示我们的注解在什么地方有效
 //SOURCE < CLASS < RUNTIME
 @Retention(RetentionPolicy.RUNTIME)
 //Documented表示注解生存在JAVAdoc中
 @Documented
 //Inherited表示注解能被子类继承
 @Inherited
 @interface myAnnotation {
 
 }

 

反射

1、什么是反射

JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

2、或得class的几种方法

 package com.chen.reflection;
 
 public class Test01 {
     public static void main(String[] args) throws ClassNotFoundException {
         Person student = new Student();
         Person teacher = new Teacher();
 
         //1、通过对象或得
         Class c1 = student.getClass();
         System.out.println(c1.hashCode());
         //2、通过forname或得
         Class c2 = Class.forName("com.chen.reflection.Student");
         System.out.println(c2.hashCode());
         //3、通过类名.class或得
         Class c3 = Student.class;
         System.out.println(c3.hashCode());
         //4、基本内置类型的包装类都有一个Type属性
         Class c4 = Integer.TYPE;
         System.out.println(c4.hashCode());
    }
 }
 
 //实体类 pojo ebtity
 class Person{
     public String name;
 }
 
 class Student extends Person{
     public Student() {
         this.name = "学生";
    }
 }
 
 class Teacher extends Person{
     public Teacher() {
         this.name = "老师";
    }
 }

 

3、java.lang.reflect.Field 属性

 //Class.getFields() 或得public属性字段
         Field[] fields = c1.getFields();
         for (Field field : fields) {
             System.out.println(field);
        }
         //Class.getDeclaredFields() 或得public和private属性字段
         fields = c1.getDeclaredFields();
         for (Field field : fields) {
             System.out.println(field);
        }
         //Class.getDeclaredField() 或得指定属性字段
         Field name = c1.getDeclaredField("name");
         System.out.println(name);

 

4、java.lang.reflect.Methon 属性

 //Class.getMethods() 或得自己以及父类的所有public方法
         Method[] methods = c1.getMethods();
         for (Method method : methods) {
             System.out.println(method);
        }
         //Class.getDeclaredMethods() 或得自己的所有方法
         methods = c1.getDeclaredMethods();
         for (Method method : methods) {
             System.out.println(method);
        }
         //Class.getDeclaredMethod() 或得指定的方法
         Method getName = c1.getDeclaredMethod("getName",null);
         System.out.println(getName);

 

5、java.lang.reflect.Constructor 属性

 //Class.getConstructors() 或得public类型的构造函数
         Constructor[] constructors = c1.getConstructors();
         for (Constructor constructor : constructors) {
             System.out.println(constructor);
        }
         //Class.getDeclaredConstructors() 或得public和private类型的构造函数
         constructors = c1.getDeclaredConstructors();
         for (Constructor constructor : constructors) {
             System.out.println(constructor);
        }
         //Class.getConstructor() 或得指定构造函数
         Constructor constructor = c1.getConstructor(int.class, String.class, int.class);
         System.out.println(constructor);

 

6、反射或得泛型

 Method method = Test04.class.getMethod("test01", Map.class, List.class);
 
         Type[] genericParameterTypes = method.getGenericParameterTypes();
         for (Type genericParameterType : genericParameterTypes) {
             System.out.println("#" + genericParameterType);
             if (genericParameterType instanceof ParameterizedType){
                 Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                 for (Type actualTypeArgument : actualTypeArguments) {
                     System.out.println(actualTypeArgument);
                }
            }
        }
 
         method = Test04.class.getMethod("test02",null);
 
         Type genericReturnType = method.getGenericReturnType();
         System.out.println("#" + genericReturnType);
         if (genericReturnType instanceof ParameterizedType){
             Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
             for (Type actualTypeArgument : actualTypeArguments) {
                 System.out.println(actualTypeArgument);
            }
        }

 

7、反射或得注解

 //通过反射或得注解
 Annotation[] annotations = c1.getAnnotations();
 for (Annotation annotation : annotations) {
     System.out.println(annotation);
 }
 //或得注解的value值
 Tablechen tablechen = (Tablechen) c1.getAnnotation(Tablechen.class);
 String value = tablechen.value();
 System.out.println(value);
 //或得指定注解
 Field f = c1.getDeclaredField("name");
 Fieldchen fieldchen = f.getAnnotation(Fieldchen.class);
 System.out.println(fieldchen.columnName());
 System.out.println(fieldchen.type());
 System.out.println(fieldchen.length());

 

 

 

 

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3