`

Java Create you own Annotations and Using Them

 
阅读更多

如何创建一个Annotation呢?其实也是很简单的。

 

有几个关键词

    @Target(ElementType.PACKAGE), package header
    @Target(ElementType.TYPE), class header
    @Target(ElementType.CONSTRUCTOR), constructor header
    @Target(ElementType.METHOD), method header
    @Target(ElementType.FIELD), for class fields only
    @Target(ElementType.PARAMATER), for method parameters only
    @Target(ElementType.LOCAL_VARIABLE), for local variables only

 

@Retention – specifies the retention level of this annotation.

    RetentionPolicy.SOURCE—Retained only at the source level and will be ignored by the compiler
    RetentionPolicy.CLASS—Retained by the compiler at compile time, but will be ignored by the VM
    RetentionPolicy.RUNTIME—Retained by the VM so they can be read only at run-time 

 

@Documented – by default annotations are mentioned in java doc, this meta-annotation will make this annotation to be mentioned.

 

@Inherited – Indicates that the annotation will automatically be inherited (take a look at the example attached to this post). 

 

下面给一个完整的例子:

package com.java.chenhailong;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InvokeMultiple {
    int numberOfTimesToInvoke() default 1;
}

 

package com.java.chenhailong;

public class TestObject
{

    private String firstName;

    private String lastName;

    public TestObject(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @InvokeMultiple(numberOfTimesToInvoke = 3)
    public void printFirstName()
    {
        System.out.println(firstName);
    }

    @InvokeMultiple(numberOfTimesToInvoke = 6)
    public void printLastName()
    {
        System.out.println(lastName);
    }

    @InvokeMultiple
    public void printMessage()
    {
        System.out.println("printed only once");
    }

    public void printSecret()
    {
        System.out.println("this will not be printed");
    }
}

 

package com.java.chenhailong;

import java.lang.reflect.Method;

public class MainOther
{

    public static void main(String[] args)
    {
        TestObject obj = new TestObject("chen", "chen");
        invokeThis(obj);
    }

    public static void invokeThis(Object theObject)
    {
        try
        {
            Method[] methods = Class.forName(theObject.getClass().getName())
                    .getMethods();

            for (int i = 0; i < methods.length; i++)
            {
                InvokeMultiple invokeMultiple = methods[i]
                        .getAnnotation(InvokeMultiple.class);
                if (invokeMultiple != null)
                {
                    int numberOfTimesToInvoke = invokeMultiple
                            .numberOfTimesToInvoke();
                    for (int j = 0; j < numberOfTimesToInvoke; j++)
                    {
                        methods[i].invoke(theObject, null);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

 

0
1
分享到:
评论

相关推荐

    annotations-java8.jar

    annotations-java8.jar;annotations-java8.jar;annotations-java8.jar

    Pro.Java.8.Programming.3rd.Edition.1484206428

    By the end of the book, you’ll be fully prepared to take advantage of Java's ease of development, and able to create powerful, sophisticated Java applications. What youll learn How to use and ...

    allure-java-annotations-1.4.0.RC2.zip

    标题 "allure-java-annotations-1.4.0.RC2.zip" 提供的信息表明,这是一个与Allure报告相关的Java库的版本。Allure是一个强大的、灵活的测试报告工具,它提供美观的界面来展示测试结果,帮助开发者和测试人员更好地...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    By the end of the book, you’ll be fully prepared to take advantage of Java's ease of development, and able to create powerful, sophisticated Java applications. What youll learn How to use and ...

    annotations-api rt servlet-api.jar

    在Java开发领域,`annotations-api.jar`,`rt.jar`,以及`servlet-api.jar`都是极为重要的库文件,它们各自承载着不同的功能和用途。让我们深入了解一下这些组件以及它们在Java应用程序,特别是Web应用程序中的作用...

    Java Projects

    You’ll see how to use the Java runtime tools, understand the Java environment, and create a simple namesorting Java application. Further on, you’ll learn about advanced technologies that Java ...

    Hands-On Data Visualization with Bokeh pdf

    Learn how to create interactive and visually aesthetic plots using the Bokeh package in Python Key Features A step by step approach to creating interactive plots with Bokeh Go from nstallation all ...

    java-annotations,基于jvm的语言的注释。.zip

    标题"java-annotations"直接指出了我们讨论的主题——Java注释。这个主题涵盖了如何使用注释来增强代码的可读性、实现编译时或运行时的代码自省、以及利用注解驱动的开发模式。基于JVM的语言如Kotlin、Groovy等也...

    Java 9 Programming By Example

    You will be able to use the Java runtime tools, understand the Java environment, and create Java programs. We then cover more simple examples to build your foundation before diving to some complex ...

    swagger-annotations-1.6.2-API文档-中文版.zip

    标签:swagger、annotations、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心...

    audience-annotations-0.5.0-API文档-中英对照版.zip

    标签:apache、yetus、audience、annotations、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    Pro JPA 2 in Java EE 8: An In-Depth Guide to Java Persistence APIs.pdf

    After completing Pro JPA 2 in Java EE 8, you will have a full understanding of JPA and be able to successfully code applications using its annotations and APIs. The book also serves as an excellent ...

    graphql-java-annotations:Java的GraphQL注释

    GraphQL-Java批注 是一个很棒的库,但是它的语法有点冗长。... compile " io.github.graphql-java:graphql-java-annotations:8.3 " } (Maven语法) &lt; groupId&gt; io . github . graphql - java

    Java.9.Programming.By.Example.epub

    You will be able to use the Java runtime tools, understand the Java environment, and create Java programs. We then cover more simple examples to build your foundation before diving to some complex ...

    Java SE 8 for the Really Impatient

    The addition of lambda expressions (closures) and streams represents the biggest change to Java programming since the introduction of generics and annotations. Now, with Java SE 8 for the Really ...

    annotations-13.0-API文档-中文版.zip

    标签:jetbrains、annotations、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...

    annotations-23.0.0-API文档-中文版.zip

    标签:jetbrains、annotations、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...

    mastering-spring-cloud2018

    will cover such topics like creating REST API using Spring MVC annotations, providing API documentation using Swagger2, and exposing health checks and metrics using Spring Boot Actuator endpoints. ...

Global site tag (gtag.js) - Google Analytics