有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有可能在注释中有条件地应用注释吗?

我试着做如下事情:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationOne {
    boolean annotationTwoToggler() default false

    {
        if (annotationTwoToggler) {
            // Apply annotation AnnotationTwo to calling type
        }
    }
}

我将如何使用这个示例:

@AnnotationOne(true) // Implies AnnotationTwo
public class MyClass { ... }

AnnotationTwo及其实现在我使用的API中,不能更改。我想让AnnotationTwo的所有实现在给定true参数的情况下触发AnnotationOne

我该怎么做


共 (0) 个答案