<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    七段

    無(wú)論怎樣,請(qǐng)讓我先感謝一下國(guó)家。

    BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
      35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks
    Pros and Cons:

    Pros:

    1, reduce configuration xml files

    2, readable by self-documenting

    Cons:

    1, it adds deployment context to classes, which should be generic enough.

    2, interfere with design principles such as IOC and dependency injection, because you need to introduce imports

    Usage (annotation works only when handled by related annotation processor):

    • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
    • Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
    • Runtime processing — Some annotations are available to be examined at runtime.

      1 import java.io.IOException;
      2 import java.io.PrintStream;
      3 import java.lang.reflect.AnnotatedElement;
      4 import java.lang.annotation.Annotation;
      5 
      6 import java.util.Date;
      7 import java.lang.annotation.Documented;
      8 import java.lang.annotation.Inherited;
      9 import java.lang.annotation.Retention;
     10 import java.lang.annotation.RetentionPolicy;
     11 
     12 public class ReflectionTester {
     13 
     14   public ReflectionTester() {
     15   }
     16 
     17   public void testAnnotationPresent(PrintStream out) throws IOException {
     18     Class c = Super.class;
     19     boolean inProgress = c.isAnnotationPresent(InProgress.class);
     20     if (inProgress) {
     21       out.println("Super is In Progress");
     22     } else {
     23       out.println("Super is not In Progress");
     24     }
     25   }
     26 
     27   public void testInheritedAnnotation(PrintStream out) throws IOException {
     28     Class c = Sub.class;
     29     boolean inProgress = c.isAnnotationPresent(InProgress.class);
     30     if (inProgress) {
     31       out.println("Sub is In Progress");
     32     } else {
     33       out.println("Sub is not In Progress");
     34     }
     35   }
     36 
     37   public void testGetAnnotation(PrintStream out) 
     38     throws IOException, NoSuchMethodException {
     39 
     40     Class c = AnnotationTester.class;
     41     AnnotatedElement element = c.getMethod("calculateInterest"
     42                                   float.classfloat.class);
     43 
     44     GroupTODO groupTodo = element.getAnnotation(GroupTODO.class);
     45     String assignedTo = groupTodo.assignedTo();
     46 
     47     out.println("TODO Item on Annotation Tester is assigned to: '" + 
     48         assignedTo + "'");
     49   }
     50 
     51   public void printAnnotations(AnnotatedElement e, PrintStream out)
     52     throws IOException {
     53 
     54     out.printf("Printing annotations for '%s'%n%n", e.toString());
     55 
     56     Annotation[] annotations = e.getAnnotations();
     57     for (Annotation a : annotations) {
     58       out.printf("    * Annotation '%s' found%n"
     59         a.annotationType().getName());
     60     }
     61   }
     62 
     63   public static void main(String[] args) {
     64     try {
     65       ReflectionTester tester = new ReflectionTester();
     66 
     67       tester.testAnnotationPresent(System.out);
     68       tester.testInheritedAnnotation(System.out);
     69 
     70       tester.testGetAnnotation(System.out);
     71 
     72       Class c = AnnotationTester.class;
     73       AnnotatedElement element = c.getMethod("calculateInterest"
     74                                     float.classfloat.class);      
     75       tester.printAnnotations(element, System.out);
     76     } catch (Exception e) {
     77       e.printStackTrace();
     78     } 
     79   }
     80 }
     81 
     82 class Sub extends Super {
     83 
     84   public void print(PrintStream out) throws IOException {
     85     out.println("Sub printing");
     86   }
     87 }
     88 
     89 @InProgress class Super {
     90 
     91   public void print(PrintStream out) throws IOException {
     92     out.println("Super printing");
     93   }
     94 }
     95 
     96 @Documented
     97 @Retention(RetentionPolicy.RUNTIME)
     98 @interface GroupTODO {
     99 
    100   public enum Severity { CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION };
    101 
    102   Severity severity() default Severity.IMPORTANT;
    103   String item();
    104   String assignedTo();
    105   String dateAssigned();
    106 }
    107 
    108 /**
    109  * Marker annotation to indicate that a method or class
    110  *   is still in progress.
    111  */
    112 @Documented
    113 @Inherited
    114 @Retention(RetentionPolicy.RUNTIME)
    115 @interface InProgress { }
    116 
    117 class AnnotationTester {
    118 
    119   @InProgress
    120   @GroupTODO(
    121     severity=GroupTODO.Severity.CRITICAL,
    122     item="Figure out the amount of interest per month",
    123     assignedTo="Brett McLaughlin",
    124     dateAssigned="04-26-2004"
    125   )
    126   public void calculateInterest(float amount, float rate) {
    127     // Need to finish this method later
    128   }
    129 }
    130 
    posted on 2010-01-04 11:28 sevenduan 閱讀(1658) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
    主站蜘蛛池模板: 亚洲va中文字幕无码久久| 可以免费看的卡一卡二| 亚洲国产精品人人做人人爽| 亚洲高清一区二区三区电影| 国产va免费精品观看精品| 亚洲三级视频在线观看| 91短视频免费在线观看| 亚洲午夜成激人情在线影院| 在人线av无码免费高潮喷水| 中文字幕 亚洲 有码 在线| 久久久高清免费视频| 亚洲乱理伦片在线观看中字| 国产成人免费全部网站| 亚洲国产精品成人综合色在线| 女人18毛片水最多免费观看| 亚洲av最新在线观看网址| 一本久久综合亚洲鲁鲁五月天| 免费国产综合视频在线看| 日日狠狠久久偷偷色综合免费| 亚洲人成国产精品无码| 女同免费毛片在线播放| 久久亚洲国产成人精品性色| 4hu四虎最新免费地址| 亚洲国产成人AV在线播放| 亚洲av无码国产精品色在线看不卡| 国产日韩AV免费无码一区二区三区 | 亚洲AV无码成人精品区日韩| 97性无码区免费| 亚洲国产一区在线观看| 麻豆国产入口在线观看免费| 欧洲乱码伦视频免费国产| 亚洲精品自产拍在线观看| 91在线亚洲精品专区| 在线观看无码AV网站永久免费| 边摸边吃奶边做爽免费视频网站| 亚洲动漫精品无码av天堂| 久久WWW免费人成人片| www一区二区www免费| 亚洲一级视频在线观看| 亚洲国产成人久久笫一页| 久久午夜夜伦鲁鲁片无码免费|