1 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 ; 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 44 final class AddCastHint implements Fix { 45 46 private JavaSource js; 47 private String treeName; 48 private String type; 49 private int position; 50 51 public AddCastHint(JavaSource js, String treeName, String 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 { 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 e) { 78 ErrorManager.getDefault().notify(e); 79 } 80 81 return null; 82 } 83 84 public String getText() { 85 return "Cast " + treeName + " to " + type; 86 } 87 88 } 89 | Popular Tags |