KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > AddCastHint


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.hints;
21
22 import com.sun.source.tree.AssignmentTree;
23 import com.sun.source.tree.ExpressionTree;
24 import com.sun.source.tree.ReturnTree;
25 import com.sun.source.tree.Tree;
26 import com.sun.source.tree.VariableTree;
27 import com.sun.source.util.TreeScanner;
28 import java.io.IOException JavaDoc;
29 import javax.lang.model.type.TypeMirror;
30 import org.netbeans.api.java.source.CancellableTask;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.TreeMaker;
33 import org.netbeans.api.java.source.JavaSource.Phase;
34 import org.netbeans.api.java.source.WorkingCopy;
35 import org.netbeans.spi.editor.hints.ChangeInfo;
36 import org.netbeans.spi.editor.hints.Fix;
37 import org.openide.ErrorManager;
38
39
40 /**
41  *
42  * @author Jan Lahoda
43  */

44 final class AddCastHint implements Fix {
45     
46     private JavaSource js;
47     private String JavaDoc treeName;
48     private String JavaDoc type;
49     private int position;
50     
51     public AddCastHint(JavaSource js, String JavaDoc treeName, String JavaDoc type, int position) {
52         this.js = js;
53         this.treeName = treeName;
54         this.type = type;
55         this.position = position;
56     }
57     
58     public ChangeInfo implement() {
59         try {
60             js.runModificationTask(new CancellableTask<WorkingCopy>() {
61                 public void cancel() {
62                 }
63                 public void run(final WorkingCopy working) throws IOException JavaDoc {
64                     working.toPhase(Phase.RESOLVED);
65                     TypeMirror[] tm = new TypeMirror[1];
66                     ExpressionTree[] expression = new ExpressionTree[1];
67                     Tree[] leaf = new Tree[1];
68                     
69                     AddCastCreator.computeType(working, position, tm, expression, leaf);
70                     
71                     TreeMaker make = working.getTreeMaker();
72                     ExpressionTree cast = make.TypeCast(make.Type(tm[0]), expression[0]);
73                     
74                     working.rewrite(expression[0], cast);
75                 }
76             }).commit();
77         } catch (IOException JavaDoc e) {
78             ErrorManager.getDefault().notify(e);
79         }
80         
81         return null;
82     }
83     
84     public String JavaDoc getText() {
85         return "Cast " + treeName + " to " + type;
86     }
87
88 }
89
Popular Tags