1

I want to use an custom annotation in a java project. Curretnly i'm having this:

@Parallel(synchronicity=Sync.SYNC, concurrency=Conc.MUTEX)

Sync and Conc are both enums.

This is a little too verbose. Its not that synchronicity is a long word, but that i have to specify the enum name. I would prefer to write it in one of the following ways:

@Parallel(synchronicity=SYNC, concurrency=MUTEX)

@Parallel(Sync.SYNC, Conc.MUTEX)

But both don't seem to be possible. Does anybody have an idea on how to make the usage of enums in an annotation less verbose?

2
  • You could probably achieve something like the second if you supplied an Object[] as value. I think the enum would look like this "@Parallel({SYNC, MUTEX})". If your enums were a single type you could ask for an array of that type, but since they are different I think you need to ask for an object array or have your enums both implement the same interface and request an array of that. Commented Feb 27, 2018 at 20:05
  • As a followup I might point out that in my experience Java is, with a few exceptions, only overly verbose to the unimaginative. Commented Feb 27, 2018 at 20:19

1 Answer 1

4

Add

import static com.foo.bar.Sync.*;
import static com.foo.bar.Conc.*;

to your imports, to be able to use

@Parallel(synchronicity=SYNC, concurrency=MUTEX)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, thats a great start. Not totally happy with it, because it means the users of my library will have to do that manually. But great answer, probably the best i'll get.
Note that the second way is also possible, but only if the annotation has a single attribute named value, IIRC.
@Asraniel I'd document the verbose way, and leave using those static imports to user's discretion. Especially if those are used only few times per class, only in your annotation, then there's no point cluttering the name space just to be able to leave 2 words out.
Good call hyde. Now that i also know about the single value annotation that can use value(), i'm trying to find a way to use it, but with two parameters. (thx for that JB Nizet)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.