0

Motivation:

In our code we have a few places where some methods are run by their name. There are some big if-else-if blocks with each function name and call of the corresponding method (I use the term function to describe just names, for example function X01 might correspond to method SomeClass.functionX01). I've been looking into ways to improve that

Goal:

Write just methods that are annotated with some custom annotation, removing the need to update or even include if-else-if blocks in order to run specific function. Have access to any generated code if any code is generated.

What I did:

I've created first prove of concept using runtime annotations and it proved successful, but slower then if-else-if. Next attempt was with source annotation

I've followed this link for an example, however it did not seam to run in IntelliJ. What I wanted is to have - in this case PersonBuilder class generated, instead there was none. In some cases an error was raised Error:java: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider BuilderProcessor not found

After some Googling and failing to find anything I've turned to book (Core Java, Volume II - Advanced Features - 9th Edition, Polish translation) and there was reccomended to run the following commands:

javac [AbstractProcessor implementation]
javac -processor [Compiled Processor] [other source files to compile]

This worked, however is unsatisfactory as it needs to happen inside IDE (NetBeans and IntelliJ to be specific) automatically during build. Code does not need to be generated on the fly, but programmer must have access to it after build (as in - be able to call methods of generated classes)

Question:

How to have and use generated code used in NetBeans and IntelliJ without the need of using external tools? Is it possible, or using reflection, runtime annotations or external tools is the only way?

Additional info (just in case):

Language level: Java 1.8

JVM versions: 12 and 13

IDEs: NetBeans and IntelliJ

Miku
  • 567
  • 6
  • 15
  • Can't you use lambdas to achieve what you describe in the "Motivation" section at the start of the question? If not, why not? And if you can, why do you prefer to go down a different path (i.e using annotations)? My apologies if I'm completely misunderstanding your issue. – skomisa Feb 05 '20 at 00:20
  • I have not considered lambdas because I don't see the use for them. The ultimate goal is to remove that if, or at least have it generated. Lambdas don't provide that mechanism as far as I know, but if you know how to do such a thing please share. – Miku Feb 05 '20 at 07:26
  • OK. I think it would be helpful if you updated your question to show a sample of your existing code with those _if-else-if..._ statements, and also how your "function name" variable is being set (since that determines which method to call, right?). – skomisa Feb 05 '20 at 15:16
  • The reason why I didn't include those if-else-if is that they themselves are very trivial - just `[else] if(todo.equals("SomeName")) functionSomeName(params)` over and over. As for how function name is set - that unfortunately comes from an API call. Client on the other side of the ethernet cable runs webmethod say `runFunction("F01")` and functionF01 is called. If that happened inside the program, then polymorphism would probably solve it. – Miku Feb 06 '20 at 08:16

0 Answers0