1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 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.CompilationUnit; 20 import org.eclipse.jdt.core.dom.IBinding; 21 import org.eclipse.jdt.core.dom.ITypeBinding; 22 import org.eclipse.jdt.core.dom.Type; 23 import org.eclipse.jdt.core.dom.TypeDeclaration; 24 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 25 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 26 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 27 28 import org.eclipse.jdt.internal.corext.dom.Bindings; 29 import org.eclipse.jdt.internal.corext.util.Messages; 30 31 import org.eclipse.jdt.internal.ui.JavaPluginImages; 32 33 36 public class ImplementInterfaceProposal extends LinkedCorrectionProposal { 37 38 private IBinding fBinding; 39 private CompilationUnit fAstRoot; 40 private ITypeBinding fNewInterface; 41 42 public ImplementInterfaceProposal(ICompilationUnit targetCU, ITypeBinding binding, CompilationUnit astRoot, ITypeBinding newInterface, int relevance) { 43 super("", targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); 45 Assert.isTrue(binding != null && Bindings.isDeclarationBinding(binding)); 46 47 fBinding= binding; 48 fAstRoot= astRoot; 49 fNewInterface= newInterface; 50 51 String [] args= { binding.getName(), Bindings.getRawName(newInterface) }; 52 setDisplayName(Messages.format(CorrectionMessages.ImplementInterfaceProposal_name, args)); 53 } 54 55 protected ASTRewrite getRewrite() throws CoreException { 56 ASTNode boundNode= fAstRoot.findDeclaringNode(fBinding); 57 ASTNode declNode= null; 58 CompilationUnit newRoot= fAstRoot; 59 if (boundNode != null) { 60 declNode= boundNode; } else { 62 newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); 63 declNode= newRoot.findDeclaringNode(fBinding.getKey()); 64 } 65 ImportRewrite imports= createImportRewrite(newRoot); 66 67 if (declNode instanceof TypeDeclaration) { 68 AST ast= declNode.getAST(); 69 ASTRewrite rewrite= ASTRewrite.create(ast); 70 71 Type newInterface= imports.addImport(fNewInterface, ast); 72 ListRewrite listRewrite= rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY); 73 listRewrite.insertLast(newInterface, null); 74 75 final String KEY_TYPE= "type"; addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE); 78 return rewrite; 79 } 80 return null; 81 } 82 83 84 } 85 | Popular Tags |