Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle and JAVA8 lambdas

I'm preparing large migration to JAVA8. Part of it is configuring checkstyle to properly work with JAVA8 constructions. I try to keep checkstyle synchronized with a default autoformatting tool in IntelliJ. Unfortunately it doesn't accept automatic IntelliJ indentation of lambda expressions.

checkstyle.xml

<module name="Indentation">
     <property name="caseIndent" value="0"/>
</module>

Code:

public class Java8CheckstyleTest {
    public void java8Elements(String ala) {
        Collections.sort(names, (String a, String b) -> {
            return b.compareTo(a); // <<<25 line
        }); // <<<26 line
    }
}

Checkstyle report:

Java8CheckstyleTest.java:25: 'block' child have incorrect indentation level 12, expected level should be 16.

Java8CheckstyleTest.java:26: 'block rcurly' have incorrect indentation level 16, expected level should be 12.

If I break "})" with a new line and autoformat:

public class Java8CheckstyleTest {
    public void java8Elements(String ala) {
        Collections.sort(names, (String a, String b) -> {
                    return b.compareTo(a); // <<<25 line
                } // <<<26 line
        ); 
    }
}

i get:

Java8CheckstyleTest.java:25: 'block' child have incorrect indentation level 20, expected level should be 16.

Java8CheckstyleTest.java:26:'block rcurly' have incorrect indentation level 16, expected level should be 12.

How should I configure checkstyle not to break old code and to accept these autoformatting schemes?

EDIT: I updated checkstyle to 6.11.1 and as I see problem is nearly solved. First code is accepted right now. Second produces report:

Java8CheckstyleTest.java:32: error: 'block' child have incorrect indentation level 20, expected level should be one of the following: 12, 16.

Java8CheckstyleTest.java:33: error: 'block rcurly' have incorrect indentation level 16, expected level should be one of the following: 8, 12.

I assume that it's probably a thing that have to be fixed in the next IntelliJ version. I will close this question.

like image 827
aerion Avatar asked Oct 20 '22 00:10

aerion


1 Answers

Adding:

  <dependency>
      <groupId>com.puppycrawl.tools</groupId>
      <artifactId>checkstyle</artifactId>
      <version>6.11.1</version>
  </dependency>

to the maven checkstyle plugin configuration solved a problem. Details in main thread.

like image 157
aerion Avatar answered Oct 22 '22 23:10

aerion