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

[BUG] "value" field in init expression of @Getter(lazy=true) not working #2917

Closed
Vyacheslav-Lapin opened this issue Aug 3, 2021 · 5 comments · Fixed by #3583
Closed

[BUG] "value" field in init expression of @Getter(lazy=true) not working #2917

Vyacheslav-Lapin opened this issue Aug 3, 2021 · 5 comments · Fixed by #3583
Assignees

Comments

@Vyacheslav-Lapin
Copy link

This code:

@RequiredArgsConstructor
public class LazyGetterBugExample {

  String value;

  @Getter(lazy = true)
  int valueLength = value.length();

  public static void main(String[] args) {
    System.out.println(
        new LazyGetterBugExample("Lorem ipsum dolor sit amet")
            .getValueLength());
  }
}

will rise exception through compile time:

error: cannot find symbol
@Getter(lazy = true)
^
symbol: method length()
location: variable value of type java.lang.Object

..., but if we just RENAME field variable "value" to whatever else - "val", for example - it compiles and executes successfully!

The reason is...
this class will delomboked to this:

public int getValueLength() {
    java.lang.Object value = this.valueLength.get();
    if (value == null) {
      //...
      final int actualValue = val.length();
      //...
    }
  }

I.e., when we try to use "value", it conflicts with local variable - "value", used in code, generated by Lombok (and has a type java.lang.Object and actually has not "length()" method, that's why we had an error message above).

So, I think, the problem is that LOCAL VARIABLE in method, generated by Lombok for @Getter(lazy = true), was not named so well...
This name - "value" - is used very often for field, so I think this bug is important to fix.

Possible solution
I think, there is 2 variants:

  1. Insert check in code of generator, that if we use the "value" field in expression, then assign the local variable any another name (and check that it is not used in expression too. of course)
  2. Just rename the "value" local variable to something, that will never used by anyone - like $_$321_IT_WILL_NEVER_USED_BY_ANYONE_123_$_$, for example...

Version info:

  • Lombok version: 1.18.20
  • Platform: javac, full $ java -version output:
    openjdk version "16.0.2" 2021-07-20
    OpenJDK Runtime Environment Corretto-16.0.2.7.1 (build 16.0.2+7)
    OpenJDK 64-Bit Server VM Corretto-16.0.2.7.1 (build 16.0.2+7, mixed mode, sharing)
@Rawi01 Rawi01 self-assigned this Jan 5, 2024
@rzwitserloot
Copy link
Collaborator

While in theory we can do some fancy footwork and notice that this is happening and inject a this.value instead of just value, adding a dollar in there somewhere would certainly be a lot simpler.

@Rawi01
Copy link
Collaborator

Rawi01 commented Jan 13, 2024

My plan was to keep using value and only use a different name if there is a conflict. Always $value is simpler, if you think that is ok I would revert my conflict detection and just use it.

@dstango
Copy link

dstango commented Jan 13, 2024

Why not just always prefix the field with "this." at the corresponding places? This should avoid name conflicts in all cases, or doesn't it? Or can @Getter also be put on a static field?

@Rawi01
Copy link
Collaborator

Rawi01 commented Jan 13, 2024

Yes, @Getter is allowed on static fields. We already handle that case and could use ClassName.value instead of this.value but transforming the provided initializer is way more error prone than changing only the code we generate.

@rspilker
Copy link
Collaborator

This is part of the latest edge release, all feedback is welcome.

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

Successfully merging a pull request may close this issue.

5 participants