Skip to content

Commit cc3ada6

Browse files
Anmol202005romani
authored andcommitted
Issue #15161: Resolved False Negative
1 parent e4b625a commit cc3ada6

8 files changed

Lines changed: 146 additions & 25 deletions

File tree

src/main/java/com/puppycrawl/tools/checkstyle/checks/annotation/AnnotationOnSameLineCheck.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,15 @@ public void visitToken(DetailAST ast) {
135135
annotationNode != null;
136136
annotationNode = annotationNode.getNextSibling()) {
137137
if (annotationNode.getType() == TokenTypes.ANNOTATION
138-
&& !TokenUtil.areOnSameLine(annotationNode, getNextNode(annotationNode))) {
138+
&& !TokenUtil.areOnSameLine(annotationNode,
139+
modifiersNode.getNextSibling())) {
139140
log(annotationNode, MSG_KEY_ANNOTATION_ON_SAME_LINE,
140141
getAnnotationName(annotationNode));
141142
}
142143
}
143144
}
144145
}
145146

146-
/**
147-
* Finds next node of ast tree.
148-
*
149-
* @param node current node
150-
* @return node that is next to given
151-
*/
152-
private static DetailAST getNextNode(DetailAST node) {
153-
DetailAST nextNode = node.getNextSibling();
154-
if (nextNode == null) {
155-
nextNode = node.getParent().getNextSibling();
156-
}
157-
return nextNode;
158-
}
159-
160147
/**
161148
* Returns the name of the given annotation.
162149
*

src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/AnnotationOnSameLineCheckTest.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public void testAnnotationOnSameLineCheckPublicMethodAndVariable() throws Except
7878
"20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
7979
"21:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
8080
"22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
81-
"28:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
81+
"30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation2"),
82+
"30:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
8283
};
8384
verifyWithInlineConfigParser(
8485
getPath("InputAnnotationOnSameLineCheckPublicMethodAndVariable.java"), expected);
@@ -90,7 +91,8 @@ public void testAnnotationOnSameLineCheckTokensOnMethodAndVar() throws Exception
9091
"20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
9192
"21:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
9293
"22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
93-
"28:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
94+
"30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation4"),
95+
"30:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
9496
};
9597
verifyWithInlineConfigParser(
9698
getPath("InputAnnotationOnSameLineCheckTokensOnMethodAndVar.java"), expected);
@@ -140,9 +142,10 @@ public void testAnnotationOnSameLineRecordsAndCompactCtors() throws Exception {
140142
"24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
141143
"29:9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
142144
"35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
143-
"38:27: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
144-
"49:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
145-
"56:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
145+
"41:13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "NonNull1"),
146+
"41:23: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
147+
"51:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
148+
"58:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
146149
};
147150
verifyWithInlineConfigParser(
148151
getNonCompilablePath("InputAnnotationOnSameLineRecordsAndCompactCtors.java"),
@@ -159,4 +162,29 @@ public void testAnnotationOnSameLinePatternVariables() throws Exception {
159162
expected);
160163
}
161164

165+
@Test
166+
public void testAnnotationOnSameLine() throws Exception {
167+
final String[] expected = {
168+
"19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
169+
"23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
170+
"36:14: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
171+
"43:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
172+
"43:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
173+
};
174+
verifyWithInlineConfigParser(
175+
getPath("InputAnnotationOnSameLine.java"), expected);
176+
}
177+
178+
@Test
179+
public void testAnnotationOnSameLine2() throws Exception {
180+
final String[] expected = {
181+
"18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
182+
"22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
183+
"26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
184+
"30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
185+
};
186+
verifyWithInlineConfigParser(
187+
getNonCompilablePath("InputAnnotationOnSameLine.java"),
188+
expected);
189+
}
162190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
AnnotationOnSameLine
3+
tokens = (default)CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, \
4+
VARIABLE_DEF, RECORD_DEF, COMPACT_CTOR_DEF
5+
6+
7+
*/
8+
9+
//non-compiled with javac: Compilable with Java17
10+
package com.puppycrawl.tools.checkstyle.checks.annotation.annotationonsameline;
11+
12+
import java.lang.annotation.Target;
13+
14+
import static java.lang.annotation.ElementType.TYPE_USE;
15+
16+
public class InputAnnotationOnSameLine {
17+
// violation below, 'Annotation 'Ann' should be on the same line with its target.'
18+
@Ann public
19+
@Ann2 class E {}
20+
21+
// violation below, 'Annotation 'Ann' should be on the same line with its target.'
22+
@Ann private
23+
@Ann2 class A {}
24+
25+
// violation below, 'Annotation 'Ann' should be on the same line with its target.'
26+
@Ann sealed
27+
@Ann2 class B permits C {}
28+
29+
// violation below, 'Annotation 'Ann' should be on the same line with its target.'
30+
@Ann final
31+
@Ann2 class C extends B {}
32+
33+
@Ann public @Ann2 class Egood {}
34+
35+
@Ann private @Ann2 class Agood {}
36+
37+
@Ann sealed @Ann2 class Bgood permits Cgood {}
38+
39+
@Ann final @Ann2 class Cgood extends Bgood {}
40+
41+
@Target({TYPE_USE}) @interface Ann {}
42+
@Target({TYPE_USE}) @interface Ann2 {}
43+
}

