手机版
你好,游客 登录 注册
背景:
阅读新闻

关于 Java注解

[日期:2018-09-05] 来源:Linux社区  作者:Linux [字体: ]

“注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释的代码本身的一部分。注解对于代码的运行效果没有直接影响。”--官方文档

元注解:

@Retention指定注解存活时间

@Documented

@Target指定注解运用的地方

@Inherited当子类没有注解时,父类注解可以被子类继承

@Repeatable Java1.8新增注解

注解的属性:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int id() default -1;
    String msg() default "";
}

定义注解的属性时,类型必须是8中基本类型、类、接口、注解及他们的数组

注解处理器就是通过反射机制获取被检查方法上的注解信息,然后根据注解元素的值进行特定的处理。
反射获取注解:

注解在类上

//判断类是否有对应TestAnnotation注解
boolean flag = class.isAnnotationPresent(MyAnnotation.class);
//获取对应注解
MyAnnotation myAnnotation = clazz.getAnnotation(MyAnnotation.class);

注解在成员变量上

Field a = class.getDeclaredField("a");
a.setAccessible(true);
//获取一个成员变量上的注解
MyAnnotation check = a.getAnnotation(MyAnnotation.class);

注解在方法上

Method testMethod = clazz.getDeclaredMethod("testMethod");
testMethod.isAnnotationPresent(MyAnnotation.class);
testMethod.getAnnotation(MyAnnotation.class);

下面记录一个使用注解进行测试的事例,main方法会检查指定注解的方法是否正确实现了排序算法

package test;

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

/**
 * Created by saishangmingzhu on 2018/8/29.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    int[] input() default {1,2,3,9,6,7,8,5,4};
    int[] output() default {9,8,7,6,5,4,3,2,1};
}

package test;

/**
 * Created by saishangmingzhu on 2018/8/29.
 */
public class Sort {

    /**
    * 冒泡
    * @param a
    * @return
    */
    @MyAnnotation(input = {1,2,3,9,6,7,8,5,4,11},output = {11,9,8,7,6,5,4,3,2,1})
    public int[] bubbleSort(int[] a){
        int length=a.length;
        for (int i=0;i<length;i++){
            for (int j=i+1;j<length;j++){
                if (a[j]>a[i]){
                    int b=a[i];
                    a[i]=a[j];
                    a[j]=b;
                }
            }
        }
        return a;
    }

    /**
    * 选择
    * @param a
    * @return
    */
    @MyAnnotation(input = {1,2,3,9,6,7,8,5,6,4,11},output = {11,9,8,7,6,6,5,4,3,2,1})
    public int[] selectionSort(int[] a){
        int length=a.length;
        for (int i=0;i<length;i++){
            int minIndex=i;
            for (int j=i+1;j<length;j++){
                if (a[minIndex]<a[j]){
                    minIndex=j;
                }
            }
            int b=a[minIndex];
            a[minIndex]=a[i];
            a[i]=b;
        }
        return a;
    }

    /**
    * 插入排序
    * @param a
    * @return
    */
    @MyAnnotation
    public int[] insertionSort(int[] a){
        int length=a.length;
        for (int i=1;i<length;i++){
            int tmp=a[i];
            int j=i-1;
            for (;j>=0&&tmp>a[j];j--){
                a[j+1]=a[j];
            }
            a[j+1]=tmp;
        }
        return a;
    }

    @MyAnnotation
    public int[] noSort(int[] a){
        return a;
    }
}

package test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by saishangmingzhu on 2018/8/29.
 */
public class TestSort {

    public static void main(String[] arg) throws InvocationTargetException, IllegalAccessException {
        Sort s=new Sort();
        Class sClass=s.getClass();
        //Method method=sClass.getDeclaredMethod("test",int[].class);
        Method[] methods=sClass.getDeclaredMethods();
        for (Method method:methods) {
            if (method.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
                int[] b = (int[]) method.invoke(s,myAnnotation.input());
                int[] out = myAnnotation.output();
                boolean flag = false;
                if (b.length == out.length) {
                    flag = true;
                    for (int i = 0; i < out.length; i++) {
                        if (b[i] != out[i]) {
                            flag = false;
                            break;
                        }
                    }
                }
                System.out.println(method.getName()+":"+flag);
                if (!flag){
                    for (int i:b){
                        System.out.print(i+",");
                    }
                    System.out.println();
                }
            }
        }
    }
}

ps:提一句,循环中最好提前获取数组长度赋值在新变量上,避免每次循环时重复计算获取影响效率。(《Effecive java》)

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-09/153892.htm

linux
相关资讯       Java注解 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款