英文:
Add javadoc in annotations
问题
I'd like to add javadoc for an annotation, specifically for lombok.
If I do this:
/**
* Custom constructor used only in <code>OrderMapper</code>.
* @param totalValue single parameter necessary to pass on the information to frontend
* @see <a href="https://github.com/projectlombok/lombok/issues/1269">Implicit @RequiredArgsConstructor on @Data will be removed when using @NoArgsConstructor #1269</a>
*/
@RequiredArgsConstructor
It doesn't return any errors, but the javadoc is not generated.
Is it even possible to do what I'm trying to? If yes, what am I doing wrong?
I thought it was because of the generic constructor, but @NoArgsConstructor
is unique and also doesn't work.
英文:
I'd like to add javadoc for an annotation, specifically for lombok.
If I do this:
/**
* Custom constructor used only in <code>OrderMapper</code></p>.
* @param totalValue single parameter necessary to pass on the information to frontend
* @see <a href= "https://github.com/projectlombok/lombok/issues/1269">Implicit @RequiredArgsConstructor on @Data will be removed when using @NoArgsConstructor #1269</a>
*/
@RequiredArgsConstructor
It doesn't returns any errors but the javadoc is not generated.
It is even possible to do what I'm trying to? If yes, What I'm doing wrong?
I thought it was because of the generic constructor, but @NoArgsConstructor
is unique and also doesn't work.
答案1
得分: 2
lombok核心维护者:
不可能直接为lombok生成的构造函数编写javadoc。理论上,lombok可以设置一些东西,使得出现在注解“正上方”的javadoc适用于该注解生成的内容,但由于各种原因,我非常怀疑我们会添加这个功能:
- 各种lombok节点生成多个内容,比如在类上使用
@Getter
生成多个getter。 - Javac希望在过程的早期将这些文档绑定到某些内容。我们可以解决这个问题,但这在技术上要求大量的错误报告。
- 它与许多样式指南和编写Java代码的已建立方式不兼容。具体而言,绝大多数Java程序员的写作方式如下:
/** javadoc */
@Override public void foo() {}
而不是:
@Override
/** javadoc */
public void foo() {}
尽管从“编译此代码”的角度来看,两者都是合法的,从“根据此代码生成javadoc”的角度来看也是合法的。
这是一个问题:如果每个人都这样写,而lombok更改了事物,使得:
/** javadoc */
@RequiredArgsConstructor
public class Foo {}
意味着javadoc现在位于构造函数而不是类上,这是一个破坏性的变化,而且测试不会捕捉到(我们不希望这样对我们的用户做!),即使以某种方式我们接受了所有这些损害,这意味着知道此更改并相应更新其代码的用户需要编写:
@RequiredArgsConstructor
/** javadoc */
public class Foo {}
而这是非标准的(从某种意义上说,它不符合90%以上的Java程序员所习惯的方式,而且lombok不打算试图以如此 drastica 的方式改变整个Java生态系统的已建立文化,除非它真的很有用,而这离有用还差得远)。
最多,我们可以说这:
/** javadoc A */
@RequiredArgsConstructor
/** javadoc B */
class Foo {}
是按照您想要的方式解释的,但是这个“功能”很难发现,而我们不喜欢添加注定几乎没有人使用的功能。这不值得维护的麻烦和学习曲线。如果没人知道它是如何工作的,如果他们看到您执行上述操作的文件,他们甚至知道它是什么意思吗?他们会推断这是否意味着如果删除javadoc B
,那么javadoc现在不再适用于构造函数而只适用于类,B必须存在以便lombok知道‘A’是为构造函数而设的吗?我怀疑,因此,如果它是这样工作的话,我们会否决此功能请求。太容易混淆了。
我想如果确实需要为一些样板代码编写javadoc,那么它就不再是样板代码,因此您将需要以长格式编写它。
但是,真的完全没有机会吗?
我们确实有一个可以用于此目的的“功能”:您可以像这样为单个字段编写javadoc:
/**
* A
* -- SETTER --
* B
*
* @param C
*/
@Getter @Setter private String field;
这将导致lombok将该javadoc拆分为多个部分,并等效于:
/**
* A
*/
private String field;
/**
* A
*/
public String getField() { return field; }
/**
* B
* @param C
*/
public void setField(String field) { this.field = field; }
相同的逻辑可以应用于类级别的javadoc,您可以执行类似以下的操作:
/**
* Water is wet
* -- CONSTRUCTOR --
* Circles are round
*/
@RequiredArgsConstructor
public class Foo {}
这将使lombok将该javadoc拆分为仅用于类的“水是湿的”和仅用于构造函数的“圆是圆的”。然而,这是一个相当大的变化 - 换句话说,您可能希望lombok为您生成的构造函数javadoc包含每个字段作为@param
,可能复制该字段上的javadoc(但如果那里有很多javadoc怎么办?@param 实际上并不打算包含很多,所以我们只取第一句吗?
您可以看到这如何变成一个需要做很多选择的大功能。总的来说,这个功能请求不会被立即拒绝,但特别是没有附带已实现该功能并更新文档的拉取请求的情况下,我怀疑我们不会立即满足该请求。
英文:
lombok core maintainer here:
It is not possible to directly javadoc the constructor that lombok generates. In theory lombok could set things up such that the javadoc that appears 'just above' an annotation applies to the thing that annotation generates, but this is problematic for various reasons so I highly doubt we would ever add this:
- Various lombok nodes generate multiple things, such as
@Getter
on a class which generates multiple getters. - Javac wants to bind those docs to something quite early in the process. We can work around it but it's technically asking for lots of bugreports.
- It isn't compatible with many style guides and established ways of writing java code. Specifically, the vast majority of java programmers write their stuff as:
/** javadoc */
@Override public void foo() {}
and not as:
@Override
/** javadoc */
public void foo() {}
even though both are equally legal both as far as 'compile this code' is concerned and as far as 'make javadoc based on this code' is concerned.
That's a problem then: If everybody writes like this and lombok changes things so that:
/** javadoc */
@RequiredArgsConstructor
public class Foo {}
means that the javadoc is now on the constructor and not on the class, that's a breaking change, and one that tests won't catch (we wouldn't want to do that to our users!) - and even if somehow we accepted all that damage, that means users who are aware of this change and update their code accordingly need to write:
@RequiredArgsConstructor
/** javadoc */
public class Foo {}
and this is non-standard (in the sense that it doesn't match what 90%+ of all java programmers are used to, and lombok is not interested in attempting to change the established culture of the entire java ecosystem in that drastic a fashion unless it really, really pays off, and this is a few ballparks short of being useful enough to try it).
At best, we could say that this:
/** javadoc A */
@RequiredArgsConstructor
/** javadoc B */
class Foo {}
is interpreted the way you want it to, but this 'feature' would be very hard to discover, and we don't like adding features that are doomed to be used by virtually nobody. It's not worth the maintenance hassle and learning curve. If nobody knows this is how it works and they see your file where you do the above thing, do they even know what it means? Would they surmise this means if you delete javadoc B
, that the javadoc now no longer applies to the constructor but only to the class, B has to be there so lombok knows that 'A' is for the constructor? I doubt it, hence, we'd veto this feature request if it works like this. Too likely to confuse.
I guess if there is a need to specifically javadoc some boilerplate, then it is no longer boilerplate so you'll need to write it out long form.
But, really, just no shot at all?
We do have a 'feature' that could be adopted for this: You can javadoc an individual field like so:
/**
* A
* -- SETTER --
* B
*
* @param C
*/
@Getter @Setter private String field;
and this results in lombok breaking that javadoc apart into bits, and is equivalent to:
/**
* A
*/
private String field;
/**
* A
*/
public String getField() { return field; }
/**
* B
* @param C
*/
public void setField(String field) { this.field = field; }
The same logic could be applied to class-level javadoc, that you could do something like:
/**
* Water is wet
* -- CONSTRUCTOR --
* Circles are round
*/
@RequiredArgsConstructor
public class Foo {}
and that lombok would break that javadoc apart into just 'water is wet' to go on the class, and just 'Circles are round' to go on the constructor. However, this is quite the change - presumably then you'd want the constructor javadoc that lombok makes for you to include each field as a @param
, perhaps copying over the javadoc on that field (but what if there's a ton of javadoc there? @param is not really intended to contain a lot, so do we just take the first sentence?
You see how that turns into a big feature that requires a lot of choices to be made. Point is, a feature request for this would not be immediately denied, but especially without an attached pull request with the feature implemented and the docs updated I doubt we'd get around to fulfilling that request anytime soon.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论