简体   繁体   English

Java覆盖注释的默认值

[英]Java override annotation's default value

I've a jar library that shared across multiple projects, the purpose of the library is to convert annotated field into stringify API response. 我有一个可在多个项目中共享的jar库,该库的目的是将带注释的字段转换为Stringify API响应。

public @interface MyField {
   ...
   String dateFormat() default "dd/MM/yyyy";
   ...
}

To use it: 要使用它:

@MyField
private String myDate;

The problem is when a project is using different Date Format (eg yyyy/MMM/dd) then I have to mark explicitly each annotation field in entire project as following example: 问题是当项目使用不同的日期格式(例如yyyy / MMM / dd)时,我必须明确标记整个项目中的每个注释字段,如下例所示:

@MyField(dateFormat = "yyyy/MMM/dd")
private String myDiffDate;

So far what I've tried: 到目前为止,我已经尝试过:

  1. No way to extends/implements an Annotation 无法扩展/实现注释

  2. Defined a Constant String as default value, but it won't compile unless the String was marked as "final". 定义了一个常量字符串作为默认值,但是除非该字符串被标记为“最终”,否则它将不会编译。

    String dateFormat() default BooClass.MyConstant; 字符串dateFormat()默认BooClass.MyConstant;


What options do I have in this case? 在这种情况下,我有什么选择?

I tried to manipulate annotation's default using array assuming it to be working same as storing value by reference. 我尝试使用数组操纵注释的默认值,假设它的工作原理与按引用存储值相同。 But it does not work and seems in case of array it returns a clone version. 但是它不起作用,并且似乎在使用数组的情况下会返回克隆版本。 Try below code ... 尝试下面的代码...

While running you will need to supply "test" as argument - 在运行时,您需要提供“测试”作为参数-

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    public class ManipulateAnnotationDefaultValue {

    public static void main(String[] args) throws NoSuchFieldException, SecurityException {

        String client = args.length > 0 ? args[0] : "default";
        MyField annotation = DefaultMyField.class.getDeclaredField("format").getAnnotation(MyField.class);

        String[] defaultValue = annotation.dateFormat();
        System.out.println("Hash code of MyField.dateFormat = " + defaultValue.hashCode());
        System.out.println("Value of MyField.dateFormat = " + defaultValue[0]);

        if (!client.equals("default")) {
            System.out.println("Changing value of MyField.dateFormat[0] to = 'dd-MM-yyyy'");
            defaultValue[0] = "dd-MM-yyyy"; // change the default value of annotation //$NON-NLS-1$
        }

        defaultValue = annotation.dateFormat();
        System.out.println("Hash code of MyField.dateFormat = " + defaultValue.hashCode());
        System.out.println("Value of MyField.dateFormat = " + defaultValue[0]);
    }
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyField {

    String[] dateFormat() default {"dd/MM/yyyy"};
}

class DefaultMyField {

    @MyField
    String format;

}

Below is the output - 以下是输出-

Hash code of MyField.dateFormat = 356573597
Value of MyField.dateFormat = dd/MM/yyyy
Changing value of MyField.dateFormat[0] to = 'dd-MM-yyyy'
Hash code of MyField.dateFormat = 1735600054
Value of MyField.dateFormat = dd/MM/yyyy

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

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