java註解,java註解原理

來源:互聯網
上載者:User

java註解,java註解原理

package com.ann.test;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.METHOD,ElementType.TYPE})// 範圍。注意首字母大寫/* * @Retention定義了該註解的生命週期 */@Retention(RetentionPolicy.RUNTIME)/* * @Inherited闡述了某個被標註的類型是被繼承的。可用於被註解類的子類 */@Inherited/* * 可以被例如javadoc此類的工具文檔化。Documented是一個標記註解,沒有成員。
* 注意:產生javadoc的時候,不能有任何中文注釋(英文注釋沒問題) */@Documentedpublic @interface Description { String value();}

建一個Person介面

package com.ann.test;public interface Person {    public void name();    public void age();    @Deprecated // 方法過時的註解    public void sing();    }
package com.ann.test;@Description("This is class annotation")public class Child implements Person {    @Override    @Description("This is method1 annotation")    public void name() {        // TODO Auto-generated method stub            }    @Override    @Description("This is method2 annotation")    public void age() {        // TODO Auto-generated method stub            }    @Override    @Description("This is method3 annotation")    public void sing() {        // TODO Auto-generated method stub            }    }

 

package com.ann.test;import java.lang.annotation.Annotation;import java.lang.reflect.Method;/** * 解析註解: *     通過反射擷取類、函數或成員上的runtime註解資訊,從而實現動態控製程序啟動並執行邏輯 * @author Administrator * */public class ParseAnn {    public static void main(String[] args) {        // 1. 使用類載入器載入類        try {            Class c = Class.forName("com.ann.test.Child");            // 2. 找到類上面的註解            c.isAnnotationPresent(Description.class);            boolean isExist = c.isAnnotationPresent(Description.class);            if (isExist) {                // 3. 拿到註解執行個體                Description d = (Description)c.getAnnotation(Description.class);                System.out.println(d.value());            } else {                System.out.println("not found class annotation");            }            // 4. 找到方法上的註解            Method[] ms = c.getMethods();//            for (Method m : ms) {//                boolean isMExist = m.isAnnotationPresent(Description.class);//                if (isMExist) {//                    Description d = (Description)m.getAnnotation(Description.class);//                    System.out.println(d.value());//                }//            }                        // 另外一種解析方法            for (Method m :ms) {                Annotation[] as = m.getAnnotations();                for(Annotation a : as) {                    if (a instanceof Description) {                        Description d = (Description)a;                        System.out.println(d.value());                    }                }            }        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

 

更詳細的解釋請看這位大神的部落格:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.