Skip to content

Commit be86e59

Browse files
authored
Fix NoEmptyLineSeparatorCheck false positives (#384)
The check would generate false positives on annotation interfaces because these also have "default" tokens. Examples are: * org.openhab.core.automation.annotation.ActionInput * org.openhab.binding.mqtt.generic.mapping.MQTTvalueTransform Signed-off-by: Wouter Born <github@maindrain.net>
1 parent be6c1de commit be86e59

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

custom-checks/checkstyle/src/main/java/org/openhab/tools/analysis/checkstyle/NoEmptyLineSeparatorCheck.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ private DetailAST findLeftCurly(DetailAST ast) {
205205
switch (ast.getType()) {
206206
case TokenTypes.LITERAL_CASE:
207207
case TokenTypes.LITERAL_DEFAULT: {
208+
if (ast.getParent().getType() == TokenTypes.ANNOTATION_FIELD_DEF) {
209+
// annotation field defaults don't have a curly
210+
return null;
211+
}
208212
DetailAST nextNode = ast.getParent().getNextSibling();
209213
return (nextNode != null) ? ast : null;
210214
}
@@ -223,6 +227,10 @@ private DetailAST findRightCurly(DetailAST ast) {
223227
switch (ast.getType()) {
224228
case TokenTypes.LITERAL_DEFAULT:
225229
case TokenTypes.LITERAL_CASE: {
230+
if (ast.getParent().getType() == TokenTypes.ANNOTATION_FIELD_DEF) {
231+
// annotation field defaults don't have a curly
232+
return null;
233+
}
226234
/*
227235
* cases are nested in case groups(a group of case clauses),
228236
* we are searching for the case in the next case group or

custom-checks/checkstyle/src/test/java/org/openhab/tools/analysis/checkstyle/test/NoEmptyLineSeparatorCheckTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,18 @@ public void verifyEmptyLineInCaseWithBraces() throws Exception {
228228
}
229229

230230
@Test
231-
public void verifyMutlitpleEmptyLinesInSwitchWithCases() throws Exception {
231+
public void verifyMultipleEmptyLinesInSwitchWithCases() throws Exception {
232232
String[] expectedMessages = generateExpectedMessages(12, MSG_LINE_AFTER_OPENING_BRACE_EMPTY, 29,
233233
MSG_LINE_BEFORE_CLOSING_BRACE_EMPTY, 16, MSG_LINE_BEFORE_CLOSING_BRACE_EMPTY, 19, MSG_FOR_EMPTY_LINE,
234234
27, MSG_LINE_BEFORE_CLOSING_BRACE_EMPTY);
235235
verifyJavaFile("MutlitpleEmptyLinesInSwitchWithCases.java", expectedMessages);
236236
}
237237

238+
@Test
239+
public void verifyValidAnnotationInterface() throws Exception {
240+
verifyJavaFileNoErrors("ValidAnnotationInterface.java");
241+
}
242+
238243
@Test
239244
public void verifyValidEmptyDefault() throws Exception {
240245
verifyJavaFileNoErrors("ValidEmptyDefaultDefinition.java");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import static java.lang.annotation.ElementType.FIELD;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface TestInterface {
11+
12+
String property1() default "";
13+
14+
String property2() default "";
15+
16+
String property2() default "";
17+
18+
}

0 commit comments

Comments
 (0)