0

I am transitioning from Scala to Java and miss having final parameters by default. I explored an experimental version of the Lombok library here (background info here) which had files hinting at this functionality:

"src/core/lombok/experimental/FinalArgs.java", "src/core/lombok/javac/handlers/HandleFinalArgs.java", "src/core/lombok/eclipse/handlers/HandleFinalArgs.java"

I have never done much reflection or annotation writing in Java, so points to anyone who can figure out how to get a working @FinalArgs annotation into my current Java project (not connected to Lombak - my team is not willing to use a modified/custom version of Lombak). First prize to anyone who can also explain what is going on.

Roel Spilker
  • 32,258
  • 10
  • 68
  • 58
Michael Lafayette
  • 2,972
  • 3
  • 20
  • 54

2 Answers2

1

You can't get such an annotation without doing what Lombok does (installing plugins in IDE's and possibly installing a vm agent).

However, you can set most IDE's to give you a warning or error if a parameter was re-assigned.

There's no further benefit in making them truly final. The JIT is smart enough to figure that out on its own.

john16384
  • 7,800
  • 2
  • 30
  • 44
0

Lombok will not add this feature. You can instruct your IDE or static source analyzer to generate warnings, but since java8, there is no added benefit to make variables and parameters final. If they are effectively final (never reassigned) they can be used inside lambda's and anonymous inner classes as if they were final.

Roel Spilker
  • 32,258
  • 10
  • 68
  • 58