天天看點

Think in Java -- Annotation

  在java SE5中,引入了Annotation的概念。引入Annotation,我們可以把一些metadata資訊也放在源碼檔案裡面,而不用放在另外一個檔案。例如在Spring中,我們可以使用annotation來實作很多的功能。用起來也是非常的友善。特别是我們在寫一些架構的時候,如果能自己定義一些Annotation,可以讓我們的架構變得更加的好用。

  •   Annotation的簡單文法
import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)

public @interface UseCase {
    public int id();
    public String description() default "no description";
}
           

這裡我們可以看到,其他Annotation的定義和interface的定義十分的相似,隻是多了一個@.在上面定義的過程中,我們發現我們使用了兩個Annotation。一個是Target,一個是Retention。Target的作用是表明,我們這個定義的UseCase Annotation是用在哪裡的。這裡我們建立的UseCase是用來修改Method的。ElementType還有其他類型,可以參考 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Retention.html。Retention是用來表用,我們建立的Annotation是在哪裡生效的。我們定義的Annotation是用來在運作時候用的,是以應該定義為RUNTIME。RetentionPolicy還有其他類型,可以參考 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Retention.html。

  • Annotation的使用
import java.util.*;

public 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, description = null)
    public String encryptPassword(String password) {
        return new StringBuilder(password).reverse().toString();
    }

    @UseCase(id = 49 , description = "New passwords can't equal previously used ones")
    public boolean checkForNewPassword(List<String>previousPasswords , String password) {
        return !previousPasswords.contains(password);
    }
}
           

我們使用上面定義的Annotation,來修飾PasswordUtils中的類。在這裡我們需要注意的是,UseCase中,有些值有有預設值的,我們可以不給他指派,如果沒有預設值的話,是一定需要指派的。 還有另外一點需要注意的,非primitive類型的資料,不能指派為null.*(其實primitive類型的本來就不能指派為null)

  • 處理Annotation

在上面,我們在PasswordUtils中使用了我們自己定義的Annotation。但是我們僅僅把Annotation加進去還是遠遠不夠的。Java中的一個預設的Annotation,如Override,Java中有專門的程式來處理這些Annotation。是以對于我們自己定義的Annotation,我們也需要自己定義程式來處理這些Annotation。

import java.lang.reflect.*;
import java.util.*;

public class UseCaseTracker {
    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);
        }
    }

    public static void main(String args[]) {
        List<Integer> useCases = new ArrayList<Integer>();
        Collections.addAll(useCases , 47 , 48 , 49 , 50);
        trackUseCases(useCases , PasswordUtils.class);
    }
}
           

上面的程式中,我們使用到了Java中的反射的一些内容。