gerard
gerard

Reputation: 1961

Can a Java annotation spread over multiple lines?

I use GWT I18N, which relies on annotations for messages with parameters.
Exemple :

@DefaultMessage("Here is a message <br/> with a param:{0}")
String messageToLocalize(String param);

In absence of a localized translation, the default message will be used.
I have some quite long strings to handle, which I would like to type in sevral lines for code readability (I don't speak about multiple lines for the rendered message).

I tried this :

@DefaultMessage("Here is a long \
message <br/> with a \
param:{0}")
String messageToLocalize(String param);

It fails (GWT PlugIn 4.2 and SDK 2.5.1) with an error "Invalid Escape Sequence".

Did I miss something ?
Is it a constraint on Java annotations or GWT ? (I am afraid so but couldn't find anything on that)
Is there a workaround ?
Thanks


Edit : Given first answers, the question must be rephrased : is it possible, and which character should I use to show continuation (if any) ?
The annotation processor obviously needs something to tell him. I tried "\" because it is the char to use in property file ...
"+" does not work either.

Upvotes: 3

Views: 6506

Answers (4)

Andremoniy
Andremoniy

Reputation: 34900

Java doesn't support C-style string-lines representation, so you could not use such multi-line style neither in annotations declarations, nor in other places of code.

If you want to have multiply lines of a single string, you have to do something like this:

@DefaultMessage("Here is a long " +
        "message <br/> with a " +
        "param:{0}")

Upvotes: 8

itsapenguinmachine
itsapenguinmachine

Reputation: 139

You can use a multiline text-block to spread a java-annotation over multiple lines. (start and end with three double-quotes)

Like this:

@SomeAnnotation("""
This is
a multiline
string
""")

https://www.baeldung.com/java-multiline-string#text-blocks

Upvotes: 3

Sylvain B
Sylvain B

Reputation: 513

As Andremoniy said, it must be cut using Java-style.

But otherwise, I recommend you to have a look at i18nCreator. It allows you to manage your i18n in properties files and have these Messages interfaces files automatically generated: https://developers.google.com/web-toolkit/doc/latest/RefCommandLineTools#i18nCreator

(There is also a maven plugin: http://mojo.codehaus.org/gwt-maven-plugin/user-guide/i18n.html)

Upvotes: 1

Dewfy
Dewfy

Reputation: 23614

Try string concatenation that is done at compile time, so:

@DefaultMessage("Here is a long"+
"message <br/> with a " +
"param:{0}"")

Upvotes: 0

Related Questions