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”?
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.
But this will not help you with your second problem ie. use
LogicalExpressionas a Parent.But you can do something like below. Declare
LogicExpressionasenumThis way you can use singleenumand various set ofAttributesto execute conditions.e.g. If you want to execute
AND,ORcondition then you can passLogicExpression.AND,LogicExpression.ORand useorAttributes()method to executeORcondition andandAttributes()to executeANDcondition