summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.samplecheckers.findviewbyid.ui/src/com/motorolamobility/preflighting/samplechecker/findviewbyid/quickfix/FindViewByIdMarkerResolution.java
blob: f29138d00360318fea059519e95158663e359825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorolamobility.preflighting.samplechecker.findviewbyid.quickfix;

import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.WhileStatement;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.graphics.Image;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.AbstractTextEditor;

import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorolamobility.preflighting.samplechecker.findviewbyid.quickfix.i18n.MessagesNLS;

/**
 * MarkerResolution responsible for moving the findViewByID call outside the loop.
 */
public class FindViewByIdMarkerResolution implements IMarkerResolution2
{

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.IMarkerResolution#getLabel()
     */
    public String getLabel()
    {
        return MessagesNLS.FindViewByIdMarkerResolution_Label;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.IMarkerResolution#run(org.eclipse.core.resources.IMarker)
     */
    public void run(IMarker marker)
    {
        IResource resource = marker.getResource();
        final ICompilationUnit iCompilationUnit =
                JavaCore.createCompilationUnitFrom((IFile) resource);

        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setProject(JavaCore.create(resource.getProject()));
        parser.setResolveBindings(true);
        parser.setSource(iCompilationUnit);
        parser.setStatementsRecovery(true);
        parser.setBindingsRecovery(true);
        final CompilationUnit compUnit = (CompilationUnit) parser.createAST(null);

        try
        {
            final int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, -1);
            MethodInvocation invokedMethod = null;

            //Look for the invokedMethod that shall be moved
            invokedMethod = getMethodInvocation(compUnit, lineNumber, invokedMethod);

            IEditorPart editor = openEditor(iCompilationUnit);

            ASTNode loopNode = getLoopNode(invokedMethod);

            //Retrieve block parent for the loop statement.
            Block targetBlock = (Block) loopNode.getParent();
            List<Statement> statements = targetBlock.statements();
            int i = getLoopStatementIndex(loopNode, statements);

            //Add the node before the loop.
            compUnit.recordModifications();
            ASTNode invokedMethodStatement = getInvokedStatement(invokedMethod);
            final VariableDeclarationStatement varDeclarationStatement[] =
                    new VariableDeclarationStatement[1];

            //Verify if the invoke statement contains a variable attribution
            if (invokedMethodStatement instanceof ExpressionStatement)
            {
                ExpressionStatement expressionStatement =
                        (ExpressionStatement) invokedMethodStatement;
                Expression expression = expressionStatement.getExpression();
                if (expression instanceof Assignment)
                {
                    Expression leftHandSide = ((Assignment) expression).getLeftHandSide();
                    if (leftHandSide instanceof SimpleName) //Search for the variable declaration
                    {
                        SimpleName simpleName = (SimpleName) leftHandSide;
                        final String varName = simpleName.getIdentifier();

                        loopNode.accept(new ASTVisitor()
                        {
                            @Override
                            public boolean visit(VariableDeclarationStatement node) //Visit all variable declarations inside the loop looking for the variable which receives the findViewById result
                            {
                                List<VariableDeclarationFragment> fragments = node.fragments();
                                for (VariableDeclarationFragment fragment : fragments)
                                {
                                    if (fragment.getName().getIdentifier().equals(varName))
                                    {
                                        varDeclarationStatement[0] = node;
                                        break;
                                    }
                                }
                                return super.visit(node);
                            }
                        });
                    }
                }
            }

            //Variable is declared inside the loop, now let's move the variable declaration if needed
            if (varDeclarationStatement[0] != null)
            {
                ASTNode varDeclarationSubTree =
                        ASTNode.copySubtree(targetBlock.getAST(), varDeclarationStatement[0]);
                statements.add(i, (Statement) varDeclarationSubTree.getRoot());

                //Delete the node inside loop.
                varDeclarationStatement[0].delete();
                i++;
            }
            ASTNode copySubtree = ASTNode.copySubtree(targetBlock.getAST(), invokedMethodStatement);
            statements.add(i, (Statement) copySubtree.getRoot());

            //Delete the node inside loop.
            invokedMethodStatement.delete();

            // apply changes to file
            final Map<?, ?> mapOptions = JavaCore.create(resource.getProject()).getOptions(true);
            final IDocument document =
                    ((AbstractTextEditor) editor).getDocumentProvider().getDocument(
                            editor.getEditorInput());
            PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
            {

                public void run()
                {
                    try
                    {
                        TextEdit edit = compUnit.rewrite(document, mapOptions);
                        iCompilationUnit.applyTextEdit(edit, new NullProgressMonitor());
                    }
                    catch (JavaModelException e)
                    {
                        EclipseUtils.showErrorDialog(
                                MessagesNLS.FindViewByIdMarkerResolution_Error_Msg_Title,
                                MessagesNLS.FindViewByIdMarkerResolution_Error_Aplying_Changes
                                        + e.getMessage());
                    }

                }
            });

            marker.delete();

        }
        catch (Exception e)
        {
            EclipseUtils.showErrorDialog(MessagesNLS.FindViewByIdMarkerResolution_Error_Msg_Title,
                    MessagesNLS.FindViewByIdMarkerResolution_Error_Could_Not_Fix_Code);
        }
    }

    private IEditorPart openEditor(final ICompilationUnit iCompilationUnit)
    {
        IEditorPart editor = null;
        try
        {
            editor = JavaUI.openInEditor(iCompilationUnit);
        }
        catch (Exception e)
        {
            EclipseUtils.showErrorDialog(MessagesNLS.FindViewByIdMarkerResolution_Error_Msg_Title,
                    MessagesNLS.FindViewByIdMarkerResolution_Error_Unable_To_Open_Editor);
        }
        return editor;
    }

    private MethodInvocation getMethodInvocation(final CompilationUnit compUnit,
            final int lineNumber, MethodInvocation invokedMethod)
    {
        final MethodInvocation[] tempMethodInvocation = new MethodInvocation[1];
        compUnit.accept(new ASTVisitor()
        {
            @Override
            public boolean visit(MethodInvocation node)
            {
                if (compUnit.getLineNumber(node.getStartPosition()) == lineNumber)
                {
                    tempMethodInvocation[0] = node;
                }
                return super.visit(node);
            };
        });
        if (tempMethodInvocation[0] != null)
        {
            invokedMethod = tempMethodInvocation[0];
        }
        return invokedMethod;
    }

    /*
     * Look for Statement containing the invokedMethod
     */
    private ASTNode getInvokedStatement(MethodInvocation invokedMethod)
    {
        boolean found = false;
        ASTNode parent = invokedMethod.getParent();
        do
        {
            if ((parent instanceof Statement))
            {
                found = true;
            }
            else
            {
                parent = parent.getParent();
            }
        }
        while (!found);
        return parent;
    }

    /*
     * Resturns the index of the loop statement, inside a list of statements
     */
    private int getLoopStatementIndex(ASTNode loopNode, List<Statement> statements)
    {
        int i = 0;
        for (i = 0; i < statements.size(); i++)
        {
            Statement statement = statements.get(i);
            if (statement.equals(loopNode))
            {
                break;
            }
        }
        return i;
    }

    /*
     * Find the loop node that contains the called method
     */
    private ASTNode getLoopNode(MethodInvocation invokedMethod)
    {
        boolean found = false;
        ASTNode parent = invokedMethod.getParent();
        do
        {
            parent = parent.getParent();
            if ((parent instanceof ForStatement) || (parent instanceof DoStatement)
                    || (parent instanceof WhileStatement)
                    || (parent instanceof EnhancedForStatement))
            {
                found = true;
            }
        }
        while (!found);
        return parent;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.IMarkerResolution2#getDescription()
     */
    public String getDescription()
    {
        return MessagesNLS.FindViewByIdMarkerResolution_Description;
    }

    public Image getImage()
    {
        // TODO Auto-generated method stub
        return null;
    }

}