src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline/InputAnnotationOnSameLineRecordsAndCompactCtors.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ record MyRecord3() {
3535
@SuppressWarnings("deprecation")
3636
public record MyRecord5() {
3737
record MyInnerRecord() {
38-
@NonNull1 @SuppressWarnings("Annotation")
39-
// violation above, "Annotation 'SuppressWarnings' should be on the same line with its target."
38+
// 2 violations 3 lines below:
39+
// "Annotation 'NonNull1' should be on the same line with its target."
40+
// "Annotation 'SuppressWarnings' should be on the same line with its target."
41+
@NonNull1 @SuppressWarnings("Annotation")
4042
public MyInnerRecord {
4143
}
4244
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
AnnotationOnSameLine
3+
tokens = CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, \
4+
VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, LITERAL_THROWS, \
5+
IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, ANNOTATION_FIELD_DEF
6+
7+
8+
*/
9+
10+
package com.puppycrawl.tools.checkstyle.checks.annotation.annotationonsameline;
11+
12+
import static java.lang.annotation.ElementType.TYPE_USE;
13+
14+
import java.lang.annotation.Target;
15+
import java.util.List;
16+
17+
public class InputAnnotationOnSameLine {
18+
// violation below, "Annotation 'Ann' should be on the same line with its target."
19+
@Ann public
20+
@Ann2 class E {}
21+
22+
// violation below, "Annotation 'Ann' should be on the same line with its target."
23+
@Ann private
24+
@Ann2 class A {}
25+
public void wildcardCase() {
26+
List<@Ann ?> list;
27+
}
28+
29+
public String typeCastCase() {
30+
Object s = new String();
31+
return (@Ann String) s;
32+
}
33+
34+
public void wildcardCase1() {
35+
// violation below, 'Annotation 'Ann' should be on the same line with its target.'
36+
List<@Ann
37+
?> list;
38+
}
39+
40+
// 2 violations 3 lines below:
41+
// "Annotation 'Deprecated' should be on the same line with its target."
42+
// "Annotation 'SuppressWarnings' should be on the same line with its target."
43+
@Deprecated @SuppressWarnings("unchecked")
44+
public void proceed(int parameter1, int parameter2, int parameter3,
45+
int parameter4, int parameter5) { }
46+
47+
@Deprecated @SuppressWarnings("unchecked") public void proceedGood(int parameter1,
48+
int parameter2, int parameter3,
49+
int parameter4, int parameter5) { }
50+
51+
int[][][] i = new @Ann3(integer = 1) int[0][][];
52+
53+
@Target({TYPE_USE}) @interface Ann {}
54+
@Target({TYPE_USE}) @interface Ann2 {}
55+
@Target({TYPE_USE}) @interface Ann3 { int integer();}
56+
57+
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline/InputAnnotationOnSameLineCheckInterfaceAndEnum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
@Ann2 Integer) x;
5858
}
5959

60-
public <T> T getXAs() {
60+
public <@Ann T> T getXAs() {
6161
return (T) x;
6262
}
6363
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline/InputAnnotationOnSameLineCheckPublicMethodAndVariable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public int getX() {
2424
return (int) x;
2525
}
2626

27-
// violation below, "Annotation 'Annotation' should be on the same line with its target."
27+
// 2 violations 3 lines below:
28+
// 'Annotation 'Annotation2' should be on the same line with its target.'
29+
// 'Annotation 'Annotation' should be on the same line with its target.'
2830
@Annotation2 @Annotation
2931
public int field;
3032

src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline/InputAnnotationOnSameLineCheckTokensOnMethodAndVar.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public int getX() {
2424
return (int) x;
2525
}
2626

27-
// violation below, "Annotation 'Annotation3' should be on the same line with its target."
27+
// 2 violations 3 lines below:
28+
// 'Annotation 'Annotation4' should be on the same line with its target.'
29+
// 'Annotation 'Annotation3' should be on the same line with its target.'
2830
@Annotation4 @Annotation3
2931
public int field;
3032

0 commit comments

Comments
 (0)