反射与注解

反射(Reflection)

1、概述

  • Reflection (反射) 是Java被视为动态语言的关键,反射机制允许程序在执行期借
    助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及
    方法。.
    Class C= Class forNam(java lang. String")

  • 加载完类之后,在堆内存的方法区中就产生了一个Class类型的对象(一个类只有
    一个Class对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对
    象看到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,
    我们形象的称之为:反射

    正常方式: 引入需要的"包类”名称 --> 通过new实例化--->取 得实例化对象

    反射方式: 实例化对象----> getClass()方法 ----> 得 到完整的“包类”名称

2、Java反射优点和缺点

优点:

  • 可以实现动态创建对象和编译,体现出很大的灵活性

缺点:

  • 对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望
    做什么并且它满足我们的要求。这类操作总是慢于直接执行相同的操作。

3、获取Class类的实例

  • 若已知具体的类, 通过类的class属性获取,该方法最为安全可靠,程序性能最高。
    Class clazz = Person.class;
  • 已知某个类的实例, 调用该实例的getClass()方法获取Class对象
    Class clazz = person.getClass();
  • 已知一个类的全类名,且该类在类路径下,可通过Class类的静态方法forName()获取,
    可能抛出ClassNotFoundException
    Class clazz = Class.forName("demo01.Student");

4、获取运行时获取类的完整结构

通过反射获取运行时类的完整结构
Field、Method、Constructor、 Superclass、 Interface、 Annotation

public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {

        Class c1 = Class.forName("com.bm.reflection.User");

        //获取类的名字
        System.out.println(c1.getName()); //获取包名+类名
        System.out.println(c1.getSimpleName()); //获取类名

        System.out.println("====================================");

        //获取类的属性
        Field[] fields = c1.getFields(); //只能找到public属性
        for (Field f1 : fields) {
            System.out.println(f1);
        }
        Field[] declaredFields = c1.getDeclaredFields(); //找到所有的属性
        for (Field df1 : declaredFields) {
            System.out.println(df1);
        }

        //获取指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        System.out.println("========================================");

        //获取类的方法
        Method[] methods = c1.getMethods(); //获取本类及其父类全部public公共的方法
        for (Method method : methods) {
            System.out.println(method);
        }

        Method[] Dmethods = c1.getDeclaredMethods();  //获取本类所有方法
        for (Method Dmethod : Dmethods) {
            System.out.println(Dmethod);
        }

        //获取指定方法
        //第二个参数是为了区分重载
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(setName);

        System.out.println("=============================================");
        //获取指定的构造器
            //获取所有的public的构造器
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }

            //获取所有的构造器
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }

            //获取指定的构造器
        Constructor declaredConstructor2 = c1.getDeclaredConstructor(String.class, int.class);
        System.out.println(declaredConstructor2);

    }
}

5、使用反射来操作类

public class Test03 {
    public static void main(String[] args) throws Exception{

        //通过反射获取类
        Class c1 = Class.forName("com.bm.reflection.User");

        //构造一个对象
        User user = (User)c1.newInstance();  //本质上调用无参构造器
        System.out.println(user);

        //获取指定构造器
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
             //通过反射创建一个对象
        User user2 = (User)declaredConstructor.newInstance("jankin", 22);
        System.out.println(user2);

        
        
        System.out.println("=======================================");
        //通过反射来操作方法

        //通过反射创建一个对象(获取无参构造器)
        User user3 = (User)c1.newInstance();
        
        //通过反射获取一个方法
        Method setName = c1.getDeclaredMethod("setName", String.class);
        setName.invoke(user3,"jankin"); // invoke 调用,激活对象


        System.out.println("=======================================");
        //通过反射来操作属性

        
        //上面已经获取 c1 类了
        
        //通过反射创建一个对象
        User user4 = (User)c1.newInstance();
        //通过反射获取一个属性
        Field name = c1.getDeclaredField("name");

        //不能直接操作私有属性,我们需要关闭程序的安全检测,属性或者方法的 setAccessible(true)
        name.setAccessible(true);

        name.set(user4,"jankin"); // set 设置


    }
}

