19.Java 注解

19.Java注解

1.Java内置注解----注解代码

@Deprecated                                    //不推荐使用的过时方法

@Deprecated
public void badMethod(){
    System.out.println("I am a old function");
}

@Override                                        //必须是覆盖父类(接口)的函数

@Override
public String toString(){
      return "override toString()";
}

@SuppressWarnings                          //关闭不恰当的编译期间警告

2.自定义注解

a.没有任何元素的注解---标记注解

b.普通注解定义、普通注解的使用

/**
 * 是否是类
 */
public @interface IsClass {
    String value();
    boolean isClass() default true;
}

 解释:该注解有两个元素value和isClass,isClass元素的目的是标记对象是否为类,该元素默认为真。

          当注解的元素都有默认值的时候,名为value的元素赋值时不用写名称

3.元注解 

  • @Target---------------用于描述注解的使用范围
      • eg:TYPE:用于描述类、接口(包括注解类型) 或enum声明
  • @Retention-----------用于描述注解的生命周期
      • SOURCE:在源文件中有效(即源文件保留)---编译时起作用
      • RUNTIME:在运行时有效(即运行时保留)----运行时起作用
      • CLASS:在class文件中有效(即class保留)[默认]
  • @Documented————被修饰的注解会被写到javadoc中(标记注解)
  • @Inherited------允许子类继承父类中的注解[注解自身时不可继承的]可以使用getAnnotations()反射获取父类被
  • @Target(ElementType.TYPE)
    @Documented
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface IsClass {
        String value();
        boolean isClass() default true;
    }

说明:元注解是用来修饰注解的注解,限定了注解的注释范围,注解的生命周期【源码级,运行时级别,Class文件级】,是否可以继承,是否创建文档等

4.运行时注解

像Spring一类的注解都与运行时注解有很大的关系,下面是一个利用注解,获取二维表的表名称,表的行名称,表的列值三个属性

4.1定义运行时注解

  • Table是二维表的表格名称
  • Column是二维表的列
  • Key是二维表的行
import java.lang.annotation.*;

/**
 * Created by yangyun on 2016/12/27.
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Key{
    String value();
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Table {
    String value();//表格的名称
}

 4.2使用注解并编写相关的注解信息获取类

  • Tables类中包含了我们所需要的信息,Table注解注解在Table类上,Key和Column注解在Field上
  • GetInformation类是获取相关的注解信息的操作类,利用的是Java的反射机制
import java.util.List;

/**
 * Created by yangyun on 2016/12/27.
 */
@Empty(field = "Not Empty")
@IsClass("value属性的值")
public class Annotation {
    public static void main(String[] args) {
        System.out.format("tableName:%s\n",GetInformation.getName(Tables.class));
        try {
            System.out.format("key:%s\n",GetInformation.getKey(Tables.class));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        System.out.format("columns:%s\n",GetInformation.getCols(Tables.class));
    }
}

@Table("students")
class GetInformation{
    public static String getName(Class<Tables> tablesClass){
        String strTableName=null;
        if(tablesClass.isAnnotationPresent(Table.class)){
            Table tableName=tablesClass.getAnnotation(Table.class);//表格名称
            strTableName=tableName.value();
        }
        return strTableName;
    }

    public static String getKey(Class<Tables> tablesClass) throws NoSuchFieldException {
        Field field=tablesClass.getDeclaredField("key");//获取private属性
        String strKey=null;
        if(field!=null){
            Key key=field.getAnnotation(Key.class);
            if(key!=null){
                strKey=key.value();
            }
        }
        return strKey;
    }

    public static List<String> getCols(Class<Tables> tablesClass){
        List<String> columns=new ArrayList<String>();
        Field[] fields=tablesClass.getDeclaredFields();
        if(fields!=null) {
            for (Field field : fields) {
                if (field.isAnnotationPresent(Column.class)) {
                    columns.add(field.getAnnotation(Column.class).value());
                }
            }
        }
        return columns;
    }
}

@Table("Students")
class Tables{
    @Key("Id")
    private String key;

    @Column("name")
    private String col1;

    @Column("sex")
    private String col2;
}

 4.3 主要的API介绍(不细说属于反射的知识)

  • 1.myClass.getAnnotations()---获取该类所有的注解 
  • 2.myClass.getAnnotation(Class annotationType)—获取特定注解类型
  • 3.myClass.isAnnotationPresent(Class annotationType)—判断是否存在特定类型的注解
  • 4.myClass.getDeclareAnnotations()— 获取该类的所有注解不包括从父类中继承的注解

 

参考文献

https://blog.zenfery.cc/archives/70.html

https://blog.zenfery.cc/archives/71.html

https://blog.zenfery.cc/archives/78.html

 

posted @ 2016-12-27 23:02  疯狂的肉包  阅读(186)  评论(0编辑  收藏  举报