I can imagine this is bad practice, but this is purely for a private project and as an exercise for myself.
I have a Class, which has a lot different static methods. They are used to test some stuff about Java (I am still learning Java, so I put different things there which I want to test myself like how Inheritance works etc.).
Some of these methods will generate compile-time errors, so my Project will not build. I want to create an annotation like @DoNotCompile, which will mark the method, and before compiling the method will be replaced with empty body or something like throw new RuntimeError("This method should not be called").
Currently I just have @Test for methods which will be executed and @NotTest for methods which should not be executed, then I put the body in comments. My goal with this is learning - I want to see, which error would be produced in my Editor, but ignore it when compiling.
I am coding in Java 17, on IntelliJ IDEA 2023.1 Community Edition if this helps. I use gradle, but I am not very experienced in it, but maybe there is some gradle script which can achieve this?
Any help is appreciated, and once again: I know this is bad practice, I am just very curious if this can be achieved.
Edit: It would also be great, if I can somehow record the compile-time error in this process and maybe replace the body with something like sout("Did not compile: <errors or warnings from compiler>");
Edit 2: In the end I just wrote a node.js script for preprocessing and added it as external tool to my run configuration in IntelliJ. In addition I removed the normal build command and replaced the classpath with my processed file path. Now works like a charm, but it has some disadvantages: I only test for a specific annotation, when running the code, so it is not flexible. Also I do not use a parser to get the method, so it just assumes, that the code style is kept. I will upload the script as an answer shortly after.
As a workaround I was only able to create a dirty pre-processor, here is the setup you will need: (Note: I am using IntelliJ, this might differ in your installation if you use another IDE)
Caveats:
@Testto find such methods, you will have to modify it yourself if you want to use another annotation@Testannotation does not have to be the last one, but it is advised to still make it the last oneThe procedure of the pre-processor:
throw new RuntimeException("Compile time error was encountered: <error(s)>")processed/srcfolderprocessed/outfolder