简体   繁体   English

在注释处理器中生成方法

[英]Generate Methods in annotation processors

I have an annotation processor that checks the classes annotated with @Builder if they have setters for every field (and generates builder class based on those methods).我有一个注释处理器,它检查使用@Builder 注释的类是否具有每个字段的设置器(并基于这些方法生成构建器 class)。 Now it is possible to generate those setters methods (if they dont exist) before my annotation processor kicks in?现在可以在我的注释处理器启动之前生成这些设置方法(如果它们不存在)吗?

I know that a annotation processor can only generate new files and not manipulate any existing ones.Also libraries like ASM and Javassist work with.class files that do not exist when the annotation processor is used.我知道注释处理器只能生成新文件而不能操作任何现有文件。ASM 和 Javassist 之类的库也可以与使用注释处理器时不存在的 class 文件一起使用。 Any suggestions?有什么建议么?

Project Lombok - and note how lombok includes plugins and special tooling for many many dev tools (such as IntelliJ, VSCode, Eclipse, GWT, gradle, maven, many many versions of javac, NetBeans, etc) and is a lot of lines of code. Project Lombok - and note how lombok includes plugins and special tooling for many many dev tools (such as IntelliJ, VSCode, Eclipse, GWT, gradle, maven, many many versions of javac, NetBeans, etc) and is a lot of lines of code .

The Annotation Processor API simply does not include options to add/change things in existing files.注释处理器 API 根本不包括在现有文件中添加/更改内容的选项。 Hence why you need to write this logic for every platform you care to target, which is slightly easier than it sounds as there is a limited set of actual baseline java parsers/compilers out there (basically, ecj and javac - on top of which most things are built), so eg lombok's VSCode integration is very simple (as VSCode uses an eclipse-based language server, so 95% of the work for VSCode and Eclipse is shared), but I chose the word slightly rather intentionally there.因此,为什么您需要为您关心的每个平台编写此逻辑,这比听起来要容易一些,因为实际基线 java 解析器/编译器(基本上,ecj 和 javac - 最重要的是大多数东西都建好了),所以例如 lombok 的 VSCode 集成非常简单(因为 VSCode 使用基于 eclipse 的语言服务器,所以VSCode和 Eclipse 的 95% 的工作是共享的),但是我在那里选择了这个词,但我是故意的。

In other words, not possible unless you want to spend a few personyears.换句话说,除非你想花几个人年,否则不可能。 Or fork Lombok.或者 fork Lombok。

DISCLAIMER: I'm a core contributor to Project Lombok.免责声明:我是 Project Lombok 的核心贡献者。

Short answer is no, if you want to keep it clean and stay away from something like lombok, Annotation processors are all about generating new classes not changing existing classes, as for your case you actually should not do something like, but your annotation processor should check if the setter exists then you use the setter if not you just assign the field directly expecting the field to have a proper visibility scope, if it is not then you get a compiler error letting the user to know that he needs to expose those fields.简短的回答是否定的,如果你想保持干净并远离龙目岛之类的东西,注释处理器都是关于生成新类而不改变现有类,至于你的情况你实际上不应该做类似的事情,但你的注释处理器应该检查 setter 是否存在,如果不存在,则使用 setter .

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

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