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

fix: backward compatible modeling of try-with-resource #4625

Merged
merged 16 commits into from
Feb 28, 2022

Conversation

monperrus
Copy link
Collaborator

@monperrus monperrus commented Feb 23, 2022

An iteration about try-with-resources on top of #4371.

Before the resource declaration was a CtVariableRead, which is not natural and breaking.

Now a resource is an implicit variable declaration. The main advantage is that it is fully backward compatible (hence we would fix https://ci.inria.fr/sos/job/npefix/)

Plus, I've added a test on pretty-printing.

@monperrus monperrus changed the title WIP fix: fix modeling of try-with-resource fix: backward compatible modeling of try-with-resource Feb 28, 2022
@monperrus monperrus marked this pull request as ready for review February 28, 2022 18:06
@monperrus monperrus changed the title fix: backward compatible modeling of try-with-resource REVIEW fix: backward compatible modeling of try-with-resource Feb 28, 2022
@monperrus
Copy link
Collaborator Author

the diff is now clean here

Copy link
Collaborator

@MartinWitt MartinWitt left a comment

Choose a reason for hiding this comment

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

Only some very minor style suggestions, otherwise looks good. 👍

assertEquals("resource", ((CtVariable<?>) ctResource).getSimpleName());

// contract: pretty-printing of existing resources works
assertEquals("try (resource) {"+System.getProperty("line.separator")+"}", tryStmt.toString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could use the LinespeparatorExtension here and \n in the testcase.

assertTrue(ctResource instanceof CtVariableRead);
assertEquals("resource", ((CtVariableRead<?>) ctResource).getVariable().getSimpleName());
assertTrue(ctResource instanceof CtVariable);
assertEquals("resource", ((CtVariable<?>) ctResource).getSimpleName());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert the spaces to tabs here. We are not super strict with formatting in testcases but a method should either use spaces or tabs, not both

// we have to find it manually
for (ASTPair pair: this.jdtTreeBuilder.getContextBuilder().stack) {
final List<CtLocalVariable> variables = pair.element.getElements(new TypeFilter<>(CtLocalVariable.class));
if (variables.size() > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (variables.size() > 0) {
if (!variables.isEmpty()) {

tryWithResource.addResource((CtLocalVariable) child);
} else if (child instanceof CtVariableRead) {
// special case of the resource being declared before
final CtVariableReference<?> variableRef = ((CtVariableRead<?>) child).getVariable();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there any reasons for the final modifiers, especially for Collections?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the IDE puts it automatically, and it does not hurt.

@monperrus
Copy link
Collaborator Author

Thanks fixed all comments

@MartinWitt MartinWitt changed the title REVIEW fix: backward compatible modeling of try-with-resource fix: backward compatible modeling of try-with-resource Feb 28, 2022
@MartinWitt MartinWitt merged commit 557501a into INRIA:master Feb 28, 2022
@MartinWitt
Copy link
Collaborator

thanks @monperrus

@monperrus
Copy link
Collaborator Author

I confirm that the regression identified in NpeFix is solved: https://ci.inria.fr/sos/job/npefix/1861/

raghav-deepsource pushed a commit to DeepSourceCorp/spoon that referenced this pull request Apr 5, 2022
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.

2 participants