How to toggle some code changes in IntelliJ?
Answered
Let's assume that you need to have a performance tuning mode that requires you to calculate the "before" and "after" time for selected methods in java. Is there a feature in IntelliJ allowing you to do the following:
1- Toggle mode (which means i'm in performance tuning mode or not in my case) then add specific line of codes to the selected methods.
2- Adding code to the selected methods should not rely on line numbers as code changes.
Please advice. This will save a tremendous amount of time if this can be automated.
Please sign in to leave a comment.
You may consider using AspectJ to alter the bytecode with the logging code when needed, or one can use javaagent that will modify the bytecode at runtime. There is nothing specific for that in IntelliJ IDEA.
Thank you for your response, Serge!
However, I'm trying to avoid using any bytecode interceptors/manipulation at the runtime since it has performance disadvantages which will result in skewing the performance tuning results itself in unpredictable manner. That's why i'm trying to see if there is a way to do such mechanism at the compile time rather in the runtime.
Annotation processing in Java is a compile time mechanism but it does not allow modifying a current code and only allows to generate new ones.
AspectJ supports that at compile time: https://connectopensource.atlassian.net/wiki/spaces/CONNECTWIKI/pages/8585458/Performance+Instrumentation+using+AOP+with+AspectJ.
I will give it a try! Thanks, Serge!