簡體   English   中英

修改自定義注釋的參數值

[英]Modify parameter value of custom annotation

有什么方法可以實現注釋以自行更改其參數值?

例如:

我想創建自定義RequestMapping注釋,以消除一些重復的代碼。

當前代碼:

@RequestMapping("/this/is/duplicate/few/times/some")
public class SomeController {
}

我想創建這樣的東西

@Retention(RetentionPolicy.RUNTIME)
@RequestMapping()
public @interface CustomRequestMapping {

    String value() default "";

    @AliasFor(annotation = RequestMapping.class, attribute = "value")
    String "/this/is/duplicate/few/times"+value();

}

為了減少對此的請求映射值:

@CustomRequestMapping("/some")
public class SomeController {
}

不幸的是,我找不到使它可編譯的方法。 也許有一種方法可以使用AliasFor批注將參數傳遞到目標數組。 像這樣:

@Retention(RetentionPolicy.RUNTIME)
    @RequestMapping()
    public @interface CustomRequestMapping {

    @AliasFor(annotation = RequestMapping.class, attribute = "value{1}")
    String value() default "";

    @AliasFor(annotation = RequestMapping.class, attribute = "value{0}")
    String prefixPath() default "/this/is/duplicate/few/times";

}

看來您正在嘗試制作注釋的子類型並修改其屬性之一的默認值

注釋的子類型化是不可能的, 是JSR說明原因。

It complicates the annotation type system,and makes it much more difficult
to write “Specific Tools”.

“Specific Tools” — Programs that query known annotation types of arbitrary
 external programs. Stub generators, for example, fall into this category.
 These programs will read annotated classes without loading them into the 
 virtual machine, but will load annotation interfaces.

解決復制問題的一種方法是提取一個常數。

@Annotation(MyClass.FOO+"localValue")
public class MyClass
{
    public static final String FOO = "foo";
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM