Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String.format() as annotation attribute value

I have a class that have a number of constants:

public class SecurityConstants {
    private static final String HAS_ROLE_TEMPLATE = "hasRole('%s')";

    public static final String ROLE_USER_INTERNAL = "ROLE_USER_INTERNAL";
    public static final String HAS_ROLE_USER_INTERNAL = String.format(HAS_ROLE_TEMPLATE, ROLE_USER_INTERNAL);
}

If I then try to use HAS_ROLE_USER_INTERNAL as @PreAuthorize annotation attribute value like this @PreAuthorize(SecurityConstants.HAS_ROLE_USER_INTERNAL) compiler fails with:

The value for annotation attribute PreAuthorize.value must be a constant expression

However if I change HAS_ROLE_USER_INTERNAL to be a simple String it works just fine:

public static final String HAS_ROLE_USER_INTERNAL = "hasRole('ROLE_USER_INTERNAL')";

What's the problem with using String.format()? Field is static and final, what can possibly go wrong?

like image 722
parxier Avatar asked May 19 '11 02:05

parxier


People also ask

What is string format () used for?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

What is the annotation used to define attributes for an element?

When we create a custom annotation, we declare elements as methods and later set values as if they were attributes. For example, here we have declared a custom annotation ComponentType with elements name() and description() that look like methods.


1 Answers

The value of String.format() isn't known at compile time, whereas a String literal is.

Since the annotations are metadata on the compiled class, their values must be known by the time the compiler generates the final .class file. Since String.format()'s value will only be known once the code is actually run, the compiler won't let you use it as part of an annotation.

like image 77
dlev Avatar answered Oct 18 '22 18:10

dlev