简体   繁体   中英

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.

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:

@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;


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

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