~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/hints/src/org/netbeans/modules/java/hints/DoubleCheck.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
 
27
 */
 
28
package org.netbeans.modules.java.hints;
 
29
 
 
30
import com.sun.source.tree.BinaryTree;
 
31
import com.sun.source.tree.BlockTree;
 
32
import com.sun.source.tree.ExpressionTree;
 
33
import com.sun.source.tree.IdentifierTree;
 
34
import com.sun.source.tree.IfTree;
 
35
import com.sun.source.tree.ParenthesizedTree;
 
36
import com.sun.source.tree.StatementTree;
 
37
import com.sun.source.tree.SynchronizedTree;
 
38
import com.sun.source.tree.Tree;
 
39
import com.sun.source.tree.Tree.Kind;
 
40
import com.sun.source.util.TreePath;
 
41
import java.io.IOException;
 
42
import java.util.Collections;
 
43
import java.util.EnumSet;
 
44
import java.util.List;
 
45
import java.util.Set;
 
46
import java.util.prefs.Preferences;
 
47
import javax.swing.JComponent;
 
48
import javax.swing.text.BadLocationException;
 
49
import javax.swing.text.Document;
 
50
import org.netbeans.api.java.source.Task;
 
51
import org.netbeans.api.java.source.CompilationInfo;
 
52
import org.netbeans.api.java.source.JavaSource;
 
53
import org.netbeans.api.java.source.ModificationResult;
 
54
import org.netbeans.api.java.source.TreePathHandle;
 
55
import org.netbeans.api.java.source.WorkingCopy;
 
56
import org.netbeans.modules.java.hints.spi.AbstractHint;
 
57
import org.netbeans.spi.editor.hints.ChangeInfo;
 
58
import org.netbeans.spi.editor.hints.ErrorDescription;
 
59
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
 
60
import org.netbeans.spi.editor.hints.Fix;
 
61
import org.openide.filesystems.FileObject;
 
62
import org.openide.util.Exceptions;
 
63
import org.openide.util.NbBundle;
 
64
 
 
65
/**
 
66
 *
 
67
 * @author Jaroslav tulach
 
68
 */
 
