简体   繁体   English

编译时注释处理

[英]Compile-time annotation processing

Is there way to do compile-time annotation processing in Java?有没有办法在 Java 中进行编译时注释处理?

Consider this example:考虑这个例子:

@Name("appName")
private Field<String> appName;

public void setAppName(String name) {
   appName.setValue(name);
}

public String getAppName(String name) {
   return appName.getValue();
}

public void someFunction() {
   String whatFieldName = appName.getName();
}

Where the annotation Name will be processed at compile-time to set the value for Field That is without the common runtime annotation processing.其中注解Name将在编译时处理为Field设置值,即没有常见的运行时注解处理。 As such, when appName.getName();因此,当appName.getName(); (the Field ) is accessed it will return the typed value. Field )被访问,它将返回键入的值。

Yes, there is, but, no, it cannot change existing files.是的,有,但是,不,它不能更改现有文件。 You can 'plug in' to the compiler and be informed of any annotations;您可以“插入”编译器并获悉任何注释; as part of this, you can see signatures (so, field declarations, method signatures, types, etc) but no contents (so not the expression used to initialize a field, and not the contents in the {} of a method declaration), and you can make NEW files, even java files, but you can't edit existing ones.作为其中的一部分,您可以看到签名(因此,字段声明、方法签名、类型等)但没有内容(因此不是用于初始化字段的表达式,也不是方法声明的 {} 中的内容),并且您可以制作文件,甚至是 java 文件,但您不能编辑现有文件。

Project Lombok does edit them, but that is quite the framework to make that possible. Project Lombok确实对它们进行了编辑,但这正是使之成为可能的框架。

There are some crazy tricks you can use.您可以使用一些疯狂的技巧。 Project lombok uses one trick (reflect its way into compiler internals, fix everything from there, install agents and plugins in IDEs). Lombok 项目使用了一个技巧(将其方式反映到编译器内部,从那里修复所有内容,在 IDE 中安装代理和插件)。 Another trick is to use a java source file as a template, of sorts.另一个技巧是使用 java 源文件作为模板。 You name your class some funky (so if you want, say, public class AppDescriptor , you'd actually make the java file AppDescriptorTemplate.java and put public class AppDescriptorTemplate inside. This file has the annotation precisely as you pasted. Your annotation processor can then, during compilation, generate AppDescriptor.java , writing the impls of all methods as simple pass-throughs (a field of type AppDescriptorTemplate is generated, and all methods in ADT are copied over, and the implementations are all one-liners that just invoke that method on the template class). The template class can be package private. In this specific scenario it sounds like you can generate virtually the whole thing based off of pretty much only "appName" , though. You name your class some funky (so if you want, say, public class AppDescriptor , you'd actually make the java file AppDescriptorTemplate.java and put public class AppDescriptorTemplate inside. This file has the annotation precisely as you pasted. Your annotation processor can然后在编译的时候生成AppDescriptor.java ,把所有方法的impls写成简单的pass- AppDescriptorTemplate (生成一个AppDescriptorTemplate类型的字段,把ADT中的所有方法都复制过来,实现都是单行调用模板类上的那个方法)。模板 class 可以是 package 私有的。在这个特定的场景中,听起来你几乎可以仅基于"appName"生成整个东西。

Lombok plugs straight into the build and is therefore virtually entirely transparent, in the sense that you simply type in your IDE and the methods it generates just appear as you type, whereas 'normal' annotation processors that eg use the XTemplate trick do not work that way and require the build system to kick in, every time. Lombok 直接插入构建,因此几乎完全透明,因为您只需键入 IDE,它生成的方法就会在您键入时出现,而使用XTemplate技巧的“普通”注释处理器不起作用方式并要求构建系统每次都启动。 It can be a bit of a productivity drain.这可能有点浪费生产力。

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

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