As we all know, we can automagicaly generate code using custom annotations and Java annotation processors as guys at project Lombook do. 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. 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:
- 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. Any usage of @Debug
with higher level would produce error or warning in the same way as @Deprecated
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 polymorphicvoid doSomething(AndroidPlatform)
orvoid doSomethingAndroid()
codehave 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.