Java Runtime注释如何在内部工作? [英] How does Java Runtime annotation work internally?

查看:105
本文介绍了Java Runtime注释如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道编译时注释功能,即运行注释处理器并使用反射API。但我不确定JVM如何获得有关运行时注释的通知。注释处理器也可以在这里工作吗?

I am aware of the compile time annotation functionality, that annotation processor is run and reflection API is used. But I am not sure how a JVM gets notified with respect to a runtime annotation. Does annotation processor come into work here too?

推荐答案

使用元注释 @Retention value RetentionPolicy.RUNTIME ,标记的注释由JVM保留,因此运行时环境可以使用它。这将允许注释信息(元数据)在运行时可用于检查。

Using the meta-annotation @Retention value RetentionPolicy.RUNTIME, the marked annotation is retained by the JVM so it can be used by the runtime environment. This will allow the annotation information (metadata) to be available for examination during runtime.

此示例声明:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
public @interface Developer {
    String value();
}

运行时
值名称本身表示当保留值为'Runtime'时,此注释将在运行时在JVM中可用。我们可以使用反射包编写自定义代码并解析注释。

Runtime The value name itself says that when the retention value is ‘Runtime’ this annotation will be available in JVM at runtime. We can write custom code using reflection package and parse the annotation.

如何使用?

package annotations;
import java.net.Socket;

public class MyClassImpl {

    @Developer("droy")
    public static void connect2Exchange(Socket sock) {
        // do something here
        System.out.println("Demonstration example for Runtime annotations");
    }
}

我们可以使用反射包来读取注释。当我们开发基于注释自动化某个过程的工具时,它非常有用。

We can use reflection package to read annotations. It is useful when we develop tools to automate a certain process based on annotation.

package annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class TestRuntimeAnnotation {
    public static void main(String args[]) throws SecurityException,
    ClassNotFoundException {
        for (Method method : Class.forName("annotations.MyClassImpl").getMethods()) {
            if(method.isAnnotationPresent(annotations.Developer.class)){
                try {
                    for (Annotation anno : method.getDeclaredAnnotations())  {
                        System.out.println("Annotation in Method '" + method + "' : " + anno);

                        Developer a = method.getAnnotation(Developer.class);
                        System.out.println("Developer Name:" + a.value());
                    }
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

输出

方法'public static void annotations.MyClassImpl.connect2Exchange(java.net.Socket)'中的注释:@ annotations.Developer(value = droy)< br>
开发者名称:droy

Output
Annotation in Method 'public static void annotations.MyClassImpl.connect2Exchange(java.net.Socket)' : @annotations.Developer(value=droy)
Developer Name:droy

这篇关于Java Runtime注释如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