These are chat archives for JnRouvignac/AutoRefactor

29th
Sep 2016
Luis Cruz
@luiscruz
Sep 29 2016 11:31
Do you have any idea why
typeDeclaration.bodyDeclarations().add(onPauseDeclaration);
does not add a method declaration to a type? I’ve checked and the list bodyDeclarations is a “live" structure.
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 11:32
I think the question is not complete?
Oh it is
Bad client on my side
Luis Cruz
@luiscruz
Sep 29 2016 11:33
I’ve managed to do it using
.insertAt(
  onPauseDeclaration,
  typeDeclaration.bodyDeclarations().size(),
  typeDeclaration.getBodyDeclarationsProperty(),
  typeDeclaration
);
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 11:33
+1
To change nodes coming from the parser, you must go through the Refactorings class
Which in your case is a bit shitty to be honest
Luis Cruz
@luiscruz
Sep 29 2016 11:38
humm ok, so If I'd want to use the first approach I would have to call some (non-implemented?) method in Refactorings. is that right?
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 11:38
No, it is more involved :)
Create a wrapper that transparently calls
Refactorings behind the scene
I think I will do it someday
It is easier to read and understand IMO
Luis Cruz
@luiscruz
Sep 29 2016 11:42
Yes, insertAt is not really intuitive at first. But it’s not critical. I asked because I wasn’t getting why the first approach did not work. Anyway, I think I got it ;)
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 11:43
Please leave a TODO or FIXME in your code so I can remember about it
I can't really take a note of it right now
Luis Cruz
@luiscruz
Sep 29 2016 11:43
sure, will do
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 11:44
Thanks
Luis Cruz
@luiscruz
Sep 29 2016 11:44
;)
Luis Cruz
@luiscruz
Sep 29 2016 16:22

I’ve decided to use

if (!wl.isHeld()) {
  wl.release();
}

instead of

wl.release();

I’ve just finished it but now I have a question

I’ve created the method

private Statement createWakelockReleaseNode(MethodInvocation methodInvocation)

to create the node with the IfStatement

Since I want to call release() from the wakelock instance wl, I had to copy the expression of the MethodInvocation
Luis Cruz
@luiscruz
Sep 29 2016 16:29
However, at first I was trying to do object = b.copyExpression(methodInvocation)and later call b.copy(object) wherever I needed it. However this did not work. I had to call b.copyExpressionevery time I needed the wake lock object.
Why?
(Not sure if I was clear.)
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 16:41
Because this method creates placeholders
A placeholder only holds one place :)
It is like when you removed one node, but tried to use it elsewhere
Just remember you can only use a node once (whether it is coming from parsing or it is a newly created node)
Do you see what I mean?
Luis Cruz
@luiscruz
Sep 29 2016 16:45
Still processing
It's quite hard for me to get it. copyExpression() also uses copy() internally
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 18:31
Ah sorry I misread
The problem is that with JDT you cannot copy a copy
You can only copy a parsed node
I tried to add some validation to catch such mistakes but I did not find an easy way
Sorry that I badly understood your question at first
Luis Cruz
@luiscruz
Sep 29 2016 20:55
Oh okay, that makes sense :)
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 20:57
I had to change code once or twice because of this
Luis Cruz
@luiscruz
Sep 29 2016 20:59
Once you know it, it's quite easy to avoid the mistake. But at first I was really confused :)
Jean-Noël Rouvignac
@JnRouvignac
Sep 29 2016 21:00
So was I!