From ec579aff0ea63c2ab37006e926f4e83e5cd13ea4 Mon Sep 17 00:00:00 2001 From: Massimo Paladin Date: Thu, 24 Dec 2015 11:15:45 +0100 Subject: [PATCH] SONARJAVA-1396 IndentationCheck: should handle lambdas having block as body --- .../sonar/java/checks/IndentationCheck.java | 32 ++++++++++++--- .../checks/IndentationCheck_default.java | 41 +++++++++++++++++-- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/java-checks/src/main/java/org/sonar/java/checks/IndentationCheck.java b/java-checks/src/main/java/org/sonar/java/checks/IndentationCheck.java index 8e204e940ed..25c6fe940bb 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/IndentationCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/IndentationCheck.java @@ -33,6 +33,7 @@ import org.sonar.plugins.java.api.tree.CaseGroupTree; import org.sonar.plugins.java.api.tree.CaseLabelTree; import org.sonar.plugins.java.api.tree.ClassTree; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; import org.sonar.plugins.java.api.tree.StatementTree; import org.sonar.plugins.java.api.tree.SyntaxToken; import org.sonar.plugins.java.api.tree.Tree; @@ -64,7 +65,8 @@ public class IndentationCheck extends SubscriptionBaseVisitor { Kind.STATIC_INITIALIZER, Kind.INITIALIZER, Kind.SWITCH_STATEMENT, - Kind.CASE_GROUP + Kind.CASE_GROUP, + Kind.METHOD_INVOCATION ); private static final int DEFAULT_INDENTATION_LEVEL = 2; @@ -103,6 +105,9 @@ public void visitNode(Tree tree) { if (!isInAnonymousClass.peek()) { checkIndentation(Collections.singletonList(classTree)); } + } else if (tree.is(Kind.METHOD_INVOCATION)) { + adjustMethodInvocation((MethodInvocationTree) tree); + return; } expectedLevel += indentationLevel; isBlockAlreadyReported = false; @@ -125,6 +130,22 @@ public void visitNode(Tree tree) { } } + private void adjustMethodInvocation(MethodInvocationTree tree) { + int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line(); + int parenthesisLine = tree.arguments().openParenToken().line(); + if (startLine != parenthesisLine) { + expectedLevel += indentationLevel; + } + } + + private void restoreMethodInvocation(MethodInvocationTree tree) { + int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line(); + int parenthesisLine = tree.arguments().openParenToken().line(); + if (startLine != parenthesisLine) { + expectedLevel -= indentationLevel; + } + } + private void checkClass(ClassTree classTree) { // Exclude anonymous classes if (classTree.simpleName() != null) { @@ -160,16 +181,12 @@ private void checkCaseGroup(CaseGroupTree tree) { private void adjustBlockForExceptionalParents(Tree parent) { if (parent.is(Kind.CASE_GROUP)) { expectedLevel -= indentationLevel; - } else if (parent.is(Kind.LAMBDA_EXPRESSION)) { - expectedLevel += indentationLevel; } } private void restoreBlockForExceptionalParents(Tree parent) { if (parent.is(Kind.CASE_GROUP)) { expectedLevel += indentationLevel; - } else if (parent.is(Kind.LAMBDA_EXPRESSION)) { - expectedLevel -= indentationLevel; } } @@ -190,7 +207,10 @@ private void checkIndentation(Tree tree, int expectedLevel) { @Override public void leaveNode(Tree tree) { - if (tree.is(Kind.BLOCK)) { + if (tree.is(Kind.METHOD_INVOCATION)) { + restoreMethodInvocation((MethodInvocationTree) tree); + return; + } else if (tree.is(Kind.BLOCK)) { restoreBlockForExceptionalParents(tree.parent()); } expectedLevel -= indentationLevel; diff --git a/java-checks/src/test/files/checks/IndentationCheck_default.java b/java-checks/src/test/files/checks/IndentationCheck_default.java index 02cdb216e4a..33ad64f4ece 100644 --- a/java-checks/src/test/files/checks/IndentationCheck_default.java +++ b/java-checks/src/test/files/checks/IndentationCheck_default.java @@ -41,9 +41,9 @@ class Foo { void foo() { IntStream .range(1, 5) - .map(a -> { - return a + 1; // should be a valid position - }); + .map((a -> { + return a + 1; + })); IntStream .range(1, 5) .map(a -> { @@ -53,6 +53,9 @@ void foo() { a += 1; // Noncompliant {{Make this line start at column 9.}} return a + 1; }); + IntStream.range(1, 5).map(a -> { + return a + 1; + }); } } @@ -157,3 +160,35 @@ public static class Inner { public static final String FOO = "foo"; } } + +class IndentFoo { + public static boolean showJobDifferences(final String name, final JobsDifference.JobDifference diff, boolean onlyOkButton) { + FutureTask task = new FutureTask<>(() -> { + Optional result = new DifferenceDialog(name, diff, onlyOkButton).showAndWait(); + if (result.isPresent() && (result.get() == DifferenceDialog.ACTION_YES_ALL || result.get() == DifferenceDialog.ACTION_OK_ALL)) { + skipDialog = true; + } + return result.get() == DifferenceDialog.ACTION_YES || result.get() == DifferenceDialog.ACTION_YES_ALL; + }); + } + + @Override + protected void append(final ILoggingEvent event) { + synchronized (lock) { + Platform.runLater(() -> { + Text text = new Text(); + text.setText(patternLayout.doLayout(event)); + switch (event.getLevel().levelInt) { + case Level.DEBUG_INT: + text.setFill(Color.BLACK); + break; + default: + text.setFill(Color.BLACK); + break; + } + listView.getItems().add(text); + }); + } + } + +}