-
Notifications
You must be signed in to change notification settings - Fork 50
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 180 #181
Issue 180 #181
Conversation
…60 -> 6.2.0.21135 and SonarEdition set to Community.
src/test/java/com/cognifide/aemrules/matcher/MethodMatcherTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/cognifide/aemrules/matcher/MethodMatcherTest.java
Outdated
Show resolved
Hide resolved
tree.resources().stream() | ||
.map(resource -> resource.simpleName().name()) | ||
tree.resourceList().stream() | ||
.map(resource -> ((VariableTreeImpl) resource).simpleName().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.
What's the reason behind referring to an implementation type here and casting?
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.
This was the only way I found to get that prop.
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.
tree.resourceList()
returns a ListTree<Tree>
and the method you're trying to use here doesn't exist at such a high interface level. Still, you don't need to use the specific implementation type here. As far as I can tell, the interface VariableTree
should be sufficient
tree.resourceList().stream()
// We're only interested in variables so filter out VariableTree instances from a list of Tree. Not all trees have names.
.filter(resource -> resource instanceof VariableTree)
.map(VariableTree.class::cast)
.map(resource -> resource.simpleName().name())
.forEach(resourceResolversInTryWithResources::add);
I'm trying to think of a unit test case that would show us where this cast could fail without the filter.
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 can't think of a way to pass a syntax tree that doesn't cast to VariableTree
inside a try
statement's resource list that is parse-able as valid Java, as per https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3
I wonder why the resourceTree
method, which appears specific to the TryStatementTree
doesn't return something more specific than Tree
. Could be just a matter of consistency.
How about:
tree.resourceList().stream()
// We're iterating over a resource specification in a try-with-resource block
// so we expect variable trees
.filter(resource -> resource instanceof VariableTree)
.map(VariableTree.class::cast)
.map(resource -> resource.simpleName().name())
.forEach(resourceResolversInTryWithResources::add);
Benefits compared to the current solution:
- Uses an interface rather than an implementation class
- Provides the reasoning behind the cast
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.
This kind of works but it turns out there's a new way to write a try-with-resources block in Java 9 and above.
https://stackoverflow.com/questions/61124346/when-does-sonars-trystatementtreeresourcelist-return-a-tree-that-isnt-a-varia/61124506#61124506
Issue #188 raised to address this.
SonarCloud claims there's no test coverage on two lines you changed here. Also, it seems to still be using @chutch 's personal SonarCloud account. |
...ifide/aemrules/java/checks/resourceresolver/close/ResourceResolverTryWithResourcesCheck.java
Show resolved
Hide resolved
Kudos, SonarCloud Quality Gate passed!
|
No description provided.