/******************************************************************************* * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.jdt.internal.ui.text.java; import java.util.Collection; import java.util.Set; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.viewers.StyledString; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil; import org.eclipse.jdt.ui.JavaElementImageDescriptor; import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; /** * Method declaration proposal. */ public class MethodDeclarationCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 { public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set suggestedMethods, Collection result) throws CoreException { IMethod[] methods= type.getMethods(); if (!type.isInterface()) { String constructorName= type.getElementName(); if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) { result.add(new MethodDeclarationCompletionProposal(type, constructorName, null, offset, length, relevance + 500)); } } if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { //$NON-NLS-1$ if (!JavaConventionsUtil.validateMethodName(prefix, type).matches(IStatus.ERROR)) result.add(new MethodDeclarationCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance)); } } private static boolean hasMethod(IMethod[] methods, String name) { for (int i= 0; i < methods.length; i++) { IMethod curr= methods[i]; if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) { return true; } } return false; } public final IType fType; public final String fReturnTypeSig; public final String fMethodName; public MethodDeclarationCompletionProposal(IType type, String methodName, String returnTypeSig, int start, int length, int relevance) { super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); //$NON-NLS-1$ Assert.isNotNull(type); Assert.isNotNull(methodName); fType= type; fMethodName= methodName; fReturnTypeSig= returnTypeSig; if (returnTypeSig == null) { setProposalInfo(new ProposalInfo(type)); ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE); setImage(JavaPlugin.getImageDescriptorRegistry().get(desc)); } else { setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE)); } } private static StyledString getDisplayName(String methodName, String returnTypeSig) { StyledString buf= new StyledString(); buf.append(methodName); buf.append('('); buf.append(')'); if (returnTypeSig != null) { buf.append(" : "); //$NON-NLS-1$ buf.append(Signature.toString(returnTypeSig)); buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$ buf.append(JavaTextMessages.MethodCompletionProposal_method_label, StyledString.QUALIFIER_STYLER); } else { buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$ buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label, StyledString.QUALIFIER_STYLER); } return buf; } /* (non-Javadoc) * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite) */ @Override protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject()); settings.generated_1584584742950606520(document, this); return true; } @Override public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { return new String(); // don't let method stub proposals complete incrementally } /* * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable() */ public boolean isAutoInsertable() { return false; } }