java循环注解

发布于 2024-11-29 12:37:07 字数 225 浏览 1 评论 0原文

我想用注释创建树结构,

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
     String name();
     MyNode next() default null;
}

但编译器告诉它是循环,因此不允许。

我想知道为什么这是不允许的以及我怎样才能制作类似的东西?

I want to create tree structure with annotation

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
     String name();
     MyNode next() default null;
}

but compiler tells that it is cycle and hence it is not allowed.

I wonder why it is not allowed and how can I make something like it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有木有妳兜一样 2024-12-06 12:37:07

它不是树结构,只是线性列表。尝试使用数组来简化声明。

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
     String name();
}

并换行:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNodes {
     MyNode[] value();
}

现在,只需声明为数组:

@MyNodes({ 
    @MyNode(name = "name1"),
    @MyNode(name = "name2")
})
public class MyClass {
}

It is not a tree structure, just the linear list. Try to use array to simplify declaration.

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
     String name();
}

And wrap:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyNodes {
     MyNode[] value();
}

Now, just declare as array:

@MyNodes({ 
    @MyNode(name = "name1"),
    @MyNode(name = "name2")
})
public class MyClass {
}
书间行客 2024-12-06 12:37:07
  1. 注释是编译时常量。
  2. 注释成员只能是编译时常量(字符串、基元、枚举、注释、类文字)。
  3. 任何引用自身的内容都不能是常量,因此注释不能引用自身。

有趣的是:Java 语言规范中有一部分似乎与此相矛盾:

注释类型声明上的注释称为
元注释。注释类型可以用来注释它自己的
宣言。更一般地说,传递闭包中的循环
允许“注释”关系。例如,这是合法的
用另一个注释类型注释一个注释类型声明,
并用前一个类型注释后一个类型的声明。
(预定义的元注释类型包含几个这样的
循环。)
[来源]

但我收到编译错误对于 this (注释引用本身):

public @interface Funky {
    Funky funky();
}

和 this (两个注释相互引用):

public @interface Funky {
    Monkey monkey();
}
public @interface Monkey {
    Funky funky();
}
  1. Annotations are compile-time constants.
  2. Annotation members can only be compile-time constants (Strings, primitives, enums, annotations, class literals).
  3. Anything that references itself can't be a constant, so an annotation can't reference itself.

The funny thing is: there is a part of the Java Language specification that seems to contradict this:

An annotation on an annotation type declaration is known as a
meta-annotation. An annotation type may be used to annotate its own
declaration. More generally, circularities in the transitive closure
of the "annotates" relation are permitted. For example, it is legal to
annotate an annotation type declaration with another annotation type,
and to annotate the latter type's declaration with the former type.
(The pre-defined meta-annotation types contain several such
circularities.)
[Source]

But I get a compile error for both this (annotation references itself):

public @interface Funky {
    Funky funky();
}

and this (two annotations reference each other):

public @interface Funky {
    Monkey monkey();
}
public @interface Monkey {
    Funky funky();
}
一腔孤↑勇 2024-12-06 12:37:07

如果您确实需要此功能,您可能需要使用 Sun Java 5 编译器,该编译器不强制执行此限制。

但也许更好的解决方案是将节点存储在数组中,并让每个节点都有其子节点的索引。

If you really need this, you may want to use the Sun Java 5 compiler, where this restriction is not enforced.

But maybe a better solution is to store the nodes in an array and let each node have the index(es) of it's child node(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文