简体   繁体   English

java注解中Completion的使用

[英]The usage of Completion in java annotation

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? javax.annotation.processing package中的getCompletions方法有很多混淆,这个方法返回的completion function是什么? 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. javax.annotation.processing package中getCompletions方法有很多混淆。

By who?由谁?

What is the completion function returned by this method?这个方法返回的补全function是什么?

it's a nice-to-have, optional feature.这是一个不错的可选功能。 What it is for, is to help IDEs specifically.它的用途是专门帮助 IDE。

When you compile code with javac or some other command line compiler, this method isn't invoked at all.当您使用javac或其他命令行编译器编译代码时,根本不会调用此方法。 It's there solely for IDEs.它专门用于 IDE。 When you open up your eclipse or intellij or whatnot, type "Hello".当您打开 eclipse 或 intellij 或诸如此类的东西时,键入"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.等待 500 毫秒或按 CTRL+SPACE 或 IDE 用作“自动完成”快捷方式的任何内容,您将获得一个显示字符串具有的所有各种方法的菜单。 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'. IDE 可以自由添加他们想要的任何内容,没有实际的 java 规范用于“IDE 中的自动完成程序”。

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.具体来说,它是为了让 IDE 在您键入时帮助您: @YourAnn(someAnnotationParamName=...) - 在...部分的任何位置,这就是此功能相关的地方。

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.关于此功能的文档可以在您找到 javadoc 的任何地方找到(谷歌、您的 IDE 以及许多其他地方): Processor.getCompletions 的 javadoc解释了一些内容,包括一个很好的例子。

Given that this example is quite nice, I dispute your characterization that 'there is a lot of confusion about the getCompletions method':)鉴于这个例子非常好,我对你关于“getCompletions 方法存在很多混淆”的描述提出异议:)

getCompletions is intended for use by IDEs. getCompletions 旨在供 IDE 使用。 It helps users to know what arguments may be supplied to an annotation.它可以帮助用户了解可以向注释提供什么 arguments。

It is well described by its Javadoc documentation .它的Javadoc 文档对其进行了很好的描述。

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 getComletions的实现可以是

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 .如果用户写了@MersennePrime ,则 IDE 可以建议 getCompletions 返回的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 .如果用户编写了@MersennePrime(1 ,则getCompletions将作为其userText命令行参数传递“1”,并且应该返回仅包含127131071的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM