A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/177 below:

[java] SingularField with lambdas as final fields · Issue #177 · pmd/pmd · GitHub

Students of mine are using PMD to check their code for style errors, and are getting a false positive on the SingularField checker when the field is used inside a lambda expression. I’ve found https://sourceforge.net/p/pmd/bugs/1307/ online, which appears to be closed and resolved a year ago, but the code below is still problematic.

I’ve narrowed the problem down to a tiny reproduction. I am using PMD 5.5.1 in class, though I’ve tested the example below with PMD 5.5.2 as well. The code below triggers a SingularField error for timer1, but not for timer2:

/** Dummy class. */
public final class Test {
  private Timer timer1;
  private Timer timer2;

  /** Dummy constructor. */
  public Timer() {
    this.timer1 = new Timer(0, e -> {
      // do nothing for now
    });
    this.timer2 = new Timer(0, e -> {
      // do nothing for now
    });
  }

  /** Use a lambda expression to reference timer1 -- triggers SingularField error. */
  private final Runnable play1 = () -> {
    this.timer1.start();
  };

  /** Use an anonymous class to reference timer2 -- no error. */
  private final Runnable play2 = new Runnable() {
    @Override
    public void run() {
      this.timer2.start();
    }
  };
}

It seems that PMD isn’t recognizing the lambda syntax properly. I tried being more explicit and saying Test.this.timer1 to make it really clear that I’m accessing a field, I’ve tried eliminating the this. before timer1, and I’ve tried changing the finality and visibility of the play1 lambda; none of them affected the error.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4