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 180 #181

Merged
merged 11 commits into from
Mar 24, 2020
Merged

Issue 180 #181

merged 11 commits into from
Mar 24, 2020

Conversation

GulderBone
Copy link
Contributor

No description provided.

@coveralls
Copy link

coveralls commented Mar 23, 2020

Coverage Status

Coverage increased (+0.2%) to 95.664% when pulling ee3e103 on issue-180 into 2caf7d6 on master.

tree.resources().stream()
.map(resource -> resource.simpleName().name())
tree.resourceList().stream()
.map(resource -> ((VariableTreeImpl) resource).simpleName().name())
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

@toniedzwiedz toniedzwiedz Mar 24, 2020

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.

Copy link
Collaborator

@toniedzwiedz toniedzwiedz Mar 24, 2020

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:

  1. Uses an interface rather than an implementation class
  2. Provides the reasoning behind the cast

Copy link
Collaborator

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.

@toniedzwiedz
Copy link
Collaborator

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.

@GulderBone GulderBone linked an issue Mar 24, 2020 that may be closed by this pull request
@jplucinski jplucinski removed the request for review from piotr-wilczynski March 24, 2020 15:06
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 1 Code Smell

100.0% 100.0% Coverage
0.0% 0.0% Duplication

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.

Support for Java Plugin 6.0 and 6.1
4 participants