了解“注释"的用法 [英] Understanding the use of 'Annotations'
问题描述
我正在尝试更好地理解注释"的使用. 我了解:
I'm trying to understand the use of 'Annotations' a bit better. I understand that:
- 如何通过此完整教程访问示例中的代码在我的代码中.
- 我可以创建方法来执行所需的操作
- How to access annotations in my code for example via this complete tutorial.
- I can create methods to perform desired operations
为了更好地理解这一点,我创建了一个虚拟问题,如下所示:
To understand this better, I created a virtual problem as following:
有注释
TestAnnotation1
,TestAnnotation2
,TestAnnotation3
(定义在后面的问题中可用).我要执行MethodsExecutorClass
类的方法,如下所示:
There are Annotations
TestAnnotation1
,TestAnnotation2
,TestAnnotation3
(definition is available latter in the question). I wants to execute the methods of classMethodsExecutorClass
as following:
-
TestClass.java
编译后执行CommonMethod()
和RetentionPolicySOURCEMethod()
- 加载
TestClass.class
后,执行CommonMethod()
和RetentionPolicyCLASSMethod()
- 只要执行
TestClass.java
的testMethod()
方法,然后执行CommonMethod()
和RetentionPolicyRUNTIMEMethod()
- When
TestClass.java
compiles then executeCommonMethod()
andRetentionPolicySOURCEMethod()
- When
TestClass.class
loads then executeCommonMethod()
andRetentionPolicyCLASSMethod()
- Whenever
testMethod()
method ofTestClass.java
executes then executeCommonMethod()
andRetentionPolicyRUNTIMEMethod()
通过这个示例,我想了解以下内容:
- 我可以指示Java编译器(
javac
)或Java运行时环境(jvm
)在我的类中执行一个方法(例如CommonMethod()and
RetentionPolicySOURCEMethod()methods of
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()and
RetentionPolicySOURCEMethod()methods of
MethodsExecutorClass`). - 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中.但是要解决您的第三个问题,您必须使用一些方法调用拦截器,例如,通过面向方面的编程.之后,您可以通过
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屋!