1

I have following class.

public class MultipartFileWrapper {
    @Extensions({".jpg",".png",".gif",".bmp",".mp4"})
    MultipartFile multipartFile;  
    ...
}

Now I want to extract formats to configuration file. But I don't understand how to replace

@Extensions({".jpg",".png",".gif",".bmp",".mp4"})

I want to rewrite it something like this:

public class MultipartFileWrapper {
        @Extensions(readFormatsFromFile())
        MultipartFile multipartFile;  
        ...
    }

formats should takes from external file with the following content:

.jpg,.png,.gif,.bmp,.mp4

Does java allow do this?

1 Answer 1

2

Attribute values must be constant.

From the Java Language Specification, section 9.7.1, emphasis mine:

Java does not allow for runtime annotation arguments: annotation parameters are stored at compile-time, and hence cannot be dynamic.

If T is a primitive type or String, and V is a constant expression (§15.28).

V is not null.

If T is Class, or an invocation of Class, and V is a class literal (§15.8.2).

If T is an enum type, and V is an enum constant.

In your case, V is not a constant expression.

Aside from this, many annotations may not be preserved in compiled code unless explicitly specified via @Retention(RetentionPolicy.RUNTIME).

Sign up to request clarification or add additional context in comments.

2 Comments

hmm...it is my custom annotation I can set another retention policy
In your situation, its not possible to accomplish what you wish, not because of the retention policy, but because of the Java Language Specification. I've updated my answer to include the relevant portion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.