5

I am facing problem with creating some metadata structure inside annotations. We are using annotations for define special attributes for hibernate entity attributes but it might be usable everywhere. I want to create condition which represent these structure:

attribute1 = ...
OR
  (attribute2 = ...
   AND
   attribute3 = ...)

Problem is that I need to define some "tree" structure using this annotations. Here is some design I want to reach:

@interface Attribute {
  ... some attributes ...
}

@interface LogicalExpression {
}

@interface OR extends LogicalExpression {
  Attribute[] attributes() default {};
  LogicalExpression logicalExpressions() default {};
}

@interface AND extends LogicalExpression {
  Attribute[] attributes() default {};
  LogicalExpression logicalExpressions() default {};
}

@interface ComposedCondition {
  Attribute[] attributes() default {};
  LogicalExpression logicalExpressions() default {};
}

All these annotation I want to use according to this example:

public class Table {

  @ComposedCondition(logicalExressions = {
    @OR(attributes = {@Attribute(... some settings ...)}, 
        logicalExpressions = {
          @AND(attributes = {@Attribute(...), @Attribute(...)})
        })
  }
  private String value;

}

I know that inheritance in that way I defined in the annotation definition above is not possible. But how can I consider my annotations AND, OR to be in one "family"?

Jan Stanicek
  • 1,201
  • 1
  • 14
  • 30

1 Answers1

5

Please check Why it is not possible to extends annotations in java?

But you can create meta annotations that can be used on annotation to create groups of annotations.

    @LogicalExpression
@interface OR {
    Attribute[] attributes() default {};
    LogicalExpression logicalExpressions() default {};
}

But this will not help you with your second problem ie. use LogicalExpression as a Parent.

But you can do something like below. Declare LogicExpression as enum This way you can use single enum and various set of Attributes to execute conditions.

e.g. If you want to execute AND, OR condition then you can pass LogicExpression.AND, LogicExpression.OR and use orAttributes() method to execute OR condition and andAttributes() to execute AND condition

public enum LogicExpression {
OR,AND,NOT;
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface ComposedCondition {
LogicExpression[] getExpressions() default {};
Attributes[] orAttributes() default {};
   Attributes[] andAttributes() default {};..
}
Community
  • 1
  • 1
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • This unfortunately does not help me. I need to define structure through some general LogicalExpression list but using specific AND, OR maybe NOT or other expressions. – Jan Stanicek Sep 06 '12 at 06:59
  • Updated the answer I think this definitely satisfies your purpose. – Amit Deshpande Sep 06 '12 at 07:33
  • It is nice idea, but still this solution has its own limits. But your solution brings me another idea - I may implement it in NODE form. Node will contain type of logical expression applied on current level and list of Attributes and list of children Nodes. – Jan Stanicek Sep 06 '12 at 07:58
  • Yes. I guess that will be also better solution. – Amit Deshpande Sep 06 '12 at 08:01
  • But now I realize that there occur recursive dependency on children nodes which causes: "Cycle detected: the annotation type Node cannot contain attributes of the annotation type itself". So this is still not final solution. I hope there will be some easy workaround – Jan Stanicek Sep 06 '12 at 08:37
  • Can you please show me what you have done? Answer your own question. – Amit Deshpande Sep 06 '12 at 08:38
  • No I didn't mean that I meant you can add another answer with your code. :) – Amit Deshpande Sep 06 '12 at 08:43
  • I started another thread, because it is different issue: [Java annotation recursive dependency](http://stackoverflow.com/questions/12296452/java-annotation-recursive-dependency) – Jan Stanicek Sep 06 '12 at 09:00