• 2007-07-11

    温习Annotation - [Java]

    Tag:Java

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://navigating.blogbus.com/logs/6594525.html

    现在annotation正在改变着java developer的一些开发生活,各种框架都在充分应用annotation来简化开发,我们来看看annotation到底是怎么一回事?
    为什么引入annotation?
    由于在Java的一些实际开发中,已经有了一些annotation出现的雏形.比方说webservice使用一些tag自动生成一些接口和实现,EJB的deployment descriptor对象,transient修饰符代表不能被serialization,@deprecated等。那么他们的共用特性是什么呢?尽管这些数据不会直接对Java的语义产生影响,但是是通过工具、库或者运行时程序的语义来散发影响力的。以这种方式出现的新功能称之为annotation机制。所以Java是在增强Java语言支持易于开发的背景下提出annotation机制的,annotation对于已有的Java机制的影响力主要在于Java source files、Java class files和运行时的reflection;主要关联java classes,interfaces,methods和fields。(,或者能够能够被javac compiler或者其他的工具读取、或者作为配置项能够被存贮在class文件中、或者在运行时用Java reflection API检测的到。)
    虽然在第一天提出annotation这个特性,就一直在强调:annotion易于使用,但是应用开发者无须自己定义annotaion类型。但是不论是更好的使用现有的annotation类型,还是便于有朝一日自己确实需要扩展annotion类型,或者更好的理解开发模型,更好的理解程序的开发运行,都非常有必要了解一些annotation的。

    我们看一下Meta-annotations,看到底如何自己定义一个annnotation类型。Meta-annotations主要包括四个部分:
    Target
    Retention
    Documented
    Inherited
    Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
     Target(ElementType.TYPE)-can be applied to any element of a class
     Target(ElementType.FIELD)-can be applied to a field or property
     Target(ElementType.METHOD)-can be applied to a method level annotation
     Target(ElementType.PARAMETER)-can be applied to the parameters of a method
     Target(ElementType.CONSTRUCTOR)-can be applied to constructors
     Target(ElementType.LOCAL_VARIABLE)-can be applied to local variables
     Target(ElementType.ANNOTATION_TYPE)-indicates that the declared type itself is an annotation typ
    Retention用来指定annotation类型保留在哪儿和多长时间。
     RetentionPolicy.SOURCE-Annotations with this type will be by retained only at the source level and will be ignored by the compiler
     RetentionPolicy.CLASS-Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM
     RetentionPolicy.RUNTIME-Annotations with this type will be retained by the VM so they can be read only at run-time
    Documented用来指定这个annotation类型应当被javadoc工具文档化使用。
    Inherited用来指定使用这个类型的类是个继承来的。
    举一个简单的例子:
    测试用例:
    public class AnnotationTestableTeseCase {

        @Test
        public void go() throws SecurityException, ClassNotFoundException {
            int passed = 0, failed = 0;
            for (Method m : Class.forName(Foo.class.getName()).getMethods()) {
                if (m.isAnnotationPresent(Testable.class)) {
                    try {
                        m.invoke(null);
                        passed++;
                    } catch (Throwable ex) {

                        failed++;
                    }
                }
            }
            Assert.assertEquals(2, passed);
            Assert.assertEquals(2, failed);
        }
    }
    Annotation类型定义类:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Testable {
    }
    使用Testable类型的客户端代码:
    public class Foo {
        @Testable
        public static void m1() {
        }

        public static void m2() {
        }

        @Testable
        public static void m3() {
            throw new RuntimeException("Boom");
        }

        public static void m4() {
        }

        @Testable
        public static void m5() {
        }

        public static void m6() {
        }

        @Testable
        public static void m7() {
            throw new RuntimeException("Crash");
        }

        public static void m8() {
        }
    }
    Okey!
    参考:
    http://java.sun.com/developer/technicalArticles/releases/j2se15/
    http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
    http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html
    http://www.developer.com/java/other/article.php/10936_3556176_3

    历史上的今天:

    第一章节 2007-07-11

    随机文章:

    JNI初探 2008-04-01
    Java线程编程 2007-11-17

    收藏到:Del.icio.us




    引用地址:

    评论

  • Lots of people still cannot decide what to use – cash or credit cards. Credit cards give you convenience but cash gives you independence. When you apply for any type of credit card, be sure, you will carry some debt. However smart credit cardholders can use their plastic to advantage. Find your credit card with low rates and best rewards at

    <a href="http://cardits-insatnt.info/discover-percent-zero-life.html">zero percent for life discover card</a>
    <a href="http://cardite-instane.info/transfers-discover-balance.html">discover balance transfers</a>

    pxqP5ZVF1oZl6
  • The main reason banks get so much profit on credit cards is that most people are not able to analyze all the aspects of using credit cards. I hope this site will help

    <a href="http://immidetlycardes.info/discover-interest-lower-card3853016.html">discover card lower interest rate</a>
    <a href="http://inminutescardds.info/discover-interest-fixed-card3847669.html">discover card fixed interest</a>

    5Hn65HTx2t
  • 刚好用Annotation做了一个东西,你可以参考一下。

    http://dreamhead.blogbus.com/logs/7226338.html
    steven_xu回复dreamhead说:
    恭喜,XRuby已经完成了annotation的feature,你的这篇文章早已经读了,annotation这东西挺好玩,有很大的空间。
    2007-08-05 23:00:45