setAccessible

  • Method和Field、 Constructor对象都有setAccessible(方法。
  • setAccessible作用是启动和禁用访问安全检查的开关。
  • 参数值为true则指示反射的对象在使用时应该取消Java语言访问检查。
    • 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被调用,那么请设置为true.
    • 使得原本无法访问的私有成员也可以访问
  • 参数值为false则指示反射的对象应该实施Java语言访问检查

分析性能问题

执行10亿次 获取对象的方法

​ new 出的对象 9ms

​ 反射但未关闭安全检测 约 5700ms

​ 反射 之后关闭安全检测 约2000ms

注解(Annotation)

1、什么是注解

  • Annotation是从JDK5.0开始引入的新技术.

  • Annotation的作用:

    • 不是程序本身,可以对程序作出解释(这- -点和注释(comment)没什么区别)
    • 可以被其他程序(比如:编译器等)读取.
  • Annotation的格式:

    • 注解是以"@注释名"在代码中存在的,还可以添加一些参数值,

    • 例如:@SuppressWarnings(value="unchecked").

  • Annotation在哪里使用?

    • 可以附加在package , class , method , field 等上面,相当于给他们添加了额外的辅助信息,
      我们可以通过反射机制编程实现对这些元数据的访问

2、内置注解

  • @Override :定义在java.lang.Override中,此注释只适用于修辞方法,表示-个方法声明打算
    重写超类中的另-一个方法声明.
  • @Deprecated :定义在java.lang .Deprecated中,此注释可以用于修辞方法,属性,类,表示不.
    鼓励程序员使用这样的元素, 通常是因为它很危险或者存在更好的选择.
  • @SuppfessWarnings :定义在java.lang .SuppressWarnings中,用来抑制编译时的警告信息.
    与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的,只
    我们选择性的使用就好了.
    • @SuppressWarnings("all")
    • @SuppressWarnings("unchecked")
    • @SuppressWarnings(value={"unchecked" "deprecation")

3、元注解

  • 元注解的作用就是负责注解其他注解, Java定义了4个标准的meta-annotation类型他们被用来
    提供对其他annotation类型作说明.
  • 这些类型和它们所支持的类在java.lang.annotation包中可以找到.( @Target , @Retention,
    @Documented , @Inherited )
    • @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
    • @Retention :表示需要在什么级别保存该注释信息,用于描述注解的生命周期
      • (SOURCE < CLASS < RUNTIME)
    • @Document: 说明该注解将被包含在javadoc中
    • @Inherited:说明子类可以继承父类中的该注解
@MyAnnotation
public class Test01 {
    @MyAnnotation
    public void test(){

    }
}

//定义一个注解
//@Target: 表示我们的注解可以用在哪些地方。
@Target(value= {ElementType.METHOD,ElementType.TYPE})

//@Retention: 表示我们写的注解在什么地方有效
//runtime(运行时) 》 class (编译) 》 sources(源码)
@Retention(value= RetentionPolicy.RUNTIME)

//@Documented 表示是否将我们的注解生成在 JAVAdoc 中
@Documented

//@Inherited 子类以继承父类的注解
@Inherited
@interface MyAnnotation{

}

4、自定义注解

  • 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
  • 分析:
    • @interface用来声明一个 注解,格式: public @interface 注解名
    • 其中的每一个方法实际上是声明了一个配置参数.
    • 方法的名称就是参数的名称.
    • 返回值类型就是参数的类型(返回值只能是基本类型,Class , String , enum ).
    • 可以通过default来声明参数的默认值
    • 如果只有一个参数成员,一般参数名为value
    • 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值
//自定义注解
public class Test02 {

    //注解可以显式赋值,如果没有默认值,我们就必须给注解赋值
    @MyAnnotation2(name="jankin")
    public void Test(){}
}

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{

    //注解的参数: 参数类型 + 参数名()
    String name();
    int age() default 22;
    int id() default -1; //如果为-1,表示不存在

    String[] School() default "山大";
}

注解与反射结合

1、了解ORM

Object relationship Mapping -->对象关系映射

class Student{
    int id;
    String name;
    int age;
}
db_id db_name db_age
001 jankin 18
002 whwyz 22
  • 类和表结构对应
    • 属性和字段对应

    • 对象和记录对应

2、要求:利用注解和反射完成类和表结构的映射关系

public class Test05 {
    public static void main(String[] args) throws Exception{

        Class c1 = Class.forName("com.bm.reflection.Student1");

        //通过反射获取注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("1."+annotation);
        }

        //获取注解的value
        TableBm tableBm = (TableBm) c1.getAnnotation(TableBm.class);
        String value = tableBm.value();
        System.out.println(value);

        //获取类指定属性的注解
        Field name = c1.getDeclaredField("name");
        FieldBm annotation = name.getAnnotation(FieldBm.class);  //获取类属性的值
        System.out.println(annotation.columnName());
        System.out.println(annotation.length());
        System.out.println(annotation.type());

    }
}

@TableBm("db_student")
class Student1{

    @FieldBm(columnName = "db_id",type = "int",length = 10)
    private int id;
    @FieldBm(columnName = "db_name",type = "varchar",length = 10)
    private String name;
    @FieldBm(columnName = "db_age",type = "int",length = 10)
    private int age;

    public Student1() {
    }

    public Student1(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableBm{
    String value();   //表名
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldBm{
    String columnName();   //字段名
    String type();         //字段类型
    int length();          //字段长度
}
posted @ 2020-09-26 15:54  火烧云Z  阅读(185)  评论(0)    收藏  举报