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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.java.hints.errors;
041:
042: import com.sun.source.tree.BlockTree;
043: import com.sun.source.tree.CatchTree;
044: import com.sun.source.tree.ExpressionTree;
045: import com.sun.source.tree.IdentifierTree;
046: import com.sun.source.tree.Scope;
047: import com.sun.source.tree.StatementTree;
048: import com.sun.source.tree.Tree;
049: import com.sun.source.tree.Tree.Kind;
050: import com.sun.source.tree.TryTree;
051: import com.sun.source.tree.VariableTree;
052: import com.sun.source.util.TreePath;
053: import com.sun.source.util.TreePathScanner;
054: import java.util.ArrayList;
055: import java.util.Arrays;
056: import java.util.Collections;
057: import java.util.EnumSet;
058: import java.util.HashSet;
059: import java.util.LinkedList;
060: import java.util.List;
061: import java.util.Set;
062: import javax.lang.model.element.Element;
063: import javax.lang.model.element.ElementKind;
064: import javax.lang.model.element.Modifier;
065: import javax.lang.model.element.TypeElement;
066: import javax.lang.model.element.VariableElement;
067: import javax.lang.model.type.TypeMirror;
068: import org.netbeans.api.java.source.CompilationInfo;
069: import org.netbeans.api.java.source.ElementUtilities.ElementAcceptor;
070: import org.netbeans.api.java.source.JavaSource;
071: import org.netbeans.api.java.source.JavaSource.Phase;
072: import org.netbeans.api.java.source.Task;
073: import org.netbeans.api.java.source.TreeMaker;
074: import org.netbeans.api.java.source.TreePathHandle;
075: import org.netbeans.api.java.source.TypeMirrorHandle;
076: import org.netbeans.api.java.source.WorkingCopy;
077: import org.netbeans.spi.editor.hints.ChangeInfo;
078: import org.netbeans.spi.editor.hints.Fix;
079: import org.openide.util.NbBundle;
080:
081: /**
082: *
083: * @author Jan Lahoda
084: */
085: class OrigSurroundWithTryCatchFix implements Fix {
086:
087: private JavaSource javaSource;
088: private List<TypeMirrorHandle> thandles;
089: private TreePathHandle path;
090:
091: public OrigSurroundWithTryCatchFix(JavaSource javaSource,
092: List<TypeMirrorHandle> thandles, TreePathHandle path) {
093: this .javaSource = javaSource;
094: this .thandles = thandles;
095: this .path = path;
096: }
097:
098: public String getText() {
099: return NbBundle.getMessage(MagicSurroundWithTryCatchFix.class,
100: "LBL_SurroundStatementWithTryCatch");
101: }
102:
103: public ChangeInfo implement() throws Exception {
104: javaSource.runModificationTask(new Task<WorkingCopy>() {
105: public void run(WorkingCopy parameter) throws Exception {
106: parameter.toPhase(Phase.RESOLVED);
107:
108: TreePath p = path.resolve(parameter);
109:
110: if (p == null) {
111: return;//XXX: log
112: }
113:
114: p = findStatement(p);
115:
116: if (p == null) {
117: return; //XXX: log
118: }
119:
120: StatementTree leaf = (StatementTree) p.getLeaf();
121: TreeMaker make = parameter.getTreeMaker();
122:
123: if (leaf.getKind() == Kind.VARIABLE) {
124: //may be necessary to separate variable declaration and assignment:
125: Element e = parameter.getTrees().getElement(p);
126: VariableTree vt = (VariableTree) leaf;
127: long start = parameter.getTrees()
128: .getSourcePositions().getStartPosition(
129: parameter.getCompilationUnit(), vt);
130:
131: if (e != null
132: && e.getKind() == ElementKind.LOCAL_VARIABLE) {
133: TreePath block = findBlock(p);
134:
135: if (block != null) {
136: boolean sep = new FindUsages(leaf,
137: parameter).scan(block,
138: (VariableElement) e) == Boolean.TRUE;
139:
140: if (sep) {
141: BlockTree bt = (BlockTree) block
142: .getLeaf();
143: int index = bt.getStatements().indexOf(
144: leaf);
145:
146: assert index != (-1);
147:
148: int skipIndex = index + 1;
149:
150: while (bt.getStatements().size() > skipIndex) {
151: StatementTree s = bt
152: .getStatements().get(
153: skipIndex);
154:
155: if (s.getKind() != Kind.VARIABLE) {
156: break;
157: }
158:
159: if (start != parameter
160: .getTrees()
161: .getSourcePositions()
162: .getStartPosition(
163: parameter
164: .getCompilationUnit(),
165: vt))
166: break;
167:
168: skipIndex++;
169: }
170:
171: StatementTree assignment = make
172: .ExpressionStatement(make
173: .Assignment(
174: make
175: .Identifier(vt
176: .getName()),
177: vt
178: .getInitializer()));
179: StatementTree declaration = make
180: .Variable(vt.getModifiers(), vt
181: .getName(), vt
182: .getType(), null);//XXX: mask out final
183: TryTree tryTree = make
184: .Try(
185: make
186: .Block(
187: Collections
188: .singletonList(assignment),
189: false),
190: MagicSurroundWithTryCatchFix
191: .createCatches(
192: parameter,
193: make,
194: thandles,
195: p),
196: null);
197: List<StatementTree> nueStatements = new LinkedList<StatementTree>();
198:
199: nueStatements.addAll(bt.getStatements()
200: .subList(0, index));
201: nueStatements.add(declaration);
202: nueStatements.addAll(bt.getStatements()
203: .subList(index + 1, skipIndex));
204: nueStatements.add(tryTree);
205: nueStatements.addAll(bt.getStatements()
206: .subList(
207: skipIndex,
208: bt.getStatements()
209: .size()));
210:
211: parameter.rewrite(bt, make.Block(
212: nueStatements, false));
213: return;
214: }
215: }
216: }
217: }
218:
219: Tree tryTree = make.Try(make.Block(Collections
220: .singletonList(leaf), false),
221: MagicSurroundWithTryCatchFix.createCatches(
222: parameter, make, thandles, p), null);
223:
224: parameter.rewrite(leaf, tryTree);
225: }
226: }).commit();
227: return null;
228: }
229:
230: private TreePath findStatement(TreePath path) {
231: while (path != null
232: && !StatementTree.class.isAssignableFrom(path.getLeaf()
233: .getKind().asInterface())) {
234: path = path.getParentPath();
235: }
236:
237: return path;
238: }
239:
240: private TreePath findBlock(TreePath path) {
241: while (path != null && path.getLeaf().getKind() != Kind.BLOCK) {
242: path = path.getParentPath();
243: }
244:
245: return path;
246: }
247:
248: private static final class FindUsages extends
249: TreePathScanner<Boolean, VariableElement> {
250:
251: private Tree ignore;
252: private CompilationInfo info;
253:
254: public FindUsages(Tree ignore, CompilationInfo info) {
255: this .ignore = ignore;
256: this .info = info;
257: }
258:
259: @Override
260: public Boolean visitIdentifier(IdentifierTree node,
261: VariableElement p) {
262: return p.equals(info.getTrees()
263: .getElement(getCurrentPath()));
264: }
265:
266: @Override
267: public Boolean scan(Tree tree, VariableElement p) {
268: if (tree == ignore)
269: return false;
270:
271: return super .scan(tree, p);
272: }
273:
274: @Override
275: public Boolean reduce(Boolean r1, Boolean r2) {
276: return r1 == Boolean.TRUE || r2 == Boolean.TRUE;
277: }
278:
279: }
280:
281: }
|