Skip to content
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

Merged
merged 1 commit into from Dec 12, 2016

Conversation

kazachka
Copy link
Contributor

Issue #3426

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 4 times, most recently from 92224dc to fd94c60 Compare November 12, 2016 21:08
Copy link
Member

@rnveach rnveach left a 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) {
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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?

Copy link
Contributor Author

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();
Copy link
Member

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.

Copy link
Contributor Author

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) != '*') {
Copy link
Member

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 *?

Copy link
Contributor Author

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;
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 2 times, most recently from ccdc33b to 17f3cfc Compare November 13, 2016 12:24
@romani
Copy link
Member

romani commented Nov 13, 2016

@kazachka , please make sure that all CIs are green , I skimmed them and all of them are real problems.

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 2 times, most recently from e9e67f5 to d9e4e56 Compare November 15, 2016 21:00
@codecov-io
Copy link

codecov-io commented Nov 15, 2016

Current coverage is 100% (diff: 100%)

Merging #3549 into master will not change coverage

@@           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          

Powered by Codecov. Last update c2bbc97...2a32f8c

@kazachka
Copy link
Contributor Author

Done

Copy link
Member

@rnveach rnveach left a 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) != '*') {
Copy link
Member

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?

Copy link
Member

@romani romani Nov 18, 2016

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;
   }

Example:
https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/checks/TodoCommentCheck.java#L68

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.
Copy link
Member

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.

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 2 times, most recently from 496b04f to 12c6220 Compare November 18, 2016 18:53
@rnveach
Copy link
Member

rnveach commented Nov 22, 2016

@kazachka CI is failing.
I also don't believe we are expecting changes in JavadocDetailNodeParser.java.

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 2 times, most recently from fff7302 to f3618ab Compare November 26, 2016 18:53
@romani
Copy link
Member

romani commented Nov 30, 2016

@kazachka ,

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 - module-info.java
http://hg.openjdk.java.net/jdk9/dev/jdk/file/b2a69d66dc65/src/jdk.naming.rmi/share/classes/module-info.java , such files will be in java9.
module-info.java we are skipping as we do not have parser for them for now.

package-info.java is smth similar but match by grammar to java files so that is why we process them.
Smth that is possible in module-info.java in not possible in java files that contains class.

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 package-info.java files.
We need to mention this nuance in javadoc/xdoc.

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 2 times, most recently from 3e2b066 to 380705c Compare November 30, 2016 20:08
@kazachka
Copy link
Contributor Author

Update to allow to placing javadoc and annotations before PACKAGE_DEF only in package-info.java

@romani
Copy link
Member

romani commented Dec 1, 2016

@kazachka ,

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html

package-info.java - Can contain a package declaration, package annotations, package comments and Javadoc tags. This file is generally preferred over package.html.

So please remove "import" from package-info.java inputs.

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html

-Xpkginfo:[always,legacy,nonempty]
    Control whether javac generates package-info.class files from package-info.java files. Possible mode arguments for this option include the following.
    always
        Always generate a package-info.class file for every package-info.java file. This option may be useful if you use a build system such as Ant, which checks that each .java file has a corresponding .class file.
    legacy
        Generate a package-info.class file only if package-info.java contains annotations. Don't generate a package-info.class file if package-info.java only contains comments.
        Note: A package-info.class file might be generated but be empty if all the annotations in the package-info.java file have RetentionPolicy.SOURCE.
    nonempty
        Generate a package-info.class file only if package-info.java contains annotations with RetentionPolicy.CLASS or RetentionPolicy.RUNTIME.

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 package-info.java state that there is not violations are expected.

I reviewed changes for trailing comments removal in inputs .... I do not like this ...
Trailing comments should not be considered as some separation. Can we update Check to skip training comments?

@kazachka
Copy link
Contributor Author

kazachka commented Dec 2, 2016

Google Guava has import declarations for annotations in package-info.java, for example there:
https://github.com/google/guava/blob/master/guava/src/com/google/common/base/package-info.java

If I remove import declarations check will not validate PACKAGE_DEF because of this:

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 4 times, most recently from 7848d5c to 831949a Compare December 2, 2016 12:01
@kazachka
Copy link
Contributor Author

kazachka commented Dec 2, 2016

  1. and 3. done

Copy link
Member

@romani romani left a 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;
Copy link
Member

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.

Copy link
Contributor Author

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;
Copy link
Member

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.

Copy link
Contributor Author

@kazachka kazachka Dec 4, 2016

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

Copy link
Member

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.

Copy link
Contributor Author

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;
Copy link
Member

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)

Copy link
Contributor Author

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.

Copy link
Member

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.

@romani
Copy link
Member

romani commented Dec 4, 2016

please revert changes for usage of javax annotations, my bad.

@kazachka
Copy link
Contributor Author

kazachka commented Dec 4, 2016

updated

@kazachka
Copy link
Contributor Author

kazachka commented Dec 4, 2016

Is it good that check allow tokens not separated by line before PACKAGE_DEF with annotations?

@romani
Copy link
Member

romani commented Dec 4, 2016

Kind of OK.
According to grammar, onlt annotations and comments are posible to put. We cover them now.

@romani
Copy link
Member

romani commented Dec 4, 2016

Please privide diff report for several projects to make sure that we dud not damage anithing else

@kazachka kazachka force-pushed the fix-in-empty-line-separator branch 3 times, most recently from 4cf2233 to 9f8bcfa Compare December 5, 2016 18:17
@kazachka
Copy link
Contributor Author

kazachka commented Dec 5, 2016

isTrailingComment() updated.
Diff-report:
https://kazachka.github.io/empty-line-separator-diff-report/

@romani
Copy link
Member

romani commented Dec 5, 2016

home/zenigata/workspace/contribution/checkstyle-tester/src/main/java/apache-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/EjbJar.java Severity Rule Message Line Co warning EmptyLineSeparator '//' should be separated from previous statement. 126

This and similar are not expected to appear.

@kazachka
Copy link
Contributor Author

kazachka commented Dec 5, 2016

Which tokens shouldn't be separated from comments by line?

@romani
Copy link
Member

romani commented Dec 5, 2016

No separation in this case.
There was no such violation before update and there should be no such violation after your update.

@kazachka
Copy link
Contributor Author

kazachka commented Dec 9, 2016

added isTrailingCommentMethod to allow comments after all of tokens except of package and imports definitions.

@kazachka
Copy link
Contributor Author

kazachka commented Dec 9, 2016

@romani
Copy link
Member

romani commented Dec 9, 2016

@kazachka , both new violations(green) are not expected.
Please move them to Inputs and UTs.

@kazachka
Copy link
Contributor Author

@romani
Copy link
Member

romani commented Dec 11, 2016

report is ok, but I do not see in UTs and Inputs all cases where you had regression with false-positives.
Please add them to Inputs, if run into regression ones it could happen second time too.

@kazachka
Copy link
Contributor Author

I add more test cases for comments.

@romani romani merged commit 65fe91d into checkstyle:master Dec 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants