简体   繁体   中英

syntax for passing array to annotation

A few days ago I was forced to use the following construction for my class declaration:

@Table(name="UserPattern",  uniqueConstraints={
   @UniqueConstraint(columnNames={"user_id", "patern_id"})
})

I was very surprised by this syntax.

Usually I thought that if I should to pass array to annotation OI should write the following:

declared_inside_annotation_name={value1,value2...}

but in this case it looks like the following:

uniqueConstraints={
                    @UniqueConstraint(columnNames={"user_id", "patern_id"})
                  }

@Table annotation declaration:

@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {

    String name() default "";


    String catalog() default "";


    String schema() default "";


    UniqueConstraint[] uniqueConstraints() default { };


    Index[] indexes() default {};
}

please clarify this syntax.

There is actually not a conflict between your expected syntax from your declared_inside_annotation_name example and the syntax from the @Table annotation. The type of elements for an array property of an annotation does not necessarily have to be a string (which might be what you have expected). It may actually be another annotation.

This is the case with the uniqueConstraints property of the @Table annotation. If you check the declaration of the UniqueConstraint class, you see that it is an annotation itself. When writing it down, you use the usual @AnnotationTypeName notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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