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

review: fix: Make CtFieldRead inherit from CtResource #5860

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

I-Al-Istannen
Copy link
Collaborator

Fields are allowed in try-with-resources statements now, so spoon should model this case. Before this patch, only CtLocalVariable implemented CtResource.

@I-Al-Istannen I-Al-Istannen force-pushed the fix/field-in-try-with-resources branch 4 times, most recently from 4e87310 to d644e27 Compare June 18, 2024 19:05
@I-Al-Istannen I-Al-Istannen changed the title fix: Make CtField inherit from CtResource fix: Make CtFieldRead inherit from CtResource Jun 18, 2024
@I-Al-Istannen I-Al-Istannen changed the title fix: Make CtFieldRead inherit from CtResource review: fix: Make CtFieldRead inherit from CtResource Jun 18, 2024
Fields are allowed in try-with-resources statements now, so spoon should
model this case.
…pe for CtResource

This allows us to correctly model try-with-resources only taking a
field, parameter, catch variable or local variable reference.
@I-Al-Istannen I-Al-Istannen force-pushed the fix/field-in-try-with-resources branch from bc7474e to a5b4c1a Compare June 24, 2024 11:10
Comment on lines -1109 to -1130
} else if (child instanceof CtVariableRead) {
// special case of the resource being declared before
final CtVariableReference<?> variableRef = ((CtVariableRead<?>) child).getVariable();
if (variableRef.getDeclaration() != null) {
// getDeclaration works
tryWithResource.addResource((CtResource<?>) variableRef.getDeclaration().clone().setImplicit(true));
} else {
// we have to find it manually
for (ASTPair pair: this.jdtTreeBuilder.getContextBuilder().getAllContexts()) {
final List<CtLocalVariable> variables = pair.element().getElements(new TypeFilter<>(CtLocalVariable.class));
for (CtLocalVariable v: variables) {
if (v.getSimpleName().equals(variableRef.getSimpleName())) {
// we found the resource
// we clone it in order to comply with the contract of being a tree
final CtLocalVariable clone = v.clone();
clone.setImplicit(true);
tryWithResource.addResource(clone);
break;
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I will miss this easy to understand and maintainable code.

Comment on lines +569 to +575
private static CtModel createModelFromString(String code) {
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(21);
launcher.getEnvironment().setShouldCompile(true);
launcher.addInputResource(new VirtualFile(code));
return launcher.buildModel();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

One day, we will have a single method for this :(

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And other lies I tell myself? despair

@I-Al-Istannen
Copy link
Collaborator Author

I-Al-Istannen commented Jun 25, 2024

This is technically a breaking change (though I would hope the breakage isn't too much). The old model with CtResource extends CtVariable just isn't sustainable anymore with modern code and can not be salvaged.

@MartinWitt
Copy link
Collaborator

@SirYwell ping

1 similar comment
@MartinWitt
Copy link
Collaborator

@SirYwell ping

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

2 participants