2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.java.hints.errors;
44
import com.sun.source.tree.ExpressionTree;
45
import com.sun.source.tree.Tree;
46
import com.sun.source.tree.VariableTree;
47
import java.io.IOException;
48
import javax.lang.model.type.TypeMirror;
49
import org.netbeans.api.java.source.Task;
50
import org.netbeans.api.java.source.JavaSource;
51
import org.netbeans.api.java.source.TreeMaker;
52
import org.netbeans.api.java.source.JavaSource.Phase;
53
import org.netbeans.api.java.source.WorkingCopy;
54
import org.netbeans.spi.editor.hints.ChangeInfo;
55
import org.netbeans.spi.editor.hints.Fix;
56
import org.openide.ErrorManager;
57
import org.openide.util.NbBundle;
61
* This hint changes the type of a variable to the type of
62
* initializer expression. In effect it works opposite of
65
* @author Sandip Chitale
67
final class ChangeTypeFix implements Fix {
69
private JavaSource js;
70
private String treeName;
74
public ChangeTypeFix(JavaSource js, String treeName, String type, int position) {
76
this.treeName = treeName;
78
this.position = position;
81
public ChangeInfo implement() throws IOException {
82
js.runModificationTask(new Task<WorkingCopy>() {
83
public void run(final WorkingCopy working) throws IOException {
84
working.toPhase(Phase.RESOLVED);
85
TypeMirror[] tm = new TypeMirror[1];
86
ExpressionTree[] expression = new ExpressionTree[1];
87
TypeMirror[] expressionType = new TypeMirror[1];
88
Tree[] leaf = new Tree[1];
90
ChangeType.computeType(working, position, tm, expression, expressionType, leaf);
92
if (leaf[0] instanceof VariableTree) {
93
VariableTree oldVariableTree = ((VariableTree)leaf[0]);
94
TreeMaker make = working.getTreeMaker();
96
VariableTree newVariableTree = make.Variable(
97
oldVariableTree.getModifiers(),
98
oldVariableTree.getName(),
99
make.Type(expressionType[0]),
100
oldVariableTree.getInitializer());
102
working.rewrite(leaf[0], newVariableTree);
110
public String getText() {
111
return NbBundle.getMessage(ChangeTypeFix.class, "MSG_ChangeVariablesType", treeName, type); // NOI18N