0

I want my coworkers to not ignore result of method, f.e. in builder, when You do not build object. I have fluent api that will log message. If someone will not used final method to log, the log message will be gone. How I can Warn, or compile error when such case occurs. F.e.: Success case- message will be logged in log file/console:

logbuilder.text("").level("").log();

Failure case- message will NOT be logged in log file/console:

logbuilder.text("").level("");

I would like compiler to fail in such case. Please help. Thanks.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
masterdany88
  • 5,041
  • 11
  • 58
  • 132
  • You can try to creating a rule, e.g. with ArchUnit that catches that. But in general you cannot achieve that, especially not using the regular compiler, only with with static code analysis. – luk2302 Apr 07 '21 at 11:56
  • 2
    Have a look at [How can ignoring of return value be marked in Java code?](https://stackoverflow.com/q/55669689/3890632) and [Compile time warning when ignoring returning object](https://stackoverflow.com/q/11779224/3890632) and [Using annotation to ensure that value returned by method is not discarded](https://stackoverflow.com/q/3614150/3890632) – khelwood Apr 07 '21 at 11:57
  • May I ask? Why would you even want to create a LogBuilder class? There are more than enough logging frameworks out there, and it seems your logbuilder makes the code more complex without giving any obvious benefit. What am I missing? – Axel Apr 07 '21 at 12:20
  • It just simple example of tought. I have high requirements about logging. – masterdany88 Apr 07 '21 at 12:38
  • @masterdany88 I doubt your requirements can't be satisfied with something like Log4j or Slf4J, which would bemuch simpler than the code you've shown – Stultuske Apr 07 '21 at 12:46

0 Answers0