1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.lang.reflect.Modifier ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.dom.AST; 18 import org.eclipse.jdt.core.dom.ASTNode; 19 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; 20 import org.eclipse.jdt.core.dom.CompilationUnit; 21 import org.eclipse.jdt.core.dom.Expression; 22 import org.eclipse.jdt.core.dom.IBinding; 23 import org.eclipse.jdt.core.dom.ITypeBinding; 24 import org.eclipse.jdt.core.dom.IVariableBinding; 25 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 26 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 27 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 28 29 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; 30 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 31 import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer; 32 33 import org.eclipse.jdt.internal.ui.JavaPluginImages; 34 35 public class AddArgumentCorrectionProposal extends LinkedCorrectionProposal { 36 37 private int[] fInsertIndexes; 38 private ITypeBinding[] fParamTypes; 39 private ASTNode fCallerNode; 40 41 public AddArgumentCorrectionProposal(String label, ICompilationUnit cu, ASTNode callerNode, int[] insertIdx, ITypeBinding[] expectedTypes, int relevance) { 42 super(label, cu, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); 43 fCallerNode= callerNode; 44 fInsertIndexes= insertIdx; 45 fParamTypes= expectedTypes; 46 } 47 48 51 protected ASTRewrite getRewrite() { 52 AST ast= fCallerNode.getAST(); 53 ASTRewrite rewrite= ASTRewrite.create(ast); 54 ChildListPropertyDescriptor property= getProperty(); 55 56 for (int i= 0; i < fInsertIndexes.length; i++) { 57 int idx= fInsertIndexes[i]; 58 String key= "newarg_" + i; Expression newArg= evaluateArgumentExpressions(ast, fParamTypes[idx], key); 60 ListRewrite listRewriter= rewrite.getListRewrite(fCallerNode, property); 61 listRewriter.insertAt(newArg, idx, null); 62 63 addLinkedPosition(rewrite.track(newArg), i == 0, key); 64 } 65 return rewrite; 66 } 67 68 private ChildListPropertyDescriptor getProperty() { 69 List list= fCallerNode.structuralPropertiesForType(); 70 for (int i= 0; i < list.size(); i++) { 71 StructuralPropertyDescriptor curr= (StructuralPropertyDescriptor) list.get(i); 72 if (curr.isChildListProperty() && "arguments".equals(curr.getId())) { return (ChildListPropertyDescriptor) curr; 74 } 75 } 76 return null; 77 78 } 79 80 81 private Expression evaluateArgumentExpressions(AST ast, ITypeBinding requiredType, String key) { 82 CompilationUnit root= (CompilationUnit) fCallerNode.getRoot(); 83 84 int offset= fCallerNode.getStartPosition(); 85 Expression best= null; 86 ITypeBinding bestType= null; 87 88 ScopeAnalyzer analyzer= new ScopeAnalyzer(root); 89 IBinding[] bindings= analyzer.getDeclarationsInScope(offset, ScopeAnalyzer.VARIABLES); 90 for (int i= 0; i < bindings.length; i++) { 91 IVariableBinding curr= (IVariableBinding) bindings[i]; 92 ITypeBinding type= curr.getType(); 93 if (type != null && canAssign(type, requiredType) && testModifier(curr)) { 94 if (best == null || isMoreSpecific(bestType, type)) { 95 best= ast.newSimpleName(curr.getName()); 96 bestType= type; 97 } 98 addLinkedPositionProposal(key, curr.getName(), null); 99 } 100 } 101 Expression defaultExpression= ASTNodeFactory.newDefaultExpression(ast, requiredType); 102 if (best == null) { 103 best= defaultExpression; 104 } 105 addLinkedPositionProposal(key, ASTNodes.asString(defaultExpression), null); 106 return best; 107 } 108 109 private boolean isMoreSpecific(ITypeBinding best, ITypeBinding curr) { 110 return (canAssign(best, curr) && !canAssign(curr, best)); 111 } 112 113 114 private boolean canAssign(ITypeBinding curr, ITypeBinding best) { 115 return curr.isAssignmentCompatible(best); 116 } 117 118 private boolean testModifier(IVariableBinding curr) { 119 int modifiers= curr.getModifiers(); 120 int staticFinal= Modifier.STATIC | Modifier.FINAL; 121 if ((modifiers & staticFinal) == staticFinal) { 122 return false; 123 } 124 if (Modifier.isStatic(modifiers) && !ASTResolving.isInStaticContext(fCallerNode)) { 125 return false; 126 } 127 return true; 128 } 129 } 130 | Popular Tags |