KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > OverrideCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.java;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.text.edits.MalformedTreeException;
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.Document;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.TextUtilities;
28 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
29
30 import org.eclipse.jdt.core.ICompilationUnit;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.dom.AST;
33 import org.eclipse.jdt.core.dom.ASTNode;
34 import org.eclipse.jdt.core.dom.ASTParser;
35 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
36 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
37 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
38 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
39 import org.eclipse.jdt.core.dom.CompilationUnit;
40 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
41 import org.eclipse.jdt.core.dom.IMethodBinding;
42 import org.eclipse.jdt.core.dom.ITypeBinding;
43 import org.eclipse.jdt.core.dom.MethodDeclaration;
44 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
45 import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition;
46 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
47 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
48 import org.eclipse.jdt.core.formatter.IndentManipulation;
49
50 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
51 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2;
52 import org.eclipse.jdt.internal.corext.dom.Bindings;
53 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
54 import org.eclipse.jdt.internal.corext.util.Strings;
55
56 import org.eclipse.jdt.internal.ui.JavaPlugin;
57 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
58
59 public class OverrideCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
60
61     private IJavaProject fJavaProject;
62     private String JavaDoc fMethodName;
63     private String JavaDoc[] fParamTypes;
64
65     public OverrideCompletionProposal(IJavaProject jproject, ICompilationUnit cu, String JavaDoc methodName, String JavaDoc[] paramTypes, int start, int length, String JavaDoc displayName, String JavaDoc completionProposal) {
66         super(completionProposal, cu, start, length, null, displayName, 0);
67         Assert.isNotNull(jproject);
68         Assert.isNotNull(methodName);
69         Assert.isNotNull(paramTypes);
70         Assert.isNotNull(cu);
71
72         fParamTypes= paramTypes;
73         fMethodName= methodName;
74
75         fJavaProject= jproject;
76         
77         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
78         buffer.append(completionProposal);
79         buffer.append(" {};"); //$NON-NLS-1$
80

81         setReplacementString(buffer.toString());
82     }
83
84     /*
85      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionText(org.eclipse.jface.text.IDocument,int)
86      */

87     public CharSequence JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
88         return fMethodName;
89     }
90
91     /*
92      * @see JavaTypeCompletionProposal#updateReplacementString(IDocument,char,int,ImportRewrite)
93      */

94     protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite importRewrite) throws CoreException, BadLocationException {
95         final IDocument buffer= new Document(document.get());
96         int index= offset - 1;
97         while (index >= 0 && Character.isJavaIdentifierPart(buffer.getChar(index)))
98             index--;
99         final int length= offset - index - 1;
100         buffer.replace(index + 1, length, " "); //$NON-NLS-1$
101
final ASTParser parser= ASTParser.newParser(AST.JLS3);
102         parser.setResolveBindings(true);
103         parser.setStatementsRecovery(true);
104         parser.setSource(buffer.get().toCharArray());
105         parser.setUnitName(fCompilationUnit.getResource().getFullPath().toString());
106         parser.setProject(fCompilationUnit.getJavaProject());
107         final CompilationUnit unit= (CompilationUnit) parser.createAST(new NullProgressMonitor());
108         ITypeBinding binding= null;
109         ChildListPropertyDescriptor descriptor= null;
110         ASTNode node= NodeFinder.perform(unit, index + 1, 0);
111         if (node instanceof AnonymousClassDeclaration) {
112             switch (node.getParent().getNodeType()) {
113                 case ASTNode.CLASS_INSTANCE_CREATION:
114                     binding= ((ClassInstanceCreation) node.getParent()).resolveTypeBinding();
115                     break;
116                 case ASTNode.ENUM_CONSTANT_DECLARATION:
117                     IMethodBinding methodBinding= ((EnumConstantDeclaration) node.getParent()).resolveConstructorBinding();
118                     if (methodBinding != null) {
119                         binding= methodBinding.getDeclaringClass();
120                     }
121             }
122             descriptor= AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
123         } else if (node instanceof AbstractTypeDeclaration) {
124             final AbstractTypeDeclaration declaration= ((AbstractTypeDeclaration) node);
125             descriptor= declaration.getBodyDeclarationsProperty();
126             binding= declaration.resolveBinding();
127         }
128         if (binding != null) {
129             ASTRewrite rewrite= ASTRewrite.create(unit.getAST());
130             IMethodBinding[] bindings= StubUtility2.getOverridableMethods(rewrite.getAST(), binding, true);
131             if (bindings != null && bindings.length > 0) {
132                 List JavaDoc candidates= new ArrayList JavaDoc(bindings.length);
133                 IMethodBinding method= null;
134                 for (index= 0; index < bindings.length; index++) {
135                     if (bindings[index].getName().equals(fMethodName) && bindings[index].getParameterTypes().length == fParamTypes.length)
136                         candidates.add(bindings[index]);
137                 }
138                 if (candidates.size() > 1) {
139                     method= Bindings.findMethodInHierarchy(binding, fMethodName, fParamTypes);
140                     if (method == null) {
141                         ITypeBinding objectType= rewrite.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
142
method= Bindings.findMethodInType(objectType, fMethodName, fParamTypes);
143                     }
144                 } else if (candidates.size() == 1)
145                     method= (IMethodBinding) candidates.get(0);
146                 if (method != null) {
147                     CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fJavaProject);
148                     ListRewrite rewriter= rewrite.getListRewrite(node, descriptor);
149                     String JavaDoc key= method.getKey();
150                     MethodDeclaration stub= null;
151                     for (index= 0; index < bindings.length; index++) {
152                         if (key.equals(bindings[index].getKey())) {
153                             stub= StubUtility2.createImplementationStub(fCompilationUnit, rewrite, importRewrite, bindings[index], binding.getName(), binding.isInterface(), settings);
154                             if (stub != null)
155                                 rewriter.insertFirst(stub, null);
156                             break;
157                         }
158                     }
159                     if (stub != null) {
160                         IDocument contents= new Document(fCompilationUnit.getBuffer().getContents());
161                         IRegion region= contents.getLineInformationOfOffset(getReplacementOffset());
162                         ITrackedNodePosition position= rewrite.track(stub);
163                         String JavaDoc indent= IndentManipulation.extractIndentString(contents.get(region.getOffset(), region.getLength()), settings.tabWidth, settings.indentWidth);
164                         try {
165                             rewrite.rewriteAST(contents, fJavaProject.getOptions(true)).apply(contents, TextEdit.UPDATE_REGIONS);
166                         } catch (MalformedTreeException exception) {
167                             JavaPlugin.log(exception);
168                         } catch (BadLocationException exception) {
169                             JavaPlugin.log(exception);
170                         }
171                         setReplacementString(IndentManipulation.changeIndent(Strings.trimIndentation(contents.get(position.getStartPosition(), position.getLength()), settings.tabWidth, settings.indentWidth, false), 0, settings.tabWidth, settings.indentWidth, indent, TextUtilities.getDefaultLineDelimiter(contents)));
172                     }
173                 }
174             }
175         }
176         return true;
177     }
178
179     /*
180      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
181      */

182     public boolean isAutoInsertable() {
183         return false;
184     }
185 }
186
Popular Tags