Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

There is a lot of confusion about the getCompletions method in the javax.annotation.processing package. What is the completion function returned by this method? Can you give me an example of specific use. Thank you。

 Iterable<? extends Completion> getCompletions(Element element,
                                                  AnnotationMirror annotation,
                                                  ExecutableElement member,
                                                  String userText);

There is a lot of confusion about the getCompletions method in the javax.annotation.processing package.

By who?

What is the completion function returned by this method?

it's a nice-to-have, optional feature. What it is for, is to help IDEs specifically.

When you compile code with javac or some other command line compiler, this method isn't invoked at all. It's there solely for IDEs. When you open up your eclipse or intellij or whatnot, type "Hello". and wait 500msec or hit CTRL+SPACE or whatever the IDE uses for shortcut for 'auto-complete', you get a menu of sorts that shows all the various methods that strings have. And possibly some templates and other features as well; IDEs are free to add whatever they want to this, there is no actual java specification for 'auto completers in IDEs'.

Nevertheless, that's what this method is for.

Specifically, it's for having the IDE help you out when you type: @YourAnn(someAnnotationParamName=...) - anywhere in the ... part, that's where this feature is relevant.

How to help yourself next time

Docs are good... if they are there. But in this case, they were there. The docs on this feature can be found where-ever you find your javadoc (google, your IDE, and so many other places): The javadoc of Processor.getCompletions explains stuff, including a great example.

Given that this example is quite nice, I dispute your characterization that 'there is a lot of confusion about the getCompletions method' :)

Thanks for you answer. Your understanding of the problem is correct. This answer solved my confusion about the meaning of the getCompletions function. But I tried to use this case in intelij, but the IDE did not give me a complete prompt. Maybe my configuration is incorrect. Can you provide a document that allows me to see that the IDE has a prompt for completion of the annotation value. – duke yu Sep 22, 2020 at 3:04 You'd have to check some intellij forums, perhaps - As it is optional intellij might not support it. More generally, intellij would have to be aware of your annotation processor so I can invoke the completion method. It's also plausible that intellij will just ignore any exceptions that you throw - you may want to add (file based) logging, check if you're being invoked at all. – rzwitserloot Sep 22, 2020 at 11:02

getCompletions is intended for use by IDEs. It helps users to know what arguments may be supplied to an annotation.

It is well described by its Javadoc documentation.

The example used there is: Suppose there is an annotation defined as

@MersennePrime {
    int value(); // must be a prime of the form 2^n-1, e.g., 3, 7, 31, 127, 8191, ...

The implementation of getComletions could be

return Arrays.asList(of("3"),
                      of("7"),
                      of("31"),
                      of("127"),
                      of("8191"),
                      of("131071"),
                      of("524287"),
                      of("2147483647"));

If a user has written @MersennePrime, the IDE can suggest the arguments returned by getCompletions. If a user has written @MersennePrime(1, then getCompletions is passed "1" as its userText command-line argument and should return a list containing just 127 and 131071.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.