Skip to content

Commit 3a3e69d

Browse files
committed
Relax NoWhitespaceBeforeCheck to allow annotated varags
Closes gh-454
1 parent 2f512ea commit 3a3e69d

File tree

10 files changed

+170
-1
lines changed

10 files changed

+170
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2017-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.javaformat.checkstyle.check;
18+
19+
import com.puppycrawl.tools.checkstyle.api.DetailAST;
20+
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
21+
import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck;
22+
23+
/**
24+
* Spring-specific customization of {@link NoWhitespaceBeforeCheck} that permits
25+
* whitespace before {@code ...} when it is a separator after an annotation, for
26+
* example {@code int @Nullable ...}.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
public class SpringNoWhitespaceBeforeCheck extends NoWhitespaceBeforeCheck {
31+
32+
@Override
33+
public void visitToken(DetailAST ast) {
34+
if (ast.getType() != TokenTypes.ELLIPSIS) {
35+
super.visitToken(ast);
36+
}
37+
else {
38+
visitEllipsis(ast);
39+
}
40+
}
41+
42+
private void visitEllipsis(DetailAST ellipsis) {
43+
DetailAST previousSibling = ellipsis.getPreviousSibling();
44+
if (previousSibling.getType() == TokenTypes.TYPE &&
45+
previousSibling.getChildCount() == 2 &&
46+
previousSibling.getLastChild().getType() == TokenTypes.ANNOTATIONS) {
47+
return;
48+
}
49+
super.visitToken(ellipsis);
50+
}
51+
52+
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck" >
146146
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS, ARRAY_DECLARATOR"/>
147147
</module>
148-
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck" />
148+
<module name="io.spring.javaformat.checkstyle.check.SpringNoWhitespaceBeforeCheck" />
149149
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.TypecastParenPadCheck" />
150150
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck" />
151151
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
+WhitespaceVarargs.java:24:24: '...' is preceded with whitespace. [SpringNoWhitespaceBefore]
2+
+1 error
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Whitespace is expected before {@code []} when @Nullable.
19+
*
20+
* @author Andy Wilkinson
21+
*/
22+
public class WhitespaceNullableArray {
23+
24+
void bytes(int @Nullable [] elements) {
25+
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Whitespace is expected before {@code type[]} when @Nullable.
19+
*
20+
* @author Andy Wilkinson
21+
*/
22+
public class WhitespaceNullableArrayElements {
23+
24+
void bytes(@Nullable int[] elements) {
25+
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Whitespace is expected before ... when @Nullable.
19+
*
20+
* @author Andy Wilkinson
21+
*/
22+
public class WhitespaceNullableVarargs {
23+
24+
void bytes(int @Nullable ... elements) {
25+
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Whitespace is not expected before {@code ...}.
19+
*
20+
* @author Andy Wilkinson
21+
*/
22+
public class WhitespaceVarargs {
23+
24+
void bytes(int ... elements) {
25+
26+
}
27+
28+
}

0 commit comments

Comments
 (0)