实施注释的用例
问题内容:
什么是实现注释的有效用例?
在设计主要基于注释的配置系统时,我有时需要创建实现代码生成或程序配置的注释的类。
另一种选择是将批注中包含的数据镜像到DTO中,这似乎是一项开销。
这是一个例子:
public enum IDType {
LOCAL,
URI,
RESOURCE;
}
@Documented
@Target( { METHOD, FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Id {
/**
* @return
*/
IDType value() default IDType.LOCAL;
}
与实施
public class IdImpl implements Id{
private final IDType idType;
public IdImpl(IDType idType){
this.idType = idType;
}
@Override
public IDType value() {
return idType;
}
@Override
public Class<? extends Annotation> annotationType() {
return Id.class;
}
}
我为此收到编译器警告,但是对于许多用例来说,它似乎是一个有效的工具。
上面示例的警告是
注释类型Id不应用作IdImpl的超级接口
编辑:
我刚刚从Guice找到了这个例子:
bind(CreditCardProcessor.class)
.annotatedWith(Names.named("Checkout"))
.to(CheckoutCreditCardProcessor.class);
有谁知道为什么存在此限制或有其他用例的信息?
问题答案:
我从没有在实践中使用过它,但是得到的是,您可以使用类代替注释。
让我们创建一个人工的例子。假设我们有一个文档生成器。它@Docu
从给定的类中读取注释并打印description
属性。像这样:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
public class DokuGenerator {
public static void main(String[] args) throws Exception {
new DokuGenerator(StaticClass.class, StaticClass2.class);
}
public DokuGenerator(Class<?>... classesToDokument) throws Exception {
List<Docu> documentAnnotations = getDocumentAnnotations(classesToDokument);
printDocumentation(documentAnnotations);
}
private List<Docu> getDocumentAnnotations(Class<?>... classesToDokument)
throws Exception {
List<Docu> result = new ArrayList<Docu>();
for (Class<?> c : classesToDokument)
if (c.isAnnotationPresent(Docu.class))
result.add(c.getAnnotation(Docu.class));
return result;
}
private void printDocumentation(List<Docu> toDocument) {
for (Docu m : toDocument)
System.out.println(m.description());
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Docu {
String description();
}
@Docu(description = "This is a static class!")
class StaticClass {
}
@Docu(description = "This is another static class!")
class StaticClass2 {
}
印刷品:
This is a static class!
This is another static class!
我们现在想要完成的是,一个类不仅可以被静态注释,而且可以将运行时信息添加到文档中。@Docu
大多数时候我们很高兴使用注释,但是在某些特殊情况下,我们需要特殊的文档。我们可能想为某些方法添加性能文档。我们可以通过让一个类实现注释来做到这一点。生成器首先检查注释,如果不存在,则检查类是否实现了注释。如果是这样,它将类添加到注释列表中。
像这样(生成器中只有两行代码):
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DokuGenerator {
public static void main(String[] args) throws Exception {
new DokuGenerator(StaticClass.class, StaticClass2.class,
DynamicClass.class);
}
public DokuGenerator(Class<?>... classesToDokument) throws Exception {
List<Docu> documentAnnotations = getDocumentAnnotations(classesToDokument);
printDocumentation(documentAnnotations);
}
private List<Docu> getDocumentAnnotations(Class<?>... classesToDokument)
throws Exception {
List<Docu> result = new ArrayList<Docu>();
for (Class<?> c : classesToDokument)
if (c.isAnnotationPresent(Docu.class))
result.add(c.getAnnotation(Docu.class));
else if (Arrays.asList(c.getInterfaces()).contains(Docu.class))
result.add((Docu) c.newInstance());
return result;
}
private void printDocumentation(List<Docu> toDocument) {
for (Docu m : toDocument)
System.out.println(m.description());
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Docu {
String description();
}
@Docu(description = "This is a static class!")
class StaticClass {
}
@Docu(description = "This is another static class!")
class StaticClass2 {
}
class DynamicClass implements Docu {
public DynamicClass() {
try {
Thread.sleep((long) (Math.random() * 100));
} catch (InterruptedException e) {
// ignore exception to make debugging a little harder
}
}
@Override
public String description() {
long millis = System.currentTimeMillis();
new DynamicClass();
millis = System.currentTimeMillis() - millis;
return "This is a dynamic class. I run on "
+ System.getProperty("os.name")
+ ". The construction of an instance of this class run for "
+ millis + " milliseconds.";
}
@Override
public Class<? extends Annotation> annotationType() {
return Docu.class;
}
}
输出为:
This is a static class!
This is another static class!
This is a dynamic class. I run on Windows XP. The construction of an instance of this class run for 47 milliseconds.
您没有太多更改代码生成器,因为您可以使用该类代替注释。
其他示例谁可能是使用注释或XML作为配置的框架。您可能有一个处理注释的处理器。如果使用XML作为配置,则可以生成实现注释的类的实例,并且您的处理器可以对它们进行处理,而无需进行任何更改!(当然,还有其他方法可以达到相同的效果,但这是一种实现方法)