Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding final to the parameter from addFinalIfNeed in HandlerSetter? #3647

Open
JadeKim042386 opened this issue Apr 2, 2024 · 3 comments
Open

Comments

@JadeKim042386
Copy link

JadeKim042386 commented Apr 2, 2024

First, I wanted to know why final is added to parameter after build when I used "@Setter".

Before Build

public class Waste {
    @Setter
    private LocalDateTime transactionAt;
}

After Build

The class file (Waste.class) that was created after the build was opened by IntelliJ.

public class Waste {
    public void setTransactionAt(final LocalDateTime transactionAt) {
        this.transactionAt = transactionAt;
    }
}

I also executed "javap -p -c -v".

    public void setTransactionAt(java.time.LocalDateTime);
    descriptor: (Ljava/time/LocalDateTime;)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: aload_1
         2: putfield      #68                 // Field transactionAt:Ljava/time/LocalDateTime;
         5: return
      LineNumberTable:
        line 64: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lfreshtrash/freshtrashbackend/entity/Waste;
            0       6     1 transactionAt   Ljava/time/LocalDateTime;
    MethodParameters:
      Name                           Flags
      transactionAt                  final

searching the code to add final keyword

I found a suspicious code.

long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());

Question

Adding final to the parameter from addFinalIfNeed in HandlerSetter?

@janrieke
Copy link
Contributor

janrieke commented Apr 2, 2024

Basically, it's just a formatting/style question (which, BTW., you can customize with the setting finalParams=skip when delomboking). There's no difference when it comes to the semantics of the generated code. It also affects neither method overriding nor overloading.

@JadeKim042386 JadeKim042386 changed the title is adding adding final to the parameter from addFinalIfNeed in HandlerSetter? Adding adding final to the parameter from addFinalIfNeed in HandlerSetter? Apr 3, 2024
@JadeKim042386
Copy link
Author

JadeKim042386 commented Apr 3, 2024

@janrieke Thans, about your comment. but my question is "Adding final to the parameter from addFinalIfNeed in HandlerSetter?". i'm just wondering what that code does.

And you said "There's no difference when it comes to the semantics of the generated code.". so what made you add the final keyword by default? If there really isn't a difference in semantics, shouldn't we add the option "finalParams" to "annotation" rather than "delombok"? (e.g. @Setter(finalParams=true))

@janrieke
Copy link
Contributor

janrieke commented Apr 4, 2024

I really don't understand your question. If it makes no semantic difference, why should that be a parameter for the annotation? Unless you delombok, you will never read that generated code, because it's generated "under the hood" of javac.

And if you delombok: Some developers like automatically making parameters final, some don't, and some simply do not care. That's mainly a choice of style. For instance, Eclipse has a similar auto-formatting setting. So delombok has it configurable and there is a default (which, to repeat, does not make any semantic difference for the code lombok generates).

@JadeKim042386 JadeKim042386 changed the title Adding adding final to the parameter from addFinalIfNeed in HandlerSetter? Adding final to the parameter from addFinalIfNeed in HandlerSetter? Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants