]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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 org.eclipse.core.runtime.Assert; | |
14 | import org.eclipse.core.runtime.CoreException; | |
15 | import org.eclipse.core.runtime.NullProgressMonitor; | |
16 | ||
17 | import org.eclipse.jface.viewers.StyledString; | |
18 | ||
19 | import org.eclipse.jface.text.BadLocationException; | |
20 | import org.eclipse.jface.text.Document; | |
21 | import org.eclipse.jface.text.IDocument; | |
22 | import org.eclipse.jface.text.IRegion; | |
23 | import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4; | |
24 | ||
25 | import org.eclipse.jdt.core.ICompilationUnit; | |
26 | import org.eclipse.jdt.core.IJavaProject; | |
27 | import org.eclipse.jdt.core.dom.ASTNode; | |
28 | import org.eclipse.jdt.core.dom.ASTParser; | |
29 | import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; | |
30 | import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; | |
31 | import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; | |
32 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
33 | import org.eclipse.jdt.core.dom.IMethodBinding; | |
34 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
35 | import org.eclipse.jdt.core.dom.NodeFinder; | |
36 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
37 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
38 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; | |
39 | import org.eclipse.jdt.core.formatter.IndentManipulation; | |
40 | ||
41 | import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; | |
42 | import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; | |
43 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; | |
44 | import org.eclipse.jdt.internal.corext.dom.Bindings; | |
45 | ||
46 | import org.eclipse.jdt.ui.SharedASTProvider; | |
47 | ||
48 | import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider; | |
49 | import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; | |
50 | ||
51 | public class OverrideCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 { | |
52 | ||
53 | public IJavaProject fJavaProject; | |
54 | private String fMethodName; | |
55 | private String[] fParamTypes; | |
56 | ||
57 | public OverrideCompletionProposal(IJavaProject jproject, ICompilationUnit cu, String methodName, String[] paramTypes, int start, int length, StyledString displayName, String completionProposal) { | |
58 | super(completionProposal, cu, start, length, null, displayName, 0); | |
59 | Assert.isNotNull(jproject); | |
60 | Assert.isNotNull(methodName); | |
61 | Assert.isNotNull(paramTypes); | |
62 | Assert.isNotNull(cu); | |
63 | ||
64 | fParamTypes= paramTypes; | |
65 | fMethodName= methodName; | |
66 | ||
67 | fJavaProject= jproject; | |
68 | ||
69 | StringBuffer buffer= new StringBuffer(); | |
70 | buffer.append(completionProposal); | |
71 | buffer.append(" {};"); //$NON-NLS-1$ | |
72 | ||
73 | setReplacementString(buffer.toString()); | |
74 | } | |
75 | ||
76 | /* | |
77 | * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionText(org.eclipse.jface.text.IDocument,int) | |
78 | */ | |
79 | @Override | |
80 | public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { | |
81 | return fMethodName; | |
82 | } | |
83 | ||
84 | private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) { | |
85 | CompilationUnit ast= SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_ACTIVE_ONLY, null); | |
86 | if (ast != null) { | |
87 | recoveredDocument.set(document.get()); | |
88 | return ast; | |
89 | } | |
90 | ||
91 | char[] content= document.get().toCharArray(); | |
92 | ||
93 | // clear prefix to avoid compile errors | |
94 | int index= offset - 1; | |
95 | while (index >= 0 && Character.isJavaIdentifierPart(content[index])) { | |
96 | content[index]= ' '; | |
97 | index--; | |
98 | } | |
99 | ||
100 | recoveredDocument.set(new String(content)); | |
101 | ||
102 | final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); | |
103 | parser.setResolveBindings(true); | |
104 | parser.setStatementsRecovery(true); | |
105 | parser.setSource(content); | |
106 | parser.setUnitName(fCompilationUnit.getElementName()); | |
107 | parser.setProject(fCompilationUnit.getJavaProject()); | |
108 | return (CompilationUnit) parser.createAST(new NullProgressMonitor()); | |
109 | } | |
110 | ||
111 | /* | |
112 | * @see JavaTypeCompletionProposal#updateReplacementString(IDocument,char,int,ImportRewrite) | |
113 | */ | |
114 | @Override | |
115 | protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite importRewrite) throws CoreException, BadLocationException { | |
116 | Document recoveredDocument= new Document(); | |
117 | CompilationUnit unit= getRecoveredAST(document, offset, recoveredDocument); | |
118 | ImportRewriteContext context; | |
119 | if (importRewrite != null) { | |
120 | context= new ContextSensitiveImportRewriteContext(unit, offset, importRewrite); | |
121 | } else { | |
122 | importRewrite= StubUtility.createImportRewrite(unit, true); // create a dummy import rewriter to have one | |
123 | context= new ImportRewriteContext() { // forces that all imports are fully qualified | |
124 | @Override | |
125 | public int findInContext(String qualifier, String name, int kind) { | |
126 | return RES_NAME_CONFLICT; | |
127 | } | |
128 | }; | |
129 | } | |
130 | ||
131 | ITypeBinding declaringType= null; | |
132 | ChildListPropertyDescriptor descriptor= null; | |
133 | ASTNode node= NodeFinder.perform(unit, offset, 1); | |
134 | if (node instanceof AnonymousClassDeclaration) { | |
135 | declaringType= ((AnonymousClassDeclaration) node).resolveBinding(); | |
136 | descriptor= AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY; | |
137 | } else if (node instanceof AbstractTypeDeclaration) { | |
138 | AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) node; | |
139 | descriptor= declaration.getBodyDeclarationsProperty(); | |
140 | declaringType= declaration.resolveBinding(); | |
141 | } | |
142 | if (declaringType != null) { | |
143 | ASTRewrite rewrite= ASTRewrite.create(unit.getAST()); | |
144 | IMethodBinding methodToOverride= Bindings.findMethodInHierarchy(declaringType, fMethodName, fParamTypes); | |
145 | if (methodToOverride == null && declaringType.isInterface()) { | |
146 | methodToOverride= Bindings.findMethodInType(node.getAST().resolveWellKnownType("java.lang.Object"), fMethodName, fParamTypes); //$NON-NLS-1$ | |
147 | } | |
148 | if (methodToOverride != null) { | |
149 | CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fJavaProject); | |
150 | settings.generated_9161868182717985500(document, importRewrite, recoveredDocument, context, declaringType, descriptor, node, rewrite, methodToOverride, this); | |
151 | } | |
152 | } | |
153 | return true; | |
154 | } | |
155 | ||
156 | public static String getIndentAt(IDocument document, int offset, CodeGenerationSettings settings) { | |
157 | try { | |
158 | IRegion region= document.getLineInformationOfOffset(offset); | |
159 | return IndentManipulation.extractIndentString(document.get(region.getOffset(), region.getLength()), settings.tabWidth, settings.indentWidth); | |
160 | } catch (BadLocationException e) { | |
161 | return ""; //$NON-NLS-1$ | |
162 | } | |
163 | } | |
164 | ||
165 | /* | |
166 | * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable() | |
167 | */ | |
168 | public boolean isAutoInsertable() { | |
169 | return false; | |
170 | } | |
171 | } |