aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBBG <djydewang@gmail.com>2017-12-03 23:43:01 +0800
committerrnveach <rveach02@gmail.com>2017-12-16 11:49:23 -0500
commitb02eed8a546c711c74566afb4c27366b4a185a03 (patch)
tree8fccbb56d76b14af2b9a455356fa9bacbfd52f7e
parentff742454d77d81ed67212e794b0bd89ce9c8ed82 (diff)
downloadcheckstyle-b02eed8a546c711c74566afb4c27366b4a185a03.tar.gz
Issue #5154: Incorrect indentation check for method preceded by annotation, with method parameter on separate line
-rw-r--r--src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java3
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java33
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationAnnotationIncorrect.java24
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java20
4 files changed, 79 insertions, 1 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java
index f8af0a778..39f2b9ce6 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java
@@ -262,7 +262,8 @@ public class LineWrappingHandler {
if (isCurrentNodeCloseAnnotationAloneInLine
|| node.getType() == TokenTypes.AT
&& (parentNode.getParent().getType() == TokenTypes.MODIFIERS
- || parentNode.getParent().getType() == TokenTypes.ANNOTATIONS)) {
+ || parentNode.getParent().getType() == TokenTypes.ANNOTATIONS)
+ || node.getLineNo() == atNode.getLineNo()) {
logWarningMessage(node, firstNodeIndent);
}
else {
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 4b30162e0..91606fe7e 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
@@ -1746,6 +1746,39 @@ public class IndentationCheckTest extends AbstractModuleTestSupport {
}
@Test
+ public void testMethodPrecedeByAnnotationsWithParameterOnSeparateLine() throws Exception {
+ final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
+ checkConfig.addAttribute("tabWidth", "4");
+ checkConfig.addAttribute("basicOffset", "2");
+ checkConfig.addAttribute("braceAdjustment", "0");
+ checkConfig.addAttribute("caseIndent", "2");
+ checkConfig.addAttribute("throwsIndent", "4");
+ checkConfig.addAttribute("lineWrappingIndentation", "4");
+ checkConfig.addAttribute("arrayInitIndent", "2");
+ final String fileName =
+ getPath("InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java");
+ final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
+ verify(checkConfig, fileName, expected);
+ }
+
+ @Test
+ public void testAnnotationIncorrect() throws Exception {
+ final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
+ checkConfig.addAttribute("tabWidth", "4");
+ checkConfig.addAttribute("basicOffset", "4");
+ checkConfig.addAttribute("braceAdjustment", "0");
+ checkConfig.addAttribute("lineWrappingIndentation", "4");
+ final String fileName =
+ getPath("InputIndentationAnnotationIncorrect.java");
+ final String[] expected = {
+ "11: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
+ "14: " + getCheckMessage(MSG_ERROR, "(", 8, 12),
+ "19: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
+ };
+ verify(checkConfig, fileName, expected);
+ }
+
+ @Test
public void testInputAnnotationScopeIndentationCheck() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
checkConfig.addAttribute("tabWidth", "4");
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationAnnotationIncorrect.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationAnnotationIncorrect.java
new file mode 100644
index 000000000..20d878423
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationAnnotationIncorrect.java
@@ -0,0 +1,24 @@
+package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0
+
+class InpuIndentationAnnotationIncorrect { //indent:0 exp:0
+
+ public @interface MyAnnotation1 { //indent:4 exp:4
+ String value(); //indent:8 exp:8
+ } //indent:4 exp:4
+
+ @MyAnnotation2 //indent:4 exp:4
+ @MyAnnotation1 //indent:4 exp:4
+ (value = "") //indent:4 exp:8
+ class innerClass { //indent:4 exp:4
+ @MyAnnotation2 @MyAnnotation1 //indent:8 exp:8
+ (value = "") //indent:8 exp:12
+ public int a; //indent:8 exp:8
+ } //indent:4 exp:4
+
+ @MyAnnotation2 @MyAnnotation1 //indent:4 exp:4
+ (value = "") //indent:4 exp:8
+ class InputIndentationAnnotationInnerClass { //indent:4 exp:4
+
+ } //indent:4 exp:4
+} //indent:0 exp:0
+
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java
new file mode 100644
index 000000000..b0ea5d2de
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java
@@ -0,0 +1,20 @@
+package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0
+
+public class InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine { //indent:0 exp:0
+
+ @interface Annotation1 { //indent:2 exp:2
+ String field1(); //indent:4 exp:4
+ String field2(); //indent:4 exp:4
+ } //indent:2 exp:2
+
+ @interface Annotation2 {}; //indent:2 exp:2
+
+ @Annotation1(field1 = "foo", field2 = "bar") //indent:2 exp:2
+ public @Annotation2 String method( //indent:2 exp:2
+ String param //indent:6 exp:6
+ ) { //indent:2 exp:2
+ return null; //indent:4 exp:4
+ } //indent:2 exp:2
+
+} //indent:0 exp:0
+