These are chat archives for JnRouvignac/AutoRefactor

22nd
Sep 2016
Luis Cruz
@luiscruz
Sep 22 2016 11:52
Hi Jean-Noël!
Now I am trying to generate the “onPause” method when it is not present:
https://github.com/luiscruz/AutoRefactor/blob/master/plugin/src/main/java/org/autorefactor/refactoring/rules/WakeLockRefactoring.java#L93
Two things I did not figure out how to do:
  1. How to add @Override annotation
  2. How to add statement super.onPause(); to the block
Do you have an idea for 1? (I still haven’t worked on 2)
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 12:12
hello Luis
You may not be able to do this from ASTBuilder
let me see. I have never done that :)
When you are unsure about how things are represented in JDT AST, I'd recommend you have a look at the javadoc:
if you read the grammar, then you may infer that ExtendedModifier is a strong candidate
Actually I know it because I have worked with it
ExtendedModifier encompasses modifiers as well as annotations
so you could try onPauseDeclaration.modifiers().add(annotation);
I'll let you find out how to add a statement to the body :)
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 12:42
does that help?
Luis Cruz
@luiscruz
Sep 22 2016 12:48
I went for lunch. I'll try it ASAP
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 12:49
alright, sorry. I hope it was good :)
Luis Cruz
@luiscruz
Sep 22 2016 13:57
back!
Luis Cruz
@luiscruz
Sep 22 2016 14:05
it was very good, thanks :D
So the resultant method declaration looks like this:
@Override() protected void onPause(){
//...
not sure about the brackets...
btw, do you use any tool to see how the AST looks like?
Luis Cruz
@luiscruz
Sep 22 2016 14:10
I’m asking this because in this particular problem I’ve tried doing r.insertBefore(annotation,onPauseDeclaration);
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 14:11
It does not work
Luis Cruz
@luiscruz
Sep 22 2016 14:11
It failed probably because annotation should be in the subtree of the method Declaration
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 14:12
You are thinking in terms of source code
not in terms of AST
Luis Cruz
@luiscruz
Sep 22 2016 14:12
exactly
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 14:12
in terms of AST, the annotation is attached to the method
this is the same semantically in Java BTW
you can query annotations of a method via reflection

btw, do you use any tool to see how the AST looks like?

What do you mean?

Luis Cruz
@luiscruz
Sep 22 2016 14:14
I wanted to know if there is any tool that creates a visualization of the AST for a given source file. That way, I could get familiar with how the AST looks like in these cases
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 14:15
I think there is
Luis Cruz
@luiscruz
Sep 22 2016 14:15
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 14:15
yes it is the one indeed
Luis Cruz
@luiscruz
Sep 22 2016 14:29
got it working. this is cool. Now I can easily see that annotations are also stored as a Modifier
Screen Shot 2016-09-22 at 15.30.27.png
Luis Cruz
@luiscruz
Sep 22 2016 15:00
Is there a way of adding newlines to separate statements?
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:06
I do not know how
that would be nice indeed
Statement placeHolder = (Statement) rewriter.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT);
ListRewrite.insertFirst(placeHolder, null);
Luis Cruz
@luiscruz
Sep 22 2016 15:07
cool!
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:08
Same for comments:
Luis Cruz
@luiscruz
Sep 22 2016 15:08
but before I try it.. maybe you can help me with this:
org.junit.ComparisonFailure: WakeLock: wrong output; expected:<...estroy();
        }
[        
        @Override() protected void onPause() {
            wl.release();super.onPause();}
        ]
    }
}> but was:<...estroy();
        }
[
        @Override() protected void onPause(){
            wl.release();super.onPause()}]
    }
}>
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:08
    Statement placeHolder = (Statement) rewriter.createStringPlaceholder("//mycomment", ASTNode.EMPTY_STATEMENT);
    listRewrite.insertFirst(placeHolder, null);
look, the second does not have a semi-column ';'
you inserted a method invocation, but you must wrap it in an ExpressionStatement
Luis Cruz
@luiscruz
Sep 22 2016 15:09
oh that’s it!! I couldn’t see it :D
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:10
When in doubt use meld: http://meldmerge.org/
it is never tired, unlike me
Luis Cruz
@luiscruz
Sep 22 2016 15:11
xD
I was so convinced it was equal that I thought the problem was with the squared brackets
The other day when we were moving a MethodInvocation we did not need to encapsulate it in an ExpressionStatement. We have used this:
r.insertAt(b.move(node.getParent()),
                                method.getBody().statements().size(),
                                Block.STATEMENTS_PROPERTY,
                                method.getBody());
Oh forget it
I see now node.getParent()
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:14
;)
Luis Cruz
@luiscruz
Sep 22 2016 15:14
Forget it :D
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:14
yeah I made you add it
there is a check on node.getNodeType() == EXPRESSION_STATEMENT on purpose
Luis Cruz
@luiscruz
Sep 22 2016 15:16
yap, that I reluctantly added upon your recommendation :sweat_smile:
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:17
better be safe than sorry when touching at somebody else's code
or even YOUR code if you run the plugin
Luis Cruz
@luiscruz
Sep 22 2016 15:36
true :)
I’m having trouble with the rewriter in the code snippet for the newline. Should I get it from Refactorings?
(I’ve tried this with no luck: ASTRewrite rewriter = ASTRewrite.create(b.getAST());)
perhaps I can create a method in ASTBuilder to return newline statements
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 15:46
+1 for ASTBuilder
Luis Cruz
@luiscruz
Sep 22 2016 16:23
I have it working but I have some questions :)
which required me to create refactorings.getRewrite()method on Refactorings
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 16:27
Ahem
Luis Cruz
@luiscruz
Sep 22 2016 16:27
  1. I only got it working with the \n character
  2. I have tried using ASTRewrite.create(b.getAST()) instead of refactorings.getRewrite(). However, it was adding ;and newlines were not working even using the \ncharacter
Luis Cruz
@luiscruz
Sep 22 2016 16:33
perhaps I should mention that I have used setBody()method to add these statements. I don’t know if it makes any difference, comparing with listRewriting.insertFirst:
https://github.com/luiscruz/AutoRefactor/blob/master/plugin/src/main/java/org/autorefactor/refactoring/rules/WakeLockRefactoring.java#L115
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 16:35
Ok just focus on making it work
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 16:42
Do not stay blocked because of prettifying the code
We can fix that together once code is working
Luis Cruz
@luiscruz
Sep 22 2016 16:43
alright
thanks ;)
But before moving on, Is having the method refactorings.getRewrite() okay with you?
Jean-Noël Rouvignac
@JnRouvignac
Sep 22 2016 16:48
Yes temporarily
We will refactor it away in the code review
Luis Cruz
@luiscruz
Sep 22 2016 16:50
alright! sounds good!