java注释中的类型层次结构 [英] Type hierarchy in java annotations

查看:30
本文介绍了java注释中的类型层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在注释中创建一些元数据结构时遇到问题.我们使用注解来定义休眠实体属性的特殊属性,但它可能在任何地方都可用.我想创建代表这些结构的条件:

attribute1 = ...或者(属性 2 = ...和属性 3 = ...)

问题是我需要使用此注释定义一些树"结构.这是我想要达到的一些设计:

@interface 属性 {......一些属性......}@接口逻辑表达式{}@interface OR 扩展 LogicalExpression {属性[] 属性() 默认{};LogicalExpression logicalExpressions() 默认{};}@interface AND 扩展 LogicalExpression {属性[] 属性() 默认{};LogicalExpression logicalExpressions() 默认{};}@interface ComposedCondition {属性[] 属性() 默认{};LogicalExpression logicalExpressions() 默认{};}

我想根据这个例子使用所有这些注释:

公共类表{@ComposedCondition(逻辑表达式 = {@OR(attributes = {@Attribute(...一些设置...)},逻辑表达式 = {@AND(attributes = {@Attribute(...), @Attribute(...)})})}私有字符串值;}

我知道我在上面的注释定义中定义的那种继承是不可能的.但是,我如何才能将我的注释 AND、OR 视为一个家庭"?

解决方案

请查看 为什么不能在java中扩展注解?

但是您可以创建可用于注释的元注释以创建注释组.

 @LogicalExpression@interface 或 {属性[] 属性() 默认{};LogicalExpression logicalExpressions() 默认{};}

但这不会帮助您解决第二个问题,即.使用 LogicalExpression 作为父级.

但是您可以执行以下操作.将 LogicExpression 声明为 enum 这样您就可以使用单个 enum 和各种 Attributes 来执行条件.>

例如如果你想执行 AND, OR 条件,那么你可以通过 LogicExpression.AND, LogicExpression.OR 并使用orAttributes() 方法执行OR 条件和andAttributes() 执行AND 条件

public enum LogicExpression {OR,AND,NOT;}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)公共@interface ComposedCondition {LogicExpression[] getExpressions() 默认{};属性[] orAttributes() 默认{};Attributes[] andAttributes() 默认{};...}

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.

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

这篇关于java注释中的类型层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