• Post Reply Bookmark Topic Watch Topic
  • New Topic

What do braces mean in an annotation?  RSS feed

 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take a look at http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html line 42 in the listing titled "Example 4: Simple Deserialization/Serialization To/From Container Object With Polymorphic Collection"
Here is a copy of the code in question for your convenience:

What the heck do those braces mean for the @JsonSubTypes({....});?
I think they create an array.
If that is true, then why does this code not compile?

Why does the code on the web site allow him to make an anonymous array literal but I cannot?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The notion for annotations is not the same as for code.
 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... So what do the braces mean then? Array? What kind of an array? If I pass {"abc", "def"} to an annotation will it infer String[]?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will infer whatever the annotation declares as its parameters.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the documentation, you'll see that the @JsonSubTypes annotation has a parameter that is an array of @Type annotations. The { and } enclose the array.

You cannot have the same annotation more than once on a class. For example, this would produce a compiler error:

To work around this, you need a wrapper annotation, which is in this case @JsonSubTypes.

In Java 8, this limitation is going to be removed, so that the wrapper annotation isn't necessary anymore.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!