前言

反射和注解在java中偏高级用法,一般在各种框架中被广泛应用,文章简单介绍下反射和注解的用法,希望对你的工作学习有一定帮助

java注解

什么是注解

java 注解也就是annotation是从 java5 开始引入的新技术

annotation的作用:

  • 不是程序本身,可以对程序作出解释
  • 可以被其他程序(编译器等)读取

annotation的格式:

  • 注解以@注释名在代码中存在的,可以添加一些数值,例如suppresswarnings(value=”unchecked”)

annotation在里使用?

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

元注解

元注解的作用就是负责注解其他注解,java定义了4个标准的meta-annotation类型,被用来提供对其他annotation类型作说明
这些类型和它们所支持的类在java.lang.annotation包中可以找到(@target,@retention,@documented,@inherited)

  • @target:用于描述使用范围(注解在什么地方使用)
  • @retetion:表示需要在什么级别保证该注释信息,用于描述注解的生命周期(source<class<runtime)
  • @document:英文意思是文档。它的作用是能够将注解中的元素包含到 javadoc 中去。
  • @inherited:注解了的注解修饰了一个父类,如果他的子类没有被其他注解修饰,则它的子类也继承了父类的注解

自定义注解

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

public class test03 {
    //注解可以显示赋值,如果没有默认值,一定要给注解赋值
    @myannotation2(name = "aj",schloos = {"机电学院"})
    public void test(){

    }

    @myannotation3("")
    public void test2(){

    }
}

@target({elementtype.method,elementtype.type})
@retention(retentionpolicy.runtime)
@interface myannotation2{
    // 注解的参数,参数类型+参数名
    string name() default  "";

    int age() default  0;

    //如果默认值为-1 代表不存在
    int id() default -1;

    string[] schloos() ;
}

@target({elementtype.method,elementtype.type})
@retention(retentionpolicy.runtime)
@interface  myannotation3{

    string value();
}

给代码加注解其实就是这么多,关键还是我们如何去读取注解,这就需要用到反射,下面重点介绍java反射

java反射

反射是java被视为动态语言的关键,反射机制允许程序在执行期借助reflection api取得任何类的内部信息,并能直接操作任意对象内部熟悉及方法

class c = class.forname("java.lang.string")

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

class类

对于每个类而言,jre都为其保留一个不变的class类型的对象,一个class对象包含了特定某个结构的有关信息。

  • class本身也是一个类
  • class对象只能由系统建立对象
  • 一个加载的类在jvm中只会有一个class实例
  • 一个class对象对应的是一个加载到jvm中的一个.class文件
  • 每个类的实例都会记得自己是由哪个class实例生成的
  • 通过class可以完整的得到一个类中的所有被加载的结构
  • class类是reflection的根源,针对任何你想动态加载、运行的类,唯有先获得相应的class对象

class类的常用方法

反射获取对象

public class test02 {
    public static void main(string[] args) throws classnotfoundexception {
        person person = new student();
        system.out.println("这个人是"+person.name);

        //通过对象获取
        class c1 = person.getclass();
        system.out.println(c1.hashcode());

        //通过forname获取
        class c2 = class.forname("reflection.student");
        system.out.println(c2.hashcode());

        //通过类名获取
        class c3 = student.class;
        system.out.println(c3.hashcode());

        //获得父类类型
        class c4 = c1.getsuperclass();
        system.out.println(c4);
    }
}

 

@data
class person{
    public string name;
    public int age;
}


class student extends  person{
    public student(){
        this.name = "学生";
    }
}

class teacher extends  person{
    public teacher(){
        this.name = "老师";
    }
}

反射操作方法、属性

public class test03 {
    public static void main(string[] args) throws classnotfoundexception, illegalaccessexception, instantiationexception, nosuchmethodexception, invocationtargetexception, nosuchfieldexception {
        class c1 = class.forname("reflection.student");

        student student = (student) c1.newinstance();
        system.out.println(student.getname());

        // 通过反射操作方法
        method setname = c1.getdeclaredmethod("setname", string.class);
        setname.invoke(student, "zhangshan");
        system.out.println(student.getname());


        student student1 = (student) c1.newinstance();
        field name = c1.getdeclaredfield("name");
        //反射不能直接操作私有属性,需要手动关掉程序的安全检测,setaccessible(true)
        name.setaccessible(true);
        name.set(student1,"lisi");
        system.out.println(student1.getname());

    }
}

性能检测

public class test04 {

    public static void test01(){
        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 test02() throws nosuchmethodexception, invocationtargetexception, illegalaccessexception {
        user user = new user();
        long starttime = system.currenttimemillis();

        class c1 = user.getclass();
        method getname = c1.getdeclaredmethod("getname", null);

        for (int i = 0; i <1000000000 ; i++) {
            getname.invoke(user, null);
        }
        long endtime = system.currenttimemillis();

        system.out.println(endtime - starttime +"ms");
    }


    public static void test03() throws nosuchmethodexception, invocationtargetexception, illegalaccessexception {
        user user = new user();
        long starttime = system.currenttimemillis();

        class c1 = user.getclass();
        method getname = c1.getdeclaredmethod("getname", null);
        getname.setaccessible(true);

        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 nosuchmethodexception, illegalaccessexception, invocationtargetexception {
        test01();
        test02();
        test03();
    }
}

反射操作注解

public class test05 {

    public static void main(string[] args) throws classnotfoundexception, nosuchfieldexception {
        class<?> c1 = class.forname("reflection.customer");

        // 通过反射获取注解
        annotation[] annotations = c1.getannotations();
        for (annotation annotation:annotations){
            system.out.println(annotation);
        }

        // 获取注解的值
        tableannotation annotation = c1.getannotation(tableannotation.class);
        system.out.println(annotation.value());

        //获取类指定注解
        field id = c1.getdeclaredfield("id");
        filedannotation annotation1 = id.getannotation(filedannotation.class);
        system.out.println(annotation1.columnname());
        system.out.println(annotation1.length());
        system.out.println(annotation1.type());


    }
}


//类注解
@target(elementtype.type)
@retention(retentionpolicy.runtime)
@interface tableannotation{
    string value();
}


@data
@tableannotation("db_customer")
class customer {

    @filedannotation(columnname="id",type = "long",length =10)
    private long id;

    @filedannotation(columnname="age",type = "int",length =10)
    private int age;

    @filedannotation(columnname="name",type = "string",length =10)
    private string name;


}


//方法注解
@target(elementtype.field)
@retention(retentionpolicy.runtime)
@interface filedannotation{
    string columnname();

    string type();

    int length();
}

总结

到此这篇关于java高级用法之注解和反射的文章就介绍到这了,更多相关java注解和反射内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!