New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #3426: remove warning on PACKAGE_DEF predicted by javadoc not separated by line #3549
Issue #3426: remove warning on PACKAGE_DEF predicted by javadoc not separated by line #3549
Conversation
92224dc
to
fd94c60
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some requested changes and some questions to think about as you fix up code to pass CI.
CI is not happy about some style and code coverage.
* @param token token. | ||
* @return true, if token is predicted by javadoc comment. | ||
*/ | ||
private boolean isPredictedByJavadoc(DetailAST token) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does predicted
fit into what this method does?
I think the correct word here is preceded
since we are checking if the javadoc comes before (precedes) the token.
Please update name and javadoc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
@@ -534,6 +534,37 @@ private boolean hasEmptyLineBefore(DetailAST token) { | |||
} | |||
|
|||
/** | |||
* Check if token is predicted by javadoc comment | |||
* @param token token. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get a slightly better description than just duplicating the parameter name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
String lineBefore; | ||
int lineIndex = 2; | ||
do { | ||
lineBefore = getLines()[lineNo - lineIndex].trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just make lineIndex
the value of lineNo
- 2 and decrement each loop. This will save us from having to redo this calculation each time through, twice.
Also please stash the result of getLines
as we do a clone on the values each call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
else if (lineBefore.length() == 0) { | ||
break; | ||
} | ||
else if (lineBefore.charAt(0) != '*') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the user has a space/tab before the *
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this case we use trim:
lineBefore = lines[lineIndex].trim();
do { | ||
lineBefore = getLines()[lineNo - lineIndex].trim(); | ||
if (lineBefore.startsWith("/**")) { | ||
result = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we know the result is true
, then why don't we break out now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
ccdc33b
to
17f3cfc
Compare
@kazachka , please make sure that all CIs are green , I skimmed them and all of them are real problems. |
e9e67f5
to
d9e4e56
Compare
Current coverage is 100% (diff: 100%)@@ master #3549 diff @@
=====================================
Files 275 275
Lines 13335 13352 +17
Methods 0 0
Messages 0 0
Branches 3039 3047 +8
=====================================
+ Hits 13335 13352 +17
Misses 0 0
Partials 0 0
|
d9e4e56
to
4202c76
Compare
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor changes.
Once these are made, I am fine with the code.
final String[] lines = getLines(); | ||
String lineBefore = lines[lineIndex].trim(); | ||
while (!lineBefore.startsWith("/**")) { | ||
if (lineIndex == 0 || !lineBefore.isEmpty() && lineBefore.charAt(0) != '*') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken from: http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html#packagecomment
Note that while the comment separators /** and /* must be present, the leading asterisks
on the intermediate lines can be omitted.
Taken from: http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html#leadingasterisks
Leading asterisks - When javadoc parses a doc comment, leading asterisk (*) characters on
each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also
discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white
space is no longer removed. This enables you to paste code examples directly into a doc
comment inside a <PRE> tag, and its indentation will be honored. Spaces are generally
interpreted by browsers more uniformly than tabs. Indentation is relative to the left
margin (rather than the separator /** or <PRE> tag).
We should accept Javadocs with no leading asterisks. If they omit the leading asterisk, your current code will fail thinking it is not part of the Javadoc as your lineBefore.charAt(0) != '*'
is looking for the leading asterisk.
Since we are looking above the package
it can't be another node as there are no valid nodes that go above the package. We can only run into Javadocs or Comments.
I think we should change the leading asterisk check to a leading comment check (//
or /*
).
We should also have a test for this.
@romani Do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we have to support both approaches to write javadoc.
current implementations is fragile by design, as this Check is not comments aware - https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java
to receive all comments well parsed Check need to override method
@Override
public boolean isCommentNodesRequired() {
return true;
}
in this case Check will have all comments in good form.
Useful method - https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/utils/JavadocUtils.java#L246
I will write a bit more about isCommentNodesRequired in scope of #3561
@@ -534,6 +534,30 @@ private boolean hasEmptyLineBefore(DetailAST token) { | |||
} | |||
|
|||
/** | |||
* Check if token is preceded by javadoc comment | |||
* @param token token for check. | |||
* @return true, if token is predicted by javadoc comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed renaming to preceded
.
496b04f
to
12c6220
Compare
@kazachka CI is failing. |
fff7302
to
f3618ab
Compare
CI is failed at - https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/package-info.java package-info.java are not ordinary java files. Here is example of similar problem -
Resolution: we have special files that we need to process separately in this Check. There is an ability to get name of the file in the Check, so pelase check it and make logic to allow no space for javadoc and annotations in |
3e2b066
to
380705c
Compare
Update to allow to placing javadoc and annotations before PACKAGE_DEF only in package-info.java |
http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html
So please remove "import" from https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html
you can see fact that it is java file is actually cause nuances(compatibility modes for other tools like ANT :) ) for javac too. Please in comments over packages in I reviewed changes for trailing comments removal in inputs .... I do not like this ... |
Google Guava has import declarations for annotations in package-info.java, for example there: If I remove import declarations check will not validate PACKAGE_DEF because of this: Line 289 in 25a37e5
|
7848d5c
to
831949a
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more items to fix
@@ -0,0 +1,5 @@ | |||
/*OK: for test allowing to place annotation before PACKAGE_DEF.*/ | |||
@Deprecated | |||
package com.puppycrawl.tools.checkstyle.checks.whitespace.test1.packageinfo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
com.puppycrawl.tools.checkstyle.checks.whitespace.test1.packageinfo
vs
com/puppycrawl/tools/checkstyle/checks/whitespace/package-info/test1
path should be the same as package - "com/puppycrawl/tools/checkstyle/checks/whitespace/packageinfo/test1", or package the same path.
Please fix other cases too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@Deprecated | ||
package com.puppycrawl.tools.checkstyle.checks.whitespace.test1.packageinfo; | ||
|
||
import java.lang.Deprecated; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please do import of the same annotations(2 annotations) as in Guava file that I/you mentioned above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, but tests aren't compile, it needs to import javax.annotation package in pom.xml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, ... adding new dependency only for Inputs is too much.
I am ok to keep usage of Deprecated only. Looks like it is only annotation that could be applied to packages in jdk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/**OK: for test allowing to place javadoc before PACKAGE_DEF.*/ | ||
package com.puppycrawl.tools.checkstyle.checks.whitespace.test2.packageinfo; | ||
|
||
import java.lang.System; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what for we have import here ? (the same question to all other Input files)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EmptyLineSeparatorCheck doesn't check last token. I place import after PACKAGE_DEF to make package declaration no last.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please put this nuance as a comment in Input file.
4390076
to
5e5e932
Compare
please revert changes for usage of javax annotations, my bad. |
5e5e932
to
92fe995
Compare
updated |
Is it good that check allow tokens not separated by line before PACKAGE_DEF with annotations? |
Kind of OK. |
Please privide diff report for several projects to make sure that we dud not damage anithing else |
4cf2233
to
9f8bcfa
Compare
isTrailingComment() updated. |
This and similar are not expected to appear. |
Which tokens shouldn't be separated from comments by line? |
No separation in this case. |
9f8bcfa
to
8ae8603
Compare
added isTrailingCommentMethod to allow comments after all of tokens except of package and imports definitions. |
8ae8603
to
b0eaddd
Compare
@kazachka , both new violations(green) are not expected. |
b0eaddd
to
4a57c96
Compare
Updated. |
report is ok, but I do not see in UTs and Inputs all cases where you had regression with false-positives. |
4a57c96
to
cc84ae7
Compare
…doc not separated by line
cc84ae7
to
2a32f8c
Compare
I add more test cases for comments. |
Issue #3426