-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
add interface CtBodyHolder #943
Conversation
Hi, We also need a method |
1b32d6e
to
927fc95
Compare
Note that some children of CtBodyHolder are using These which expects public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> block;
if (statement instanceof CtBlock<?>) {
block = (CtBlock<?>) statement;
} else {
block = getFactory().Code().createCtBlock(statement);
}
return setBody(block);
} Another solution would be to throw exception that CtBlock is expected. But I vote for automatic creation of CtBlock, because it makes live of clients more comfortable. |
|
ParentContractTest failed, but I do not understand what is that. Could you fix it or explain it? |
For different reasons, first because having both
by
then it works. But then, other contracts are violated, and for good reasons. I would go for removing the automatic block creation in |
So it should throw exception if client does not sent CtBody? The I do not understand how will look the generic code which can call CtBodyHolder.setBody() A) client does not know whether CtBlock or CtStatement has to be used, because it does not know real instance which is behind. And if it uses the wrong value then we throw an exception? |
if we all agree on #956, it should solve the parent problem. |
So should I change something in that PR? I do not understand the problem of ParentContractTest so I do not know what to do |
927fc95
to
bf4db3f
Compare
Now I see that |
6726695
to
ceceb1e
Compare
|
9fcd54c
to
9923a95
Compare
Now the |
Because since you have a change the signature of a metamodel method, you also have to regenerate the ReplacementVisitor. It's generated by
and to add it in the commit. |
What about changing all setBody methods to have the same signature setBody(statement)? And convert it block if needed internaly. |
It would be less intuitive for clients who just rely on the type system for documentation. I prefer to keep it this way. |
the tests pass hurray! could you push ReplacementVisitor with Unix newlines (the diff will be much smaller)? also could you add tests for the new setBody() methods? |
Afterthought about this. Instead of adding 3x
|
;-) ... no, this PR is never ending story :(
I would prefer opposite. In suggest to use
cons
I understand, that from technical point of view (java specification), the CtExecutable.body must be a And if setBody(CtBlock) is always used, then everybody has to always create CtBlock even if s/he wants to add one statement only. What is advantage in that? I see, ... hard choice. ;-) |
@Override | ||
public <T extends CtLoop> T setBody(CtStatement body) { | ||
public <T extends CtBodyHolder> T setBody(CtStatement body) { | ||
if (body != null) { |
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.
to be consistent, we should also create a block if the argument is not a block
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 do not understand why. CtLoop accepts statement, so we should not create CtBlock. If you speak about change of spoon model, then I would prefer to do it in another PR ... PLEASE!!! :-)))
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.
for two reasons:
- because we want a 100% consistent behavior over all subinterfaces of CtBodyHolder
- because it's 100% along the line of the new AlwaysInBlock contract introduced in new feature: Statement always in a block #683
@tdurieux agrees with you, and your reasoning makes sense. however, we should also modify CtLoopImpl.setBody to be consistent. |
Do I understand well, that you will accept the PR, which has only one method It of course means, that all methods setBody(CtBlock) will be removed. Yes, I like that solution. But I do not like to rewrite it again and again ... so I would prefer to have your agreement about that concept, before I spent hours of my free time for that. ;-) |
I understand :-)
Yes but it will be backward compatible both statically ( And we'll keep the return type of This solution seems to be 1) fully backward compatible and 2) more powerful because it abstracts away details and simplifies the job of client code. |
You'll also have to rebase due to the change in |
96b995c
to
39b3d1a
Compare
39b3d1a
to
15c825f
Compare
CtBlock<?> getBody(); | ||
|
||
/** | ||
* Sets the catch's body. | ||
* Sets the catch's body. The instance of CtBlock makes sense too |
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.
we don't need to add it again in CtCatch if we don't change the return type or the contract.
CtStatement getBody(); | ||
|
||
/** | ||
* Sets the body of this element. |
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.
here we should document the automated block creation.
"If body is not a block, it is wrapped in a CtBlock which is semantically equivalent and eases transformation afterwards if required."
*/ | ||
<T extends CtLoop> T setBody(CtStatement body); | ||
@Override |
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.
same
CtBlock<?> getBody(); | ||
|
||
/** | ||
* Sets the tried body. | ||
* Sets the tried body. The instance of CtBlock makes sense too |
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.
same
@Override | ||
public <T extends CtLoop> T setBody(CtStatement body) { | ||
public <T extends CtBodyHolder> T setBody(CtStatement body) { | ||
if (body != null) { |
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.
for two reasons:
- because we want a 100% consistent behavior over all subinterfaces of CtBodyHolder
- because it's 100% along the line of the new AlwaysInBlock contract introduced in new feature: Statement always in a block #683
* @param element | ||
* @return CtBlock instance | ||
*/ | ||
public <T extends CtStatement> CtBlock<?> getOrCreateCtBlock(T element) { |
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.
excellent to factorize the code!
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.
Thanks!
What about setImplicit? it is mentioned in #683
return this.createCtBlock(element).setImplicit(true);
I have forgot to add tests. I will do it tommorrow |
No, setImplicit is only for JDTTReeBuilder. As soon as you use an |
@@ -53,16 +53,12 @@ | |||
boolean removeCatcher(CtCatch catcher); | |||
|
|||
/** | |||
* Sets the tried body. | |||
* Gets the tried body. |
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.
Thanks for the first type, one remains "try body" (and not tried)
looks like we're almost done :-) |
cdf2c67
to
ec992f7
Compare
I had to fix some failing tests too. They were creating CtElement instances without factory. I guess it was a bug. Or should spoon work well when CtElement.factory == null ?? |
It will be like a X-mass for me :-) But tomorrow. The test is missing |
|
throw Exception which contains details about real problem
17e1a21
to
4060e6e
Compare
many thanks Pavel. |
Note! I had to rollback
CtExecutable.getBody
declaration made byto make it compilable with new
CtBodyHolder
. I have not found a way how to declareCtBodyHolder
to have it compilable with originCtExecutable.getBody
declaration.I have no idea whether my changes are correct. It might have some impact to Spoon templates!