Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making all parameters final with an annotation in Java

I've recently been working with Lombok--and I'm wondering it would be possible to provide a class level or method level annotation that adds final to all parameters. For example:

@finalizer
void foo(Bar bar);

Becomes

void foo(final Bar bar);
like image 835
wegry Avatar asked Nov 09 '22 13:11

wegry


1 Answers

It is possible. Lombok could do it, there's already the FieldDefaults annotation allowing to make all fields final by default. Doing this for parameters would be even easier. While final on fields is something the JVM deals with (by not allowing assignments and issuing write barriers at the end of the constructor), final on parameters is only a compile time feature.

It isn't possible. Lombok doesn't do it currently and the author's opinion is that it makes no sense.

AFAIK there are tools enforcing final parameters.

I'd personally be most happy if in Java everything was final by default and could be overridden by using something like var. But this idea is coming 20 year too late.

like image 89
maaartinus Avatar answered Nov 14 '22 23:11

maaartinus