69
public class DoubleCheck extends AbstractHint {
 
70
    private transient volatile boolean stop;
 
71
    
 
72
    /** Creates a new instance of AddOverrideAnnotation */
 
73
    public DoubleCheck() {
 
74
        super( true, true, AbstractHint.HintSeverity.WARNING);
 
75
    }
 
76
    
 
77
    public Set<Kind> getTreeKinds() {
 
78
        return EnumSet.of(Kind.SYNCHRONIZED);
 
79
    }
 
80
    
 
81
    public List<ErrorDescription> run(CompilationInfo compilationInfo,
 
82
                                      TreePath treePath) {
 
83
        stop = false;
 
84
        try {
 
85
            Document doc = compilationInfo.getDocument();
 
86
            
 
87
            if (doc == null) {
 
88
                return null;
 
89
            }
 
90
            
 
91
            Tree e = treePath.getLeaf();
 
92
            if (e == null || e.getKind() != Kind.SYNCHRONIZED) {
 
93
                return null;
 
94
            }
 
95
            
 
96
            SynchronizedTree synch = (SynchronizedTree)e;
 
97
            IfTree outer = findOuterIf(compilationInfo, treePath);
 
98
            if (outer == null) {
 
99
                return null;
 
100
            }
 
101
            
 
102
            IfTree same = null;
 
103
            for (StatementTree statement : synch.getBlock().getStatements()) {
 
104
                if (sameIf(statement, outer)) {
 
105
                    same = (IfTree)statement;
 
106
                    break;
 
107
                }
 
108
                if (stop) {
 
109
                    return null;
 
110
                }
 
111
            }
 
112
            if (same == null) {
 
113
                return null;
 
114
            }
 
115
            
 
116
            TreePath outerPath = compilationInfo.getTrees().getPath(compilationInfo.getCompilationUnit(), outer);
 
117
            
 
118
            List<Fix> fixes = Collections.<Fix>singletonList(new FixImpl(
 
119
                TreePathHandle.create(treePath, compilationInfo),
 
120
                TreePathHandle.create(outerPath, compilationInfo),
 
121
                compilationInfo.getFileObject()
 
122
            ));
 
123
 
 
124
            int span = (int)compilationInfo.getTrees().getSourcePositions().getStartPosition(
 
125
                compilationInfo.getCompilationUnit(),
 
126
                synch
 
127
            );
 
128
 
 
129
            ErrorDescription ed = ErrorDescriptionFactory.createErrorDescription(
 
130
                getSeverity().toEditorSeverity(),
 
131
                NbBundle.getMessage(DoubleCheck.class, "MSG_FixDoubleCheck"), // NOI18N
 
132
                fixes,
 
133
                doc,
 
134
                doc.createPosition(span),
 
135
                doc.createPosition(span + "synchronized".length()) // NOI18N
 
136
            );
 
137
 
 
138
            return Collections.singletonList(ed);
 
139
        } catch (BadLocationException e) {
 
140
            Exceptions.printStackTrace(e);
 
141
        } catch (IOException e) {
 
142
            Exceptions.printStackTrace(e);
 
143
        }
 
144
        
 
145
        return null;
 
146
    }
 
147
 
 
148
    public String getId() {
 
149
        return getClass().getName();
 
150
    }
 
151
 
 
152
    public String getDisplayName() {
 
153
        return NbBundle.getMessage(DoubleCheck.class, "MSG_DoubleCheck"); // NOI18N
 
154
    }
 
155
 
 
156
    public String getDescription() {
 
157
        return NbBundle.getMessage(DoubleCheck.class, "HINT_DoubleCheck"); // NOI18N
 
158
    }
 
159
 
 
160
    public void cancel() {
 
161
        stop = true;
 
162
    }
 
163
    
 
164
    public Preferences getPreferences() {
 
165
        return null;
 
166
    }
 
167
    
 
168
    @Override
 
169
    public JComponent getCustomizer(Preferences node) {
 
170
        return null;
 
171
    }    
 
172
 
 
173
    private IfTree findOuterIf(CompilationInfo compilationInfo, TreePath treePath) {
 
174
        while (!stop) {
 
175
            treePath = treePath.getParentPath();
 
176
            if (treePath == null) {
 
177
                break;
 
178
            }
 
179
            Tree leaf = treePath.getLeaf();
 
180
            
 
181
            if (leaf.getKind() == Kind.IF) {
 
182
                return (IfTree)leaf;
 
183
            }
 
184
            
 
185
            if (leaf.getKind() == Kind.BLOCK) {
 
186
                BlockTree b = (BlockTree)leaf;
 
187
                if (b.getStatements().size() == 1) {
 
188
                    // ok, empty blocks can be around synchronized(this) 
 
189
                    // statements
 
190
                    continue;
 
191
                }
 
192
            }
 
193
            
 
194
            return null;
 
195
        }
 
196
        return null;
 
197
    }
 
198
 
 
199
    private boolean sameIf(StatementTree statement, IfTree second) {
 
200
        if (statement.getKind() != Kind.IF) {
 
201
            return false;
 
202
        }
 
203
        
 
204
        IfTree first = (IfTree)statement;
 
205
        
 
206
        if (first.getElseStatement() != null) {
 
207
            return false;
 
208
        }
 
209
        if (second.getElseStatement() != null) {
 
210
            return false;
 
211
        }
 
212
        
 
213
        ExpressionTree varFirst = equalToNull(first.getCondition());
 
214
        ExpressionTree varSecond = equalToNull(second.getCondition());
 
215
        
 
216
        if (varFirst == null || varSecond == null) {
 
217
            return false;
 
218
        }
 
219
        
 
220
        if (varFirst.getKind() == Kind.IDENTIFIER && varSecond.getKind() == Kind.IDENTIFIER) {
 
221
            IdentifierTree idFirst = (IdentifierTree)varFirst;
 
222
            IdentifierTree idSecond = (IdentifierTree)varSecond;
 
223
            
 
224
            return idFirst.getName().equals(idSecond.getName());
 
225
        }
 
226
        
 
227
        return false;
 
228
    }
 
229
    
 
230
    private ExpressionTree equalToNull(ExpressionTree t) {
 
231
        if (t.getKind() == Kind.PARENTHESIZED) {
 
232
            ParenthesizedTree p = (ParenthesizedTree)t;
 
233
            t = p.getExpression();
 
234
        }
 
235
        
 
236
        if (t.getKind() != Kind.EQUAL_TO) {
 
237
            return null;
 
238
        }
 
239
        BinaryTree bt = (BinaryTree)t;
 
240
        if (bt.getLeftOperand().getKind() == Kind.NULL_LITERAL && bt.getRightOperand().getKind() != Kind.NULL_LITERAL) {
 
241
            return bt.getRightOperand();
 
242
        }
 
243
        if (bt.getLeftOperand().getKind() != Kind.NULL_LITERAL && bt.getRightOperand().getKind() == Kind.NULL_LITERAL) {
 
244
            return bt.getLeftOperand();
 
245
        }
 
246
        return null;
 
247
    }
 
248
    
 
249
    private static final class FixImpl implements Fix, Task<WorkingCopy> {
 
250
        private TreePathHandle synchHandle;
 
251
        private TreePathHandle ifHandle;
 
252
        private FileObject file;
 
253
 
 
254
        public FixImpl(TreePathHandle synchHandle, TreePathHandle ifHandle, FileObject file) {
 
255
            this.synchHandle = synchHandle;
 
256
            this.ifHandle = ifHandle;
 
257
            this.file = file;
 
258
        }
 
259
        
 
260
        
 
261
        public String getText() {
 
262
            return NbBundle.getMessage(DoubleCheck.class, "MSG_DoubleCheck"); // NOI18N
 
263
        }
 
264
        
 
265
        public ChangeInfo implement() throws IOException {
 
266
            ModificationResult result = JavaSource.forFileObject(file).runModificationTask(this);
 
267
            result.commit();
 
268
            return null;
 
269
        }
 
270
        
 
271
        @Override public String toString() {
 
272
            return "FixDoubleCheck"; // NOI18N
 
273
        }
 
274
 
 
275
 
 
276
        public void run(WorkingCopy wc) throws Exception {
 
277
            wc.toPhase(JavaSource.Phase.RESOLVED);
 
278
            Tree syncTree = synchHandle.resolve(wc).getLeaf();
 
279
            Tree ifTree = ifHandle.resolve(wc).getLeaf();
 
280
            wc.rewrite(ifTree, syncTree);
 
281
        }
 
282
    }
 
283
    
 
284
}