001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.java.hints;
029:
030: import com.sun.source.tree.BinaryTree;
031: import com.sun.source.tree.BlockTree;
032: import com.sun.source.tree.ExpressionTree;
033: import com.sun.source.tree.IdentifierTree;
034: import com.sun.source.tree.IfTree;
035: import com.sun.source.tree.ParenthesizedTree;
036: import com.sun.source.tree.StatementTree;
037: import com.sun.source.tree.SynchronizedTree;
038: import com.sun.source.tree.Tree;
039: import com.sun.source.tree.Tree.Kind;
040: import com.sun.source.util.TreePath;
041: import java.io.IOException;
042: import java.util.Collections;
043: import java.util.EnumSet;
044: import java.util.List;
045: import java.util.Set;
046: import java.util.prefs.Preferences;
047: import javax.swing.JComponent;
048: import javax.swing.text.BadLocationException;
049: import javax.swing.text.Document;
050: import org.netbeans.api.java.source.Task;
051: import org.netbeans.api.java.source.CompilationInfo;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.netbeans.api.java.source.ModificationResult;
054: import org.netbeans.api.java.source.TreePathHandle;
055: import org.netbeans.api.java.source.WorkingCopy;
056: import org.netbeans.modules.java.hints.spi.AbstractHint;
057: import org.netbeans.spi.editor.hints.ChangeInfo;
058: import org.netbeans.spi.editor.hints.ErrorDescription;
059: import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
060: import org.netbeans.spi.editor.hints.Fix;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.Exceptions;
063: import org.openide.util.NbBundle;
064:
065: /**
066: *
067: * @author Jaroslav tulach
068: */
069: public class DoubleCheck extends AbstractHint {
070: private transient volatile boolean stop;
071:
072: /** Creates a new instance of AddOverrideAnnotation */
073: public DoubleCheck() {
074: super (true, true, AbstractHint.HintSeverity.WARNING);
075: }
076:
077: public Set<Kind> getTreeKinds() {
078: return EnumSet.of(Kind.SYNCHRONIZED);
079: }
080:
081: public List<ErrorDescription> run(CompilationInfo compilationInfo,
082: TreePath treePath) {
083: stop = false;
084: Tree e = treePath.getLeaf();
085: if (e == null || e.getKind() != Kind.SYNCHRONIZED) {
086: return null;
087: }
088:
089: SynchronizedTree synch = (SynchronizedTree) e;
090: IfTree outer = findOuterIf(compilationInfo, treePath);
091: if (outer == null) {
092: return null;
093: }
094:
095: IfTree same = null;
096: for (StatementTree statement : synch.getBlock().getStatements()) {
097: if (sameIf(statement, outer)) {
098: same = (IfTree) statement;
099: break;
100: }
101: if (stop) {
102: return null;
103: }
104: }
105: if (same == null) {
106: return null;
107: }
108:
109: TreePath outerPath = compilationInfo.getTrees().getPath(
110: compilationInfo.getCompilationUnit(), outer);
111:
112: List<Fix> fixes = Collections.<Fix> singletonList(new FixImpl(
113: TreePathHandle.create(treePath, compilationInfo),
114: TreePathHandle.create(outerPath, compilationInfo),
115: compilationInfo.getFileObject()));
116:
117: int span = (int) compilationInfo.getTrees()
118: .getSourcePositions().getStartPosition(
119: compilationInfo.getCompilationUnit(), synch);
120:
121: ErrorDescription ed = ErrorDescriptionFactory
122: .createErrorDescription(getSeverity()
123: .toEditorSeverity(),
124: NbBundle.getMessage(DoubleCheck.class,
125: "MSG_FixDoubleCheck"), // NOI18N
126: fixes, compilationInfo.getFileObject(), span,
127: span + "synchronized".length() // NOI18N
128: );
129:
130: return Collections.singletonList(ed);
131: }
132:
133: public String getId() {
134: return getClass().getName();
135: }
136:
137: public String getDisplayName() {
138: return NbBundle
139: .getMessage(DoubleCheck.class, "MSG_DoubleCheck"); // NOI18N
140: }
141:
142: public String getDescription() {
143: return NbBundle.getMessage(DoubleCheck.class,
144: "HINT_DoubleCheck"); // NOI18N
145: }
146:
147: public void cancel() {
148: stop = true;
149: }
150:
151: public Preferences getPreferences() {
152: return null;
153: }
154:
155: @Override
156: public JComponent getCustomizer(Preferences node) {
157: return null;
158: }
159:
160: private IfTree findOuterIf(CompilationInfo compilationInfo,
161: TreePath treePath) {
162: while (!stop) {
163: treePath = treePath.getParentPath();
164: if (treePath == null) {
165: break;
166: }
167: Tree leaf = treePath.getLeaf();
168:
169: if (leaf.getKind() == Kind.IF) {
170: return (IfTree) leaf;
171: }
172:
173: if (leaf.getKind() == Kind.BLOCK) {
174: BlockTree b = (BlockTree) leaf;
175: if (b.getStatements().size() == 1) {
176: // ok, empty blocks can be around synchronized(this)
177: // statements
178: continue;
179: }
180: }
181:
182: return null;
183: }
184: return null;
185: }
186:
187: private boolean sameIf(StatementTree statement, IfTree second) {
188: if (statement.getKind() != Kind.IF) {
189: return false;
190: }
191:
192: IfTree first = (IfTree) statement;
193:
194: if (first.getElseStatement() != null) {
195: return false;
196: }
197: if (second.getElseStatement() != null) {
198: return false;
199: }
200:
201: ExpressionTree varFirst = equalToNull(first.getCondition());
202: ExpressionTree varSecond = equalToNull(second.getCondition());
203:
204: if (varFirst == null || varSecond == null) {
205: return false;
206: }
207:
208: if (varFirst.getKind() == Kind.IDENTIFIER
209: && varSecond.getKind() == Kind.IDENTIFIER) {
210: IdentifierTree idFirst = (IdentifierTree) varFirst;
211: IdentifierTree idSecond = (IdentifierTree) varSecond;
212:
213: return idFirst.getName().equals(idSecond.getName());
214: }
215:
216: return false;
217: }
218:
219: private ExpressionTree equalToNull(ExpressionTree t) {
220: if (t.getKind() == Kind.PARENTHESIZED) {
221: ParenthesizedTree p = (ParenthesizedTree) t;
222: t = p.getExpression();
223: }
224:
225: if (t.getKind() != Kind.EQUAL_TO) {
226: return null;
227: }
228: BinaryTree bt = (BinaryTree) t;
229: if (bt.getLeftOperand().getKind() == Kind.NULL_LITERAL
230: && bt.getRightOperand().getKind() != Kind.NULL_LITERAL) {
231: return bt.getRightOperand();
232: }
233: if (bt.getLeftOperand().getKind() != Kind.NULL_LITERAL
234: && bt.getRightOperand().getKind() == Kind.NULL_LITERAL) {
235: return bt.getLeftOperand();
236: }
237: return null;
238: }
239:
240: private static final class FixImpl implements Fix,
241: Task<WorkingCopy> {
242: private TreePathHandle synchHandle;
243: private TreePathHandle ifHandle;
244: private FileObject file;
245:
246: public FixImpl(TreePathHandle synchHandle,
247: TreePathHandle ifHandle, FileObject file) {
248: this .synchHandle = synchHandle;
249: this .ifHandle = ifHandle;
250: this .file = file;
251: }
252:
253: public String getText() {
254: return NbBundle.getMessage(DoubleCheck.class,
255: "MSG_DoubleCheck"); // NOI18N
256: }
257:
258: public ChangeInfo implement() throws IOException {
259: ModificationResult result = JavaSource.forFileObject(file)
260: .runModificationTask(this );
261: result.commit();
262: return null;
263: }
264:
265: @Override
266: public String toString() {
267: return "FixDoubleCheck"; // NOI18N
268: }
269:
270: public void run(WorkingCopy wc) throws Exception {
271: wc.toPhase(JavaSource.Phase.RESOLVED);
272: Tree syncTree = synchHandle.resolve(wc).getLeaf();
273: Tree ifTree = ifHandle.resolve(wc).getLeaf();
274: wc.rewrite(ifTree, syncTree);
275: }
276: }
277:
278: }
|