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 #4714: Make SuppressionCommentFilter and SuppressWithNearbyCommentFilter children of TreeWalker #4755
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4755 +/- ##
======================================
Coverage 100% 100%
======================================
Files 287 288 +1
Lines 15396 15432 +36
Branches 3499 3504 +5
======================================
+ Hits 15396 15432 +36
Continue to review full report at Codecov.
|
From wercker.
Doing this in one change is going to be hard, and probably impossible thanks to wercker and all the regression we do. This change can't be made to other projects until we release this newest code. @MEZk I recommend we split this PR into 2. @romani To keep this class backwards compatible should we have them implement |
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.
ModuleReflectionUtils
needs to be updated to recognize the new filters.
checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/utils/ModuleReflectionUtils.java
Line 75 in ddeb553
|| isFilterModule(clazz) |
Yes, this PR will completely break compatibility with previous versions. @romani This is what we intend to do.
No, we decided to break compatibility.
Yes, but we should do this. @romani As far as I remember we decided not to split this PR.
Why do we need to introduce just interface and a wrapper (TreeWalkerAuditEvent) ? What for? They do not contain logic. |
I meant all logic too except for backward breaking stuff.
I will make a suggestion in gitter on this. If we are breaking, we might as well disable all of wercker. |
We could make transition without braking compatibility:
Disadvantage: but we will end up with new Filter names and there still CI related changes but smooth. Braking compatibility:
Good: names are the same,CI is always green. I do prefer to follow "Braking compatibility" as it will speedup acceptance of fixes and will keep names and will be the same efforts on CI update. |
@timurt , while we try to persuade @rnveach , please prepare separate commit in this PR to substitute all configs in affected projects with file from your gist. |
@romani If you are referring to breaking compatibility I am fine with that. I assumed we would want to try to avoid breaking compatibility like we have in other PRs. |
Why aren't we doing it from a location all of us can access? If we restrict access to one person, it will cause a conflict if they happen to go away when an emergency fix is needed. |
@timurt |
just try to economize time, as release is planned to this weekend, so that location is very temporal. |
@timurt |
in wercker: |
config/checkstyle_checks.xml
Outdated
@@ -155,6 +141,20 @@ | |||
value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection" | |||
/> | |||
</module> | |||
<module name="SuppressionCommentFilter"> |
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.
These are filters not annotations as the header on line 122 suggest.
Make a new header for filters inside TreeWalker
and move these lines right under it.
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 also make sure all headers are in alphabetical order.
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
final DetailAST rootAST = parse(contents); | ||
|
||
if (!ordinaryChecks.isEmpty()) { | ||
walk(rootAST, contents, AstState.ORDINARY); | ||
} | ||
if (!commentChecks.isEmpty()) { | ||
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST); | ||
|
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.
restore empty line.
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
@rnveach |
We should have access to the |
@timurt , |
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.
item to improve:
wercker.yml
Outdated
@@ -46,7 +46,10 @@ build: | |||
CS_POM_VERSION=$(mvn -q -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec) | |||
echo CS_version: ${CS_POM_VERSION} | |||
for i in 1 2 3 4 5; do git clone https://github.com/pgjdbc/pgjdbc.git && break || sleep 15; done | |||
cd pgjdbc/pgjdbc | |||
cd pgjdbc/pgjdbc/src/main/checkstyle |
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 avoid change of this line cd ....
, HACK lines(wget+patch) should not affect other lines to be easily removed.
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
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 change
* | ||
* @author Timur Tibeyev. | ||
*/ | ||
public interface AstFilter { |
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.
@timurt , @MEZk
I do not remember that I allowed new classes in API.
We discussed to keep then out of API till we finish implementation, to make sure that tested that contract and we could keep it.
One more point is names of interfaces: AstFilter vs TreeWalkerAuditEvent .
it is better to name filter as TreeWalkerFilter.
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.
- AstFilter should be outside API package.
- If somebody implements the check which is able to work with AST like TreeWalker, the filter will be available to use in that check. Why should we name it TreeWalker* ?
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.
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 I have to choose between the 2, I currently like TreeWalker*
better since it is connected with that module and Java compiling.
I dislike Ast
slightly as Javadoc is a form of AST even though we call it Node
. If Java upgrades to ANTLR4, will we rename these classes accordingly too and break previous compatibility? And how will we name them differently so not to confuse with Javadoc as both will be nodes?
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.
agree with rnveach.
additional point is that TreeWalkerAuditEvent will contain not only Ast , but some other context from TreeWalker.
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.
AstFilter
AstAuditEvent
Maybe we should call them
CheckerFilter
CheckerAuditEvent
because they are children of module Checker
, similarly to TreeWalkerFilter
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.
because they are children of module Checker, similarly to TreeWalkerFilter
it is already too late, with such a change you will brake whole word of Checkstyle.
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 would also vote for TreeWalkerAuditEvent
since it is produced by TreeWalker
and, therefore, for TreeWalkerFilter
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 dislike Ast slightly as Javadoc is a form of AST even though we call it Node. If Java upgrades to ANTLR4, will we rename these classes accordingly too and break previous compatibility? And how will we name them differently so not to confuse with Javadoc as both will be nodes?
Ok, lets name them as TreeWalkerFilter and TreeWalkerAuditEvent.
@timurt
Please, do.
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
9082055
to
6ff7c2c
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.
We removed FileContentsHolder
from filters but documentation in xdoc still makes reference to FileContentsHolder
.
See http://checkstyle.sourceforge.net/config_filters.html#SuppressWithNearbyCommentFilter and http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter in Description
and Examples
.
XDoc needs to be updated for all affected filters.
mvn -s settings-example.xml clean install -DskipTests=true -Dtest.elasticsearch.host.provided=true | ||
wget https://gist.githubusercontent.com/timurt/f8e6c08b681337dfee76fdf8cc4ebb37/raw/c95333027111d055de8c943d3bc0b757ad638605/hibernate.patch | ||
git apply hibernate.patch | ||
mvn -s settings-example.xml clean install -DskipTests=true -Dtest.elasticsearch.host.provided=true -Dpuppycrawl.checkstyle.version=${CS_POM_VERSION} |
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.
@romani Looks like we had a bug with hibernate
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.
@rnveach , what do you mean by this ?
@Override | ||
public void setupChild(Configuration childConf) | ||
throws CheckstyleException { | ||
final String name = childConf.getName(); | ||
final Object module = moduleFactory.createModule(name); | ||
if (!(module instanceof AbstractCheck)) { | ||
if (module instanceof AbstractCheck) { | ||
final AutomaticBean bean = (AutomaticBean) module; |
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.
@timurt
Why do we need to treat AbstractCheck as AutomaticBean directly? Please, restore original actions:
final AbstractCheck check = (AbstractCheck) module;
check.contextualize(childContext);
check.configure(childConf);
check.init();
registerCheck(check);
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 again this TC violation
175: setupChild() Cast (AutomaticBean) module conflicts with surrounding 'instanceof' check
Should I suppress it?
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.
resolved, TeamCity passed
"TreeWalker is not allowed as a parent of " + name | ||
+ " Please review 'Parent Module' section for this Check in web" | ||
+ " documentation if Check is standard."); | ||
"TreeWalker is not allowed as a parent of " + 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.
@timurt
Do not ignore this point
@@ -169,10 +188,11 @@ protected void processFiltered(File file, FileText fileText) throws CheckstyleEx | |||
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) { | |||
final String msg = "%s occurred during the analysis of file %s."; | |||
final String fileName = file.getPath(); | |||
final FileContents contents = new FileContents(fileText); | |||
|
|||
try { | |||
if (!ordinaryChecks.isEmpty() |
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.
@timurt
So, FileContext holder position should be restored. It should be closer to its first usage.
} | ||
|
||
final String[] expected = { | ||
"2:16: Name 'I' must match pattern '^[a-z][a-zA-Z0-9]*$'.", |
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.
Use getCheckMessage, do not hardcode violation message in the test.
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
@timurt |
9735983
to
ea45b23
Compare
* | ||
* @author Timur Tibeyev. | ||
*/ | ||
@FunctionalInterface |
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 declare functional interface. but do not use lambdas. Please, remove. It is not necessary. Future contributor will not understand your intention.
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.
@MEZk
Done
Added noinspection
javadoc because TeamCity was failing with violation:
27: TreeWalkerFilter Interface TreeWalkerFilter may be annotated with @FunctionalInterface
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.
@rnveach
Please explain why do we need the annotation ?
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.
annotation is allowed here: #3435 (comment)
a469677
to
4fad17a
Compare
* An interface for filtering {@code TreeWalkerAuditEvent}. | ||
* | ||
* @author Timur Tibeyev. | ||
* @noinspection InterfaceMayBeAnnotatedFunctional |
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 #4755 (comment) ,
remove suppression and add annotation.
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
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 explain why do we need the annotation ?
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.
@@ -144,23 +155,35 @@ public void finishLocalSetup() { | |||
childContext = checkContext; | |||
} | |||
|
|||
/** | |||
* {@inheritDoc} Creates child module. | |||
* @noinspection ChainOfInstanceofChecks |
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 do we need this suppression here when Checker, which has similar code doesn't need it?
https://github.com/rnveach/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java#L436
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.
@rnveach
Your example from your forked repo, inside main repo this suppression exists
https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java#L442
Otherwise TeamCity fails
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.
Sorry, I used the wrong link to my own fork.
You are correct and can ignore this.
@Override | ||
public void setupChild(Configuration childConf) | ||
throws CheckstyleException { | ||
final String name = childConf.getName(); | ||
final Object module = moduleFactory.createModule(name); | ||
if (!(module instanceof AbstractCheck)) { | ||
if (module instanceof AutomaticBean) { |
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.
Should this be surrounded by a try/catch like Checker?
https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java#L450-L462
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.
@rnveach
I think moduleFactory.createModule(name);
is where exception probably could happen, but i was not surrounded by try/catch before. Should I do it 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.
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.
createModule already put in exception all required context for user - https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java#L183
but other methods contextualize
and configure
will not provide context of which module was in reflection initialization process.
@timurt , please add try-catch.
UPDATE: moved to #4814
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.
@romani @rnveach @MEZk
As I understood, TreeWalker
is the child of the Checker
. So if any exception occurs inside TreeWalker.setupChild
it will be caught inside Checker.setupChild
, right?
Is this try-catch necessary?
It will require some tests to be changed, because new thrown exception differs from old one.
Previous exception message
cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'excludedPackages' to 'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck
Exception with try/catch inside TreeWalker.setupChild
cannot initialize module com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck - Cannot set property 'excludedPackages' to 'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck
Other moment, if for example TreeWalker.setupChild
catches exception and throws CheckstyleException
. This thrown exception will be caught by Checker.setupChild
and message becomes little bit weird:
cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - cannot initialize module com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck - Cannot set property 'option' to 'invalid_option' in module com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck
original message
cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to 'invalid_option' in module com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck
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.
@@ -59,6 +63,7 @@ | |||
* | |||
* @author Oliver Burn | |||
*/ | |||
// -@cs[ClassFanOutComplexity] Number of classes current class relies on exceeds 25. |
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.
Suppression comment should be on why you can't comply with check, not what violation was.
Example: https://github.com/checkstyle/checkstyle/blob/master/config/suppressions.xml#L74-L75
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
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.
@rnveach
Please explain why do we need the annotation ?
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.
@Override | ||
public void setupChild(Configuration childConf) | ||
throws CheckstyleException { | ||
final String name = childConf.getName(); | ||
final Object module = moduleFactory.createModule(name); | ||
if (!(module instanceof AbstractCheck)) { | ||
if (module instanceof AutomaticBean) { |
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.
createModule already put in exception all required context for user - https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java#L183
but other methods contextualize
and configure
will not provide context of which module was in reflection initialization process.
@timurt , please add try-catch.
UPDATE: moved to #4814
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 don't see anything else.
final File file = temporaryFolder.newFile("file.java"); | ||
try (Writer writer = new BufferedWriter( | ||
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) { | ||
final String content = "public class Main {\n" |
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 isn't this in an input file? Why do we have to create it at runtime?
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 thought source is too small to be stored inside separate file
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.
Size doesn't matter for inputs. Please move this to input file.
Code and inputs should be separated. Inputs are in files so they can be verified that they compile fine. We don't deal with non-compilable 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.
done
Look at change in command. Edit: I see we have snapshot in the next command, which actually runs regression. The line commented on was building their custom modules, I assume? so we were missing regression with that. |
I think no, inside
Probably runs
Probably runs checkstyle for all subprojects. So it would definitely fail, if compatibility error occurred. I added
|
@rnveach @romani |
…NearbyCommentFilter children of TreeWalker
…hNearbyCommentFilter gist patches
@romani |
#4714