简体   繁体   English

Java批注中的类型层次结构

[英]Type hierarchy in java annotations

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"? 但是,我怎么能将我的注释AND,或OR归入一个“家庭”?

Please check Why it is not possible to extends annotations in java? 请检查为什么无法在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. 使用LogicalExpression作为父级。

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. LogicExpression声明为enum这样,您可以使用单个enum和各种Attributes集来执行条件。

eg 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 例如,如果要执行AND OR条件,则可以传递LogicExpression.ANDLogicExpression.OR并使用orAttributes()方法执行OR条件,并使用andAttributes()执行AND条件

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 {};..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM