Search code examples
javarecursionannotations

Java annotation recursive dependency


I am trying to create some information tree structure inside annotations. After some tryings and helps (see Type hierarchy in java annotations) I moved to following model.

@interface Node {
  LogicalExpression logicalExpression() default LogicalExpression.AND;
  Attribute[] attributes() default {};
  Node[] nodes() default {};
}

This node should allow me define one level of condition tree. Value inside logicalExpression defines relation between children (attributes and another nodes). Problem is that annotation does not allow recursive dependency:

Cycle detected: the annotation type Node cannot contain attributes of the annotation type itself

Even if I put some NodeList annotation into Node and NodeList contain list of Nodes the cyclic dependency is recognized again.

@interface NodeList {
  Node[] nodes();
}

@interface Node {
  LogicalExpression logicalExpression() default LogicalExpression.AND;
  Attribute[] attributes() default {};
  NodeList nodes() default EmptyList;
}

Is there any solution on cyclic annotation definition?


Solution

  • This is because of this bug.

    And annotations' inheritance, polymorphism, 'cycle detected' limitations are... thread discuss about it.

    You can create Something like below

    @interface NodeInfo {
        LogicalExpression logicalExpression() default LogicalExpression.AND;
        Attribute[] attributes() default {};
    }
    
    
    @interface Node {
        NodeInfo[] nodes() default {};
    }