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

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

问题描述

我知道编译时注释功能,运行注释处理器并使用反射 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?

推荐答案

使用元注解 @RetentionRetentionPolicy.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();
}

运行时值名称本身表示,当保留值是运行时"时,此注释将在运行时在 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)
开发者名称:droy

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

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

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