[英]How to use annotation in an annotation?
鉴于:
public @interface MyAnnotation(){
public SomeType[] value();
}
在Java 7中,可以执行以下操作:
@MyAnnotation({
value1,
@MyAnnotation({subValue1, subvalue2, ...}) value2,
...
valueN
})
public Object someProperty;
?
您可以。 这是杰克逊图书馆的一个例子(省略了注释):
package com.fasterxml.jackson.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.FIELD,
ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonSubTypes {
public Type[] value();
public @interface Type {
/**
* Class of the subtype
*/
public Class<?> value();
/**
* Logical type name used as the type identifier for the class
*/
public String name() default "";
}
}
这是一个示例用法:
@JsonSubTypes({
@JsonSubTypes.Type(value = TestSystemEvent.class, name = "TestEvent"),
})
public interface SystemEvent {
}
如何在注释中使用注释?
也许是这样
public @interface MyAnnotation(){
public SomeType[] value();
public MyAnnotation[] refine();
}
@MyAnnotation(
{value1, value2},
refine={ @MyAnnotation({valueX, valueY}), @MyAnnotation(valueM) }
)
public Object someProperty;
同样,在Java 8中,您可以具有Repeatable批注-因此,您可以细化或添加由同一批注的后续重复带来的其他“细化”(例如第一个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.