简体   繁体   English

Java注释处理器是否能够删除注释的代码

[英]Is Java annotation processor capable of removing annotated code

As we all know, we can automagicaly generate code using custom annotations and Java annotation processors as guys at project Lombook do. 众所周知,我们可以像Lombook项目中的人一样使用自定义注释和Java注释处理器自动生成代码。 But can we remove the annotated code from compiled sources ? 但是我们可以从编译源中删除带注释的代码吗?

I've tried searching the web for it, but only things that appear are "generate your code" topics and tutorials on "how to generate server with one annotation". 我曾尝试在网上搜索它,但是只有出现的事情是“生成代码”主题和有关“如何使用一个批注生成服务器”的教程。 It came to my mind while I was searching for ways to "compile out" debug messages from prod app. 当我在寻找从prod应用程序“编译”调试消息的方法时,就想到了这一点。 I can understand, that having the debug/test and production code is not a good practice, but sometimes it is needed to keep things simple. 我可以理解,拥有调试/测试和生产代码不是一个好习惯,但是有时需要使事情保持简单。 I think of few scenarios for this: 我认为有几种方案:

  1. make debug only, laggy code used in developer-only version of code that can have different levels of importance for example: 仅进行调试,在开发人员专用版本的代码中使用的延迟代码可能具有不同的重要性级别,例如:
@Debug(0) void cpuLightFunction(){}
@Debug(100) void cpuHeavyFunction(){} 

void doWork(){
   cpuLightFunction();
   cpuHeavyFunction();
}

In annotation processing step we could use some option to define max level of @Debug annotations that would be compiled. 在注释处理步骤中,我们可以使用一些选项来定义将被编译的@Debug注释的最大级别。 Any usage of @Debug with higher level would produce error or warning in the same way as @Deprecated 更高级别的@Debug任何使用@Debug以与@Deprecated相同的方式产生错误或警告

  1. platform specyfic code versions - create custom @Platform(ANDROID) void doSomething() and @Plaform(IOS) void doSomething functions that run only on given plaform to get rid of polymorphic void doSomething(AndroidPlatform) or void doSomethingAndroid() code 平台专用代码版本-创建自定义@Platform(ANDROID) void doSomething()@Plaform(IOS) void doSomething仅在给定平台上运行的函数可摆脱多态的void doSomething(AndroidPlatform)void doSomethingAndroid()代码

  2. have parts of code that are conditionally compiled: 有部分代码是有条件编译的:

@Optional("NetworkStub")
class NetworkStub{
   // ...
}

@Optional("PaymentStub")
class PaymentStub{
   // ...
}

and only use compiler/annotation processor options to enable/disable parts of the code, for example -Aoptional="NetworkStub" that would only compile code related to NetworkStub in the code and remove all code touching PaymentStub. 并且仅使用编译器/注释处理器选项来启用/禁用代码的某些部分,例如-Aoptional="NetworkStub" ,它将仅编译代码中与NetworkStub相关的代码,并删除所有涉及PaymentStub的代码。

You can do this by writing an annotation processor that traverses and modifies the program's AST (abstract syntax tree) during compilation, before code generation. 您可以通过编写一个注释处理器来做到这一点,该处理器在代码生成之前在编译过程中遍历和修改程序的AST(抽象语法树)。

That is how Project Lombok works. 这就是龙目岛项目的运作方式。

This question is a near-duplicate of How to write a Java annotation processor? 这个问题几乎与如何编写Java注释处理器重复 , but the accepted answer to that question says it's impossible, which is factually wrong. ,但该问题的公认答案表明这是不可能的,这实际上是错误的。

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

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