-
注解的定义
注解通过@interface关键字来定义
public @interface TestAnnotation {} //创建了一个名字为TestAnnotation的注解 -
注解的应用
@TestAnnotation
public class Test {} //Test类定义时添加注解,
- 提供信息给编译器:编译器可以利用注解来探测错误和警告信息
- 编译阶段时的处理:软件工具可以利用注解信息来生成代码,html文档或其他的相应处理
- 运行阶段的处理:某些注解可以在程序运行时接受代码的提取
需要注意的是,注解并不是代码本身的一部分,其对于代码的运行效果没有直接的影响
- 元注解和标准注解
java中内置了几种标准注解及元注解,元注解的作用就是注解其他的注解(将注解想想成一个标签,那么元注解就是一张特殊的标签,用来解释其他普通的标签)
标准注解(定义在java.lang中):
@Test | 子类继承父类(实现接口),并且重写父类中的方法 |
---|---|
@Deprecated | 所修饰的方法可以被调用但是不建议使用 |
@SuppressWarnings | 关闭不正确的编译器警告信息 |
@AafeVarargs | 参数安全类型的注解,提醒开发者不要用参数做一些不安全的操作,它会阻止编译器产生unchecked这样的警告(jdk 1.7加入) |
@FunctionalInterface | 函数式接口注解(jdk 1.8的新特性),就是一个具有一个方法的普通接口,例如Runnable接口就被@FunctionalInterface注解,Runnable就是一个典型的函数式接口 |
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
元注解(定义在java.lang.annotation中)
@Target | 指定注解使用的地方,当一个注解被@Target注解时这个注解就被限定了运用的场景@Target有以下的ElementType参数1. CONSTRACTOR:构造器的声明2. FIELD:属性的声明(包括枚举类型)3. LOCAL_VARIABLE:局部变量的声明4. METHOD:对方法进行注解5. PACKAGE:对包进行注解6. PARAMETER:声明方法的参数7. TYPE:类、接口或枚举进行注解 |
---|---|
@Retention | 被@Retention注解的注解,其存活时间RetentionPolicy参数取值如下:1. SOURCE:只在源码阶段保留,在编译器编译阶段时,将被丢弃2. CLASS:只被保留到编译进行的时候,在class文件中可用,但不会被加载到JVM中3. RUNTIME :注解可以被保留到程序运行的时候,会被加载到JVM中,可以在运行时通过反射机制获取注解的信息 |
@Document | 将注解中的元素包含到javadoc中 |
@Inherited | 并不是注解本身可以被继承,是指如果一个父类被@Inherited注解过的注解进行注解的话,如果其子类没有被任何注解应用时,这个子类就继承了父类的注解 |
@Repeatable | jdk 1.8之后的新特性,注解的值可以取多个 |
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
}
@TestAnnotation
public void fun() {
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface TAnnotation{}
@TAnnotation
class Parent {
}
class Child extends Parent {
/**
* @TAnnotation被@Inherited注解
* Parent被@TAnnotation注解,
* Child继承自Parent,
* Child自然也拥有@TAnnotation这个注解
*/
}
/**
* @Repeatable注解了Person,@Repeatable后面括号中的类相当于一个容器注解
* 容器注解本身也是一个注解,用来存放其他注解
* 容器注解中必须要有一个value属性,属性类型是被@Repeatable注解过的注解数组
*/
@interface tables {
Table[] value();
}
@Repeatable(tables.class)
@interface Table{
String role() default "";
/**
* role【类似于】一个方法定义,编译器会对其进行类型检查
* role元素有一个默认值,如果在注解某个方法时没有给出role值,该注解的处理器会使用这个元素的默认值
*/
}
@Table(role="coder")
@Table(role="teacher")
public class Man{
}
/**
* 相当于table是一个个单独的标签
* 将多个table放到一个tables容器中
* 则tables中必须要有存放这些table标签的容器,即数组
* 当tables被贴到man身上,相当于给它贴上了多个标签
*/
- 注解的属性
注解的属性也叫成员变量,在注解的定义中以无参方法的形式来声明,方法名就是该成员变量的名字,返回值就是其类型,并且注解中仅有成员变量,没有方法。
赋值方式在注解的括号中以键值对的形式用逗号隔开
定义属性的类型:8中基本数据类型、class、enum、注解,及其数组;属性可以设置默认值,在括号后用default关键词指定
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface User{
int id();
String name() default "";
}
@User(id = 1, name = "tujia")
class Test {
}
===========================
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface User{
int id();
}
@User(id = 1, name = "tujia")
class Test {
}
- 注解与反射
注解通过反射获取
通过Class对象的isAnnotationPresent()方法判断它是否应用了某个注解
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return GenericDeclaration.super.isAnnotationPresent(annotationClass);
}
- getAnnotation()和getAnnotations()可以获取Annotation对象
//返回指定类型的注解
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
return (A) annotationData().annotations.get(annotationClass);
}
//返回该元素上的所有注解
public Annotation[] getAnnotations() {
return AnnotationParser.toArray(annotationData().annotations);
}
6、注解的提取与处理
下面的代码是java编程思想的栗子~读取PasswordUtils类,使用反射机制提取@UseCode标记
package com.gy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.plaf.basic.BasicPasswordFieldUI;
import sun.security.util.Password;
/**
* @Author: liuyang_5
* @Data:2019/12/3 14:43
*/
public class UseCaseTracker {
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase {
public int id() default -1;
public String description() default "";
}
public static void trackUseCases(List<Integer> useCases, Class<?> cl) {
for (Method m : cl.getDeclaredMethods()) {
UseCase uc = m.getAnnotation(UseCase.class);//该方法返回指定类型的注解对象
if (uc != null) {
System.out.println("Found Use Case: " + uc.id() + " " + uc.description());
useCases.remove(new Integer(uc.id()));
}
}
for (int i : useCases) {
System.out.println("Warning : Missing use case : " + i);
}
}
class PasswordUtils {
@UseCase(id = 47, description = "Passwords must contain at least one numeric")
public boolean validatePassword(String password) {
return (password.matches("\\w*\\d\\w*"));
}
@UseCase(id = 48)
public String encryptPassword(String password) {
return new StringBuilder(password).reverse().toString();
}
@UseCase(id = 49, description = "New passwords cannot equal previously used ones")
public boolean checkForNewPassword(List<String> prevPasswords, String password) {
return !prevPasswords.contains(password);
}
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 47, 48, 49, 50);
trackUseCases(list, PasswordUtils.class);
}
}