-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
review: fix: Make CtFieldRead inherit from CtResource #5860
Conversation
4e87310
to
d644e27
Compare
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.
bc7474e
to
a5b4c1a
Compare
} 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; | ||
} | ||
} | ||
} | ||
} |
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 will miss this easy to understand and maintainable code.
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(); | ||
} |
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.
One day, we will have a single method for this :(
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.
And other lies I tell myself? despair
This is technically a breaking change (though I would hope the breakage isn't too much). The old model with |
@SirYwell ping |
1 similar comment
@SirYwell ping |
Thanks for the modeling PR @I-Al-Istannen For, the record the corresponding update to handle the breaking change: SpoonLabs/npefix@9512a41 |
Fields are allowed in try-with-resources statements now, so spoon should model this case. Before this patch, only
CtLocalVariable
implementedCtResource
.