添加@ForceAliasFor注解

This commit is contained in:
huangchengxing 2022-07-08 11:59:44 +08:00
parent 491c53e7dd
commit 17b48024ad
5 changed files with 51 additions and 10 deletions

View File

@ -3,12 +3,12 @@ package cn.hutool.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
* <p>表示原始属性将作为关联属性的别名 * <p>{@link Link}的子注解表示原始属性将作为关联属性的别名
* <ul> * <ul>
* <li>原始属性为默认值时获取关联属性将返回关联属性本身的值</li> * <li>原始属性为默认值时获取关联属性将返回关联属性本身的值</li>
* <li>原始属性不为默认值时获取关联属性将返回原始属性的值</li> * <li>原始属性不为默认值时获取关联属性将返回原始属性的值</li>
* </ul> * </ul>
* <b>注意该注解与{@link Link}{@link MirrorFor}一起使用时将只有被声明在最上面的注解会生效</b> * <b>注意该注解与{@link Link}{@link ForceAliasFor}{@link MirrorFor}一起使用时将只有被声明在最上面的注解会生效</b>
* *
* @author huangchengxing * @author huangchengxing
* @see Link * @see Link

View File

@ -0,0 +1,31 @@
package cn.hutool.core.annotation;
import java.lang.annotation.*;
/**
* <p>{@link Link}的子注解表示原始属性将强制作为关联属性的别名效果等同于在原始属性上添加{@link Alias}注解
* 任何情况下获取关联属性的值都将直接返回原始属性的值
* <b>注意该注解与{@link Link}{@link AliasFor}{@link MirrorFor}一起使用时将只有被声明在最上面的注解会生效</b>
*
* @author huangchengxing
* @see Link
* @see RelationType#FORCE_ALIAS_FOR
*/
@Link(type = RelationType.FORCE_ALIAS_FOR)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface ForceAliasFor {
/**
* 产生关联的注解类型当不指定时默认指注释的属性所在的类
*/
@Link(annotation = Link.class, attribute = "annotation", type = RelationType.FORCE_ALIAS_FOR)
Class<? extends Annotation> annotation() default Annotation.class;
/**
* {@link #annotation()}指定注解中关联的属性
*/
@Link(annotation = Link.class, attribute = "attribute", type = RelationType.FORCE_ALIAS_FOR)
String attribute() default "";
}

View File

@ -5,11 +5,20 @@ import java.lang.annotation.*;
/** /**
* <p>用于在同一注解中或具有一定关联的不同注解的属性中表明这些属性之间具有特定的关联关系 * <p>用于在同一注解中或具有一定关联的不同注解的属性中表明这些属性之间具有特定的关联关系
* 在通过{@link SyntheticAnnotation}获取合成注解后合成注解获取属性值时会根据该注解进行调整<br /> * 在通过{@link SyntheticAnnotation}获取合成注解后合成注解获取属性值时会根据该注解进行调整<br />
* <b>注意该注解的优先级低于{@link Alias}且与{@link MirrorFor}{@link AliasFor}一起使用时将只有被声明在最上面的注解会生效</b> *
* <p>该注解存在三个字注解{@link MirrorFor}{@link ForceAliasFor}{@link AliasFor}
* 使用三个子注解等同于{@link Link}但是需要注意的是
* 当注解中的属性同时存在多个{@link Link}或基于{@link Link}的子注解时
* 仅有声明在被注解的属性最上方的注解会生效其余注解都将被忽略
*
* <b>注意该注解的优先级低于{@link Alias}</b>
* *
* @author huangchengxing * @author huangchengxing
* @see SyntheticAnnotation * @see SyntheticAnnotation
* @see RelationType * @see RelationType
* @see AliasFor
* @see MirrorFor
* @see ForceAliasFor
*/ */
@Documented @Documented
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

View File

@ -3,7 +3,7 @@ package cn.hutool.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
* <p>表示注解的属性与指定的属性互为镜像通过一个属性将能够获得对方的值<br> * <p>{@link Link}的子注解表示注解的属性与指定的属性互为镜像通过一个属性将能够获得对方的值<br>
* 它们遵循下述规则 * 它们遵循下述规则
* <ul> * <ul>
* <li>互为镜像的两个属性必须同时通过指定模式为{@code MIRROR_FOR}{@link Link}注解指定对方</li> * <li>互为镜像的两个属性必须同时通过指定模式为{@code MIRROR_FOR}{@link Link}注解指定对方</li>
@ -11,7 +11,7 @@ import java.lang.annotation.*;
* <li>互为镜像的两个属性在获取值且两者的值皆不同时必须且仅允许有一个非默认值该值被优先返回</li> * <li>互为镜像的两个属性在获取值且两者的值皆不同时必须且仅允许有一个非默认值该值被优先返回</li>
* <li>互为镜像的两个属性在值都为默认值或都不为默认值时两者的值必须相等</li> * <li>互为镜像的两个属性在值都为默认值或都不为默认值时两者的值必须相等</li>
* </ul> * </ul>
* <b>注意该注解与{@link Link}{@link AliasFor}一起使用时将只有被声明在最上面的注解会生效</b> * <b>注意该注解与{@link Link}{@link ForceAliasFor}{@link AliasFor}一起使用时将只有被声明在最上面的注解会生效</b>
* *
* @author huangchengxing * @author huangchengxing
* @see Link * @see Link

View File

@ -222,11 +222,12 @@ public class SyntheticMetaAnnotationTest {
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE }) @Target({ ElementType.METHOD, ElementType.TYPE })
@interface AnnotationForceForAliasForTest { @interface AnnotationForceForAliasForTest {
@Link( //@Link(
annotation = MetaAnnotationForForceAliasForTest.class, // annotation = MetaAnnotationForForceAliasForTest.class,
attribute = "name", // attribute = "name",
type = RelationType.FORCE_ALIAS_FOR // type = RelationType.FORCE_ALIAS_FOR
) //)
@ForceAliasFor(annotation = MetaAnnotationForForceAliasForTest.class, attribute = "name")
String value() default ""; String value() default "";
} }
@AnnotationForceForAliasForTest @AnnotationForceForAliasForTest