From 001d9cc2131faaadb4e6fd13e2bb8fef6be09f60 Mon Sep 17 00:00:00 2001 From: Roman Ivanov Date: Thu, 24 Apr 2014 22:35:19 -0700 Subject: [PATCH] issue #129: make a handler for INDEX_OP so child method call gets a proper suggested indent level --- .../checks/indentation/HandlerFactory.java | 1 + .../checks/indentation/IndexHandler.java | 58 +++++++++++++++++++ .../indentation/IndentationCheckTest.java | 1 + .../indentation/InputInvalidMethodIndent.java | 8 +++ .../indentation/InputValidMethodIndent.java | 8 +++ 5 files changed, 76 insertions(+) create mode 100644 src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndexHandler.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java index 644595c94..f0915ab60 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java @@ -118,6 +118,7 @@ public class HandlerFactory register(TokenTypes.BOR_ASSIGN, AssignHandler.class); register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class); register(TokenTypes.LITERAL_NEW, NewHandler.class); + register(TokenTypes.INDEX_OP, IndexHandler.class); } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndexHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndexHandler.java new file mode 100644 index 000000000..56fc4ee7a --- /dev/null +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndexHandler.java @@ -0,0 +1,58 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2014 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks.indentation; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; + +/** + * Handler for array index operation. + * + * @author yudindi + */ +public class IndexHandler extends ExpressionHandler +{ + + /** + * Construct an instance of this handler with the given indentation check, + * abstract syntax tree, and parent handler. + * + * @param aIndentCheck the indentation check + * @param aAST the abstract syntax tree + * @param aParent the parent handler + */ + public IndexHandler(IndentationCheck aIndentCheck, + DetailAST aAST, + ExpressionHandler aParent) + { + super(aIndentCheck, "index op", aAST, aParent); + } + + @Override + public void checkIndentation() + { + // do nothing. Used to provide a correct suggested child level for now. + } + + @Override + public IndentLevel suggestedChildLevel(ExpressionHandler aChild) + { + return getLevel(); + } + +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index 144571251..76c95d7a6 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -134,6 +134,7 @@ public class IndentationCheckTest extends BaseCheckTestSupport "164: method def child at indentation level 4 not at correct indentation, 8", "169: method def child at indentation level 4 not at correct indentation, 8", "173: method def return type at indentation level 0 not at correct indentation, 4", + "184: method def child at indentation level 12 not at correct indentation, 8", }; verify(c, fname, expected); } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputInvalidMethodIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputInvalidMethodIndent.java index 2f303e965..5b8bf2003 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputInvalidMethodIndent.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputInvalidMethodIndent.java @@ -175,4 +175,12 @@ int[] { return null; } + + private int[] getArray() { + return new int[] {1}; + } + + private void indexTest() { + getArray()[0] = 2; + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidMethodIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidMethodIndent.java index 83772bf8c..a6be2392d 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidMethodIndent.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidMethodIndent.java @@ -181,5 +181,13 @@ public class InputValidMethodIndent extends java.awt.event.MouseAdapter implemen } + + private int[] getArray() { + return new int[] {1}; + } + + private void indexTest() { + getArray()[0] = 2; + } }