了解“注释"的使用 [英] Understanding the use of 'Annotations'

查看:23
本文介绍了了解“注释"的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更好地理解注释"的使用.我明白:

I'm trying to understand the use of 'Annotations' a bit better. I understand that:

为了更好地理解这一点,我创建了一个虚拟问题,如下所示:

To understand this better, I created a virtual problem as following:

Annotations TestAnnotation1TestAnnotation2TestAnnotation3(定义在后面的问题中可用).我想执行类 MethodsExecutorClass 的方法如下:

There are Annotations TestAnnotation1, TestAnnotation2, TestAnnotation3(definition is available latter in the question). I wants to execute the methods of class MethodsExecutorClass as following:

  • TestClass.java 编译后执行CommonMethod()RetentionPolicySOURCEMethod()
  • TestClass.class 加载后执行CommonMethod()RetentionPolicyCLASSMethod()
  • 每当 TestClass.javatestMethod() 方法执行时,然后执行 CommonMethod()RetentionPolicyRUNTIMEMethod()
  • When TestClass.java compiles then execute CommonMethod() and RetentionPolicySOURCEMethod()
  • When TestClass.class loads then execute CommonMethod() and RetentionPolicyCLASSMethod()
  • Whenever testMethod() method of TestClass.java executes then execute CommonMethod() and RetentionPolicyRUNTIMEMethod()

通过这个例子,我想了解以下内容:

  • 我能否指示 Java 编译器 (javac) 或 Java 运行时环境 (jvm) 执行我的类中的方法(例如 CommonMethod())code>RetentionPolicySOURCEMethod()方法MethodsExecutorClass`).
  • 我可以将监控(即搜索使用我的注释的方法/类等)委托给任何其他实体(在 Java SE 中可用).
  • 我想做一些类似 @Override@deprecated 注释的事情.我们不做额外的事情.尽管 在 Oracle javadoc 站点上,这里 很明显Java 平台一直有各种特殊的注释机制.@deprecated 就是其中之一.但我想知道我是否可以做这样的事情.
  • Can I instruct Java compiler (javac) or Java Runtime Environment (jvm) to execute a method in my class(e.g. CommonMethod()andRetentionPolicySOURCEMethod()methods ofMethodsExecutorClass`).
  • Can I delegate the monitoring (i.e. searching the methods/classes which are using my annotation etc.) to any other entity(which is available in Java SE).
  • I want to do something like @Override and @deprecated annotations. We don't do something extra. Although on Oracle javadoc site, here it is clearly mentioned that The Java platform has always had various ad hoc annotation mechanisms. and @deprecated is one of them. But I wondered If I can do something like this.

定义应如下所示:

MyAnnotations.java:

@Retention(RetentionPolicy.SOURCE)
public @interface TestAnnotation1 
{
  String className();
}

@Retention(RetentionPolicy.CLASS)
public @interface TestAnnotation2 
{
  String className();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation3 
{
  String className();
  String methodName();
}

MethodsExecutorClass.java:

class MethodsExecutorClass
{
    public static void CommonMethod()
    {
        System.out.println("In method: CommonMethod()");
    }

    public void RetentionPolicySOURCEMethod()
    {
        System.out.println("In method: RetentionPolicySOURCEMethod()");
        //Also print annotation arguments e.g. Class name etc
    }

    public void RetentionPolicyCLASSMethod()
    {
        System.out.println("In method: RetentionPolicyCLASSMethod()");
        //Also print annotation arguments e.g. Class name etc
    }

    public void RetentionPolicyRUNTIMEMethod()
    {
        System.out.println("In method: RetentionPolicyRUNTIMEMethod()");
        //Also print annotation arguments e.g. Class name etc
    }
}

TestClass.java:

@TestAnnotation1(TestClass.class)
@TestAnnotation2(TestClass.class)
class TestClass
{
    @TestAnnotation2(TestClass.class, "testMethod()")
    public void testMethod()
    {
        System.out.println("In method: testMethod()");
    }
} 

你能帮我实现这个目标吗?(请不要猜测或假设,但假设也会有帮助.
我不确定这是否可以实现,但很期待.

May you help me in achieving this? (Please no guess or assumptions, but presumptions would be also helpful).
I'm not sure if this can be achieve, but looking forward.

推荐答案

具有保留策略的注解 RetentionPolicy.SOURCE 仅在代码编译期间可用,因此您的编译器应支持您的注解以使用它,否则无法处理注释.通常,此类注解用于在编译时检测可能存在的问题,例如注解@Override.这就是为什么你的第一个问题不能以通常的方式实现.

Annotations with retention policy RetentionPolicy.SOURCE are only available during compilation time of the code so your compiler should support your annotation to use it, otherwise it's not possible to handle the annotation. Usually, such annotations are used to detect possible problems at compilation time, for example, annotation @Override. That's why your first problem can't be implemented in usual ways.

带有保留策略的注释 RetentionPolicy.CLASS 仅在 .class 文件中可用,并且可以通过 JVM 使用.请参阅此答案如何使用它.第二,您的问题也无法通过标准方式实现.

Annotations with retention policy RetentionPolicy.CLASS are available only in .class files and can be used via JVMs. Please see this answer how it can be used. The second your problem also can't be implemented via standard ways.

常用的注释带有保留策略 RetentionPolicy.RUNTIME,可通过 Java 中的反射机制.但是要解决您的第三个问题,您必须使用一些方法调用拦截器,例如,通过 Aspect Oriented Programming.之后,您可以通过 method.getDeclaredAnnotations().

Commonly used annotations are with retention policy RetentionPolicy.RUNTIME that are available via reflection mechanism in Java. But to solve your third problem you have to use some method invocation interceptors, for example, via Aspect Oriented Programming. After that you can get method's annotations via method.getDeclaredAnnotations().

我可以指导 Java 编译器 (javac) 或 Java 运行时环境 (jvm)在我的班级中执行一个方法(例如CommonMethod() 和 RetentionPolicySOURCEMethod() 方法ofMethodsExecutorClass`).

Can I instruct Java compiler (javac) or Java Runtime Environment (jvm) to execute a method in my class(e.g. CommonMethod()andRetentionPolicySOURCEMethod()methods ofMethodsExecutorClass`).

不,你不能.

我可以委托监控吗(即搜索方法/类使用我的注释等)到任何其他实体(这是在 Java SE 中可用).

Can I delegate the monitoring (i.e. searching the methods/classes which are using my annotation etc.) to any other entity(which is available in Java SE).

您可以通过 AOP 来实现,例如,使用库 AspectJ.

You can do it via AOP, for example, use the library AspectJ.

这篇关于了解“注释"的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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