在Java注解中的使用完成(Completion)

huangapple go评论75阅读模式
英文:

The usage of Completion in java annotation

问题

关于javax.annotation.processing包中的getCompletions方法存在许多困惑。这个方法返回的是什么完成功能?您能给我一个具体用途的示例吗?谢谢。

 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. What is the completion function returned by this method? Can you give me an example of specific use. Thank you。

 Iterable&lt;? extends Completion&gt; getCompletions(Element element,
                                                  AnnotationMirror annotation,
                                                  ExecutableElement member,
                                                  String userText);

答案1

得分: 2

javax.annotation.processing 包中,关于 getCompletions 方法存在许多混淆。

谁产生了这些混淆?

这个方法返回的是什么补全函数?

它是一个很好但是可选的特性。它的用途是专门帮助集成开发环境(IDE)。

当你使用 javac 或其他命令行编译器编译代码时,根本不会调用这个方法。它完全是为了 IDE 而存在的。当你在 Eclipse、IntelliJ 或其他类似工具中输入 "Hello". 并等待 500 毫秒,或者按下 CTRL+SPACE 或者其他 IDE 用于 '自动完成' 的快捷键,你会得到一个菜单,显示出字符串(strings)具有的所有不同方法。可能还会有一些模板和其他功能;IDE 可以自由地向其中添加任何内容,实际上并没有针对 'IDE 中的自动补全' 的 Java 规范。

尽管如此,这就是这个方法的用途。

具体来说,它是用于在你键入 @YourAnn(someAnnotationParamName=...) 时,帮助 IDE 完成 ... 部分,这是该特性相关的部分。

下次如何自助

文档是很有用的……如果它们存在的话。但在这种情况下,文档是存在的。关于这个特性的文档可以在你找到 Java 文档的任何地方找到(例如谷歌、你的 IDE,以及其他很多地方):Processor.getCompletions 的 JavaDoc 解释了相关信息,包括一个很好的示例。

鉴于这个示例相当不错,我对你关于 '有关 getCompletions 方法存在许多混淆' 的描述提出异议 在Java注解中的使用完成(Completion)

英文:

> 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 &quot;Hello&quot;. 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' 在Java注解中的使用完成(Completion)

答案2

得分: 2

getCompletions 旨在供集成开发环境(IDEs)使用。它帮助用户了解可以提供给注解的哪些参数。

它的Javadoc文档中有很详细的描述。

其中使用的示例是:假设有一个定义为

@MersennePrime {
    int value(); // 必须是形式为2^n-1的素数,例如:3, 7, 31, 127, 8191, ...
}

getCompletions 的实现可能是

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

如果用户已经写了 @MersennePrime,IDE 可以建议由 getCompletions 返回的参数。如果用户已经写了 @MersennePrime(1,那么 getCompletions 将 "1" 作为其 userText 命令行参数,并且应返回一个只包含 127131071 的列表。

英文:

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(&quot;3&quot;),
                      of(&quot;7&quot;),
                      of(&quot;31&quot;),
                      of(&quot;127&quot;),
                      of(&quot;8191&quot;),
                      of(&quot;131071&quot;),
                      of(&quot;524287&quot;),
                      of(&quot;2147483647&quot;));

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.

huangapple
  • 本文由 发表于 2020年9月21日 21:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63993658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定