Java Code Examples for org.eclipse.jdt.core.CompletionProposal#getSignature()

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#getSignature() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Resolves the method described by the receiver and returns it if found.
 * Returns <code>null</code> if no corresponding member can be found.
 *
 * @param proposal
 *            - completion proposal
 * @param javaProject
 *            - Java project
 *
 * @return the resolved method or <code>null</code> if none is found
 * @throws JavaModelException
 *             if accessing the java model fails
 */

public static IMethod resolveMethod(CompletionProposal proposal, IJavaProject javaProject) throws JavaModelException {
	char[] declarationSignature = proposal.getDeclarationSignature();
	String typeName = SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type = javaProject.findType(typeName);
	if (type != null) {
		String name = String.valueOf(proposal.getName());
		if (proposal.getKind() == CompletionProposal.ANNOTATION_ATTRIBUTE_REF) {
			IMethod method = type.getMethod(name, CharOperation.NO_STRINGS);
			if (method.exists()) {
				return method;
			} else {
				return null;
			}
		}
		char[] signature = proposal.getSignature();
		if (proposal instanceof InternalCompletionProposal) {
			Binding binding = ((InternalCompletionProposal) proposal).getBinding();
			if (binding instanceof MethodBinding) {
				MethodBinding methodBinding = (MethodBinding) binding;
				MethodBinding original = methodBinding.original();
				if (original != binding) {
					signature = Engine.getSignature(original);
				}
			}
		}
		String[] parameters = Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(signature)));
		for (int i = 0; i < parameters.length; i++) {
			parameters[i] = SignatureUtil.getLowerBound(parameters[i]);
		}
		boolean isConstructor = proposal.isConstructor();

		return JavaModelUtil.findMethod(name, parameters, isConstructor, type);
	}

	return null;
}
 
Example 2
Source File: JavaElExpressionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ICompletionProposal createProposal(CompletionProposal javaProposal) {
  char[] signature = javaProposal.getSignature();
  String replacementText = null;
  int relevanceAdjustment = 0;

  if (javaProposal.getKind() != CompletionProposal.METHOD_REF) {
    return null;
  }

  if (Signature.getParameterCount(signature) != 0) {
    // Only zero-arg methods are allowed
    return null;
  }

  String returnType = String.valueOf(Signature.getReturnType(signature));
  if (Signature.SIG_VOID.equals(returnType)) {
    // Methods with void return type are not allowed
    return null;
  }

  relevanceAdjustment += getRelevanceAdjustmentForMyTypeAndDeclarationType(
      returnType, javaProposal.getDeclarationSignature());
  replacementText = String.valueOf(javaProposal.getName());

  IJavaCompletionProposal jdtCompletionProposal = JavaContentAssistUtilities.getJavaCompletionProposal(
      javaProposal, getContext(), getJavaProject());
  ReplacementCompletionProposal proposal = ReplacementCompletionProposal.fromExistingCompletionProposal(
      replacementText, getReplaceOffset(), getReplaceLength(),
      jdtCompletionProposal);

  if (relevanceAdjustment != 0) {
    proposal.setRelevance(proposal.getRelevance() + relevanceAdjustment);
  }

  return proposal;
}
 
Example 3
Source File: JavaElementToken.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public JavaElementToken(String rep, String doc, String args, String parentPackage, int type,
        IJavaElement javaElement, CompletionProposal completionProposal) {
    super(rep, doc, args, parentPackage, type, null);
    this.javaElement = javaElement;
    this.completionProposalKind = completionProposal.getKind();
    this.completionProposalFlags = completionProposal.getFlags();
    if (HAS_ADDITIONAL_FLAGS) {
        this.completionProposalAdditionalFlags = completionProposal.getAdditionalFlags();
    }
    this.completionPropsoalSignature = completionProposal.getSignature();
}
 
Example 4
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private IJavaElement resolveJavaElement(IJavaProject project, CompletionProposal proposal) throws JavaModelException {
	char[] signature= proposal.getSignature();
	String typeName= SignatureUtil.stripSignatureToFQN(String.valueOf(signature));
	return project.findType(typeName);
}
 
Example 5
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private StringBuilder appendImportProposal(StringBuilder buffer, CompletionProposal proposal, int coreKind) {
	int proposalKind= proposal.getKind();
	String qualifiedTypeName= null;
	char[] qualifiedType= null;
	if (proposalKind == CompletionProposal.TYPE_IMPORT) {
		qualifiedType= proposal.getSignature();
		qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
	} else if (proposalKind == CompletionProposal.METHOD_IMPORT || proposalKind == CompletionProposal.FIELD_IMPORT) {
		qualifiedType= Signature.getTypeErasure(proposal.getDeclarationSignature());
		qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
	} else {
		/*
		 * In 3.3 we only support the above import proposals, see
		 * CompletionProposal#getRequiredProposals()
		 */
		Assert.isTrue(false);
	}

	/* Add imports if the preference is on. */
	if (importRewrite != null) {
		if (proposalKind == CompletionProposal.TYPE_IMPORT) {
			String simpleType= importRewrite.addImport(qualifiedTypeName, null);
			if (coreKind == CompletionProposal.METHOD_REF) {
				buffer.append(simpleType);
				buffer.append(COMMA);
				return buffer;
			}
		} else {
			String res= importRewrite.addStaticImport(qualifiedTypeName, String.valueOf(proposal.getName()), proposalKind == CompletionProposal.FIELD_IMPORT, null);
			int dot= res.lastIndexOf('.');
			if (dot != -1) {
				buffer.append(importRewrite.addImport(res.substring(0, dot), null));
				buffer.append('.');
				return buffer;
			}
		}
		return buffer;
	}

	// Case where we don't have an import rewrite (see allowAddingImports)

	if (compilationUnit != null && isImplicitImport(Signature.getQualifier(qualifiedTypeName), compilationUnit)) {
		/* No imports for implicit imports. */

		if (proposal.getKind() == CompletionProposal.TYPE_IMPORT && coreKind == CompletionProposal.FIELD_REF) {
			return buffer;
		}
		qualifiedTypeName= String.valueOf(Signature.getSignatureSimpleName(qualifiedType));
	}
	buffer.append(qualifiedTypeName);
	buffer.append('.');
	return buffer;
}
 
