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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.java;
13 import java.util.Collection;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.StyledString;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
27 import org.eclipse.jdt.core.IMethod;
28 import org.eclipse.jdt.core.IType;
29 import org.eclipse.jdt.core.Signature;
30 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
32 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
33 import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
35 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
36 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.JavaPluginImages;
40 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
41 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
45 * Method declaration proposal.
47 public class MethodDeclarationCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
50 public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException {
51 IMethod[] methods= type.getMethods();
52 if (!type.isInterface()) {
53 String constructorName= type.getElementName();
54 if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) {
55 result.add(new MethodDeclarationCompletionProposal(type, constructorName, null, offset, length, relevance + 500));
59 if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { //$NON-NLS-1$
60 if (!JavaConventionsUtil.validateMethodName(prefix, type).matches(IStatus.ERROR))
61 result.add(new MethodDeclarationCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance));
65 private static boolean hasMethod(IMethod[] methods, String name) {
66 for (int i= 0; i < methods.length; i++) {
67 IMethod curr= methods[i];
68 if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) {
75 public final IType fType;
76 public final String fReturnTypeSig;
77 public final String fMethodName;
79 public MethodDeclarationCompletionProposal(IType type, String methodName, String returnTypeSig, int start, int length, int relevance) {
80 super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); //$NON-NLS-1$
81 Assert.isNotNull(type);
82 Assert.isNotNull(methodName);
85 fMethodName= methodName;
86 fReturnTypeSig= returnTypeSig;
88 if (returnTypeSig == null) {
89 setProposalInfo(new ProposalInfo(type));
91 ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
92 setImage(JavaPlugin.getImageDescriptorRegistry().get(desc));
94 setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE));
98 private static StyledString getDisplayName(String methodName, String returnTypeSig) {
99 StyledString buf= new StyledString();
100 buf.append(methodName);
103 if (returnTypeSig != null) {
104 buf.append(" : "); //$NON-NLS-1$
105 buf.append(Signature.toString(returnTypeSig));
106 buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
107 buf.append(JavaTextMessages.MethodCompletionProposal_method_label, StyledString.QUALIFIER_STYLER);
109 buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
110 buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label, StyledString.QUALIFIER_STYLER);
116 * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
119 protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
121 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
122 settings.generated_1584584742950606520(document, this);
127 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
128 return new String(); // don't let method stub proposals complete incrementally
132 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
134 public boolean isAutoInsertable() {