Java注解与反射

Java注解与反射

1.注解 Java.Annotation

  • Annotation不是程序本身,可以对程序作出解释

  • 可以被其他程序读取

  • 可以通过反射机制编程实现对这些元数据的访问

  • 格式是"@注解名"

  • public class test {
        @Override
        public String toString() {
            return super.toString();
        }
    }
    

2.内置注解

  • @Override:表示一个方法声明打算重写超类中的另一个声明
  • @Deprecated:表示已经废弃,通常是因为它很危险或存在更好的选择
  • @SuppressWarnings:用来抑制编译时的警告信息,需要参数

3.元注解

负责注解其他注解的注解

  • @Target:用于描述注解的使用范围,即注解可以用在什么地方

  • @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期

    • SOURCE<CLASS<RUNTIME
  • @Documented:说明该注解将被包含在javadoc中

  • @Inherited:说明子类可以继承父类中的该注解

  • import java.lang.annotation.ElementType;
    import java.lang.annotation.Target;
    
    @MyAnnotation
    public class test {
        @MyAnnotation
        public void Test(){
    
        }
    }
    
    //定义一个注解
    @Target(value = {ElementType.TYPE,ElementType.METHOD})//Target 表示注解可以用在哪些地方
    @interface MyAnnotation{
    
    }
    

4.自定义注解

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class test {
    //注解可以显示赋值,如果没有默认值,就必须要给注解赋值
    @MyAnnotation(age=18,id=1,name="luffy")
    public void Test(){
    }
    @MyAnnotation1("")
    public void Test1(){
    }
}

//定义一个注解
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE,ElementType.METHOD})//Target 表示注解可以用在哪些地方
@interface MyAnnotation{
    //注解的参数:参数类型 参数名();
    String name() default "";
    int age() default 0;
    int id() default -1;
    String[] hobbies() default {"a","b"};
}

@interface MyAnnotation1{
    String value();//如果只有一个参数成员,一般参数名为value
}

5.反射Reflection

Class c= Class.forName(“java.lang.String”);

public class test {
    public static void main(String[] args) throws ClassNotFoundException {
        //通过反射获取类的Class对象
        //一个类在内存中只能有一个class对象
        Class c1=Class.forName("test");
        System.out.println(c1);
    }


}
//实体类:pojo/entity
class User{
    private String name;
    private int id;

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

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

得到class类的几种方法

public class test {
    public static void main(String[] args) throws ClassNotFoundException {
        Person p1=new Student();
        //得到class类的几种方法
        //方法一:通过对象获取
        Class c1=p1.getClass();
        System.out.println(c1.hashCode());
        //方法二:forname方式获取
        Class c2=Class.forName("Student");
        System.out.println(c2.hashCode());
        //方法三:通过类.class方式获取
        Class c3=Student.class;
        System.out.println(c3.hashCode());
        //以上三种方式得到的c1,c2,c3皆为同一个对象
        
        //获取父类类型
        Class c4=c1.getSuperclass();
        System.out.println(c4);
    }


}
//实体类:pojo/entity
class Person{

}
class Student extends Person{

}
class Teacher extends Person{

}

哪些类型可以有Class对象

class:外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类

interface:接口

[]:数组

enum:枚举

annotation:注解@interface

primitive type:基本数据类型

void

获取运行时类的完整结构

Field,Method,Constructor,Superclass,Interface,Annotation

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.FileHandler;

public class test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1=Class.forName("User");
        //获得类的名字
        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());
        //获得类的属性
        Field[] fields =c1.getFields();//只能找到public属性
        for (Field field : fields) {
            System.out.println(field);
        }
        Field[] declaredFields=c1.getDeclaredFields();//可以找到全部的属性
        for (Field field : declaredFields) {
            System.out.println(field);
        }
        //获取指定属性
        Field name=c1.getDeclaredField("name");
        System.out.println(name);
        //获取类的方法
        Method[] methods=c1.getMethods();//获取本类及父类的public方法
        for (Method method : methods) {
            System.out.println(method);
        }
        Method[] declaredMethods=c1.getDeclaredMethods();//获取本类的所有方法
        for (Method method : declaredMethods) {
            System.out.println(method);
        }
    }


}
//实体类:pojo/entity
class User{
    private String name;
    private int id;

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

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

反射机制示例及性能分析

public class test {
    //普通方法调用
    public static void test1(){
        User user=new User();
        long startTime=System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            user.getName();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("普通方法执行时间:"+(endTime-startTime)+"ms");
    }
    //反射方法调用
    public static void test2() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        User user=new User();
        Class c1=user.getClass();
        Method getName=c1.getDeclaredMethod("getName",null);
        long startTime=System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(user,null);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("反射方法执行时间:"+(endTime-startTime)+"ms");
    }
    //反射方法关闭检测
    public static void test3() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        User user=new User();
        Class c1=user.getClass();
        Method getName=c1.getDeclaredMethod("getName",null);
        getName.setAccessible(true);
        long startTime=System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(user,null);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("反射方法关闭检测执行时间:"+(endTime-startTime)+"ms");
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        test1();
        test2();
        test3();
    }
}

练习反射操作注解:ORM

import java.lang.annotation.*;
import java.lang.reflect.Field;

//练习反射操作注解
public class test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("Student");

        //通过反射获取注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获取注解的值
        DbTable dbTable = (DbTable) c1.getAnnotation(DbTable.class);
        String value = dbTable.value();
        System.out.println(value);

        //获取类指定的注解
        Field field = c1.getDeclaredField("id");
        DbField dbField = field.getAnnotation(DbField.class);
        System.out.println(dbField.columnName());
        System.out.println(dbField.type());
        System.out.println(dbField.length());
    }

}

//实体类表pojo
@DbTable("db_student")
class Student {
    @DbField(columnName = "db_id", type = "int", length = 10)
    private int id;
    @DbField(columnName = "db_name", type = "varchar", length = 10)
    private String name;
    @DbField(columnName = "db_age", type = "int", length = 10)
    private int age;

    public Student() {
    }

    public Student(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 "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

//表注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface DbTable {
    String value();
}

//属性注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface DbField {
    String columnName();

    String type();

    int length();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值