Example 6
Source File: JavaTypeCompletionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void accept(CompletionProposal proposal) {
	switch (proposal.getKind()) {
		case CompletionProposal.PACKAGE_REF :
			char[] packageName= proposal.getDeclarationSignature();
			if (TypeFilter.isFiltered(packageName))
				return;
			addAdjustedCompletion(
					new String(packageName),
					new String(proposal.getCompletion()),
					proposal.getReplaceStart(),
					proposal.getReplaceEnd(),
					proposal.getRelevance(),
					JavaPluginImages.DESC_OBJS_PACKAGE);
			return;

		case CompletionProposal.TYPE_REF :
			char[] signature= proposal.getSignature();
			char[] fullName= Signature.toCharArray(signature);
			if (TypeFilter.isFiltered(fullName))
				return;
			StringBuffer buf= new StringBuffer();
			buf.append(Signature.getSimpleName(fullName));
			if (buf.length() == 0)
				return; // this is the dummy class, whose $ have been converted to dots
			char[] typeQualifier= Signature.getQualifier(fullName);
			if (typeQualifier.length > 0) {
				buf.append(JavaElementLabels.CONCAT_STRING);
				buf.append(typeQualifier);
			}
			String name= buf.toString();

			// Only fully qualify if it's a top level type:
			boolean fullyQualify= fFullyQualify && CharOperation.equals(proposal.getDeclarationSignature(), typeQualifier);

			ImageDescriptor typeImageDescriptor;
			switch (Signature.getTypeSignatureKind(signature)) {
				case Signature.TYPE_VARIABLE_SIGNATURE :
					typeImageDescriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
					break;
				case Signature.CLASS_TYPE_SIGNATURE :
					typeImageDescriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, proposal.getFlags(), false);
					break;
				default :
					typeImageDescriptor= null;
			}

			addAdjustedTypeCompletion(
					name,
					new String(proposal.getCompletion()),
					proposal.getReplaceStart(),
					proposal.getReplaceEnd(),
					proposal.getRelevance(),
					typeImageDescriptor,
					fullyQualify ? new String(fullName) : null);
			return;

		case CompletionProposal.KEYWORD:
			if (! fEnableBaseTypes)
				return;
			String keyword= new String(proposal.getName());
			if ( (fEnableVoid && VOID.equals(keyword)) || (fEnableBaseTypes && BASE_TYPES.contains(keyword)) )
				addAdjustedCompletion(
						keyword,
						new String(proposal.getCompletion()),
						proposal.getReplaceStart(),
						proposal.getReplaceEnd(),
						proposal.getRelevance(),
						null);
			return;

		default :
			return;
	}

}
 
Example 7
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Updates a display label for a given type proposal. The display label
 * consists of:
 * <ul>
 *   <li>the simple type name (erased when the context is in javadoc)</li>
 *   <li>the package name</li>
 * </ul>
 * <p>
 * Examples:
 * A proposal for the generic type <code>java.util.List&lt;E&gt;</code>, the display label
 * is: <code>List<E> - java.util</code>.
 * </p>
 *
 * @param typeProposal the method proposal to display
 * @param item the completion to update
 */
private void createTypeProposalLabel(CompletionProposal typeProposal, CompletionItem item) {
	char[] signature;
	if (fContext != null && fContext.isInJavadoc()) {
		signature= Signature.getTypeErasure(typeProposal.getSignature());
	} else {
		signature= typeProposal.getSignature();
	}
	char[] fullName= Signature.toCharArray(signature);
	createTypeProposalLabel(fullName, item);
	setDeclarationSignature(item, String.valueOf(signature));
}
 
Example 8
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Creates a display label for a given type proposal. The display label
 * consists of:
 * <ul>
 *   <li>the simple type name (erased when the context is in javadoc)</li>
 *   <li>the package name</li>
 * </ul>
 * <p>
 * Examples:
 * A proposal for the generic type <code>java.util.List&lt;E&gt;</code>, the display label
 * is: <code>List<E> - java.util</code>.
 * </p>
 *
 * @param typeProposal the method proposal to display
 * @return the display label for the given type proposal
 */
StyledString createTypeProposalLabel(CompletionProposal typeProposal) {
	char[] signature;
	if (fContext != null && fContext.isInJavadoc())
		signature= Signature.getTypeErasure(typeProposal.getSignature());
	else
		signature= typeProposal.getSignature();
	char[] fullName= Signature.toCharArray(signature);
	return createTypeProposalLabel(fullName);
}
 
Example 9
Source File: JavaContentAssistUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Gets the fully qualified type name from a java completion proposal for
 * {@link CompletionProposal#TYPE_REF}.
 * 
 * @param javaProposal the java type reference completion proposal generated
 *          by a code complete
 * @return the fully qualified type name
 */
public static String getFullyQualifiedTypeName(CompletionProposal javaProposal) {
  char[] signature = javaProposal.getSignature();
  return String.valueOf(Signature.toCharArray(signature));
}