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

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#getDeclarationSignature() . 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: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void createAnonymousTypeLabel(CompletionProposal proposal, CompletionItem item) {
	char[] declaringTypeSignature= proposal.getDeclarationSignature();
	declaringTypeSignature= Signature.getTypeErasure(declaringTypeSignature);
	String name = new String(Signature.getSignatureSimpleName(declaringTypeSignature));
	item.setInsertText(name);
	StringBuilder buf= new StringBuilder();
	buf.append(name);
	buf.append('(');
	appendUnboundedParameterList(buf, proposal);
	buf.append(')');
	buf.append("  "); //$NON-NLS-1$
	buf.append("Anonymous Inner Type"); //TODO: consider externalization
	item.setLabel(buf.toString());

	if (proposal.getRequiredProposals() != null) {
		char[] signatureQualifier= Signature.getSignatureQualifier(declaringTypeSignature);
		if (signatureQualifier.length > 0) {
			item.setDetail(String.valueOf(signatureQualifier) + "." + name);
		}
	}
	setDeclarationSignature(item, String.valueOf(declaringTypeSignature));
}
 
Example 2
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Resolves the field 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 field or <code>null</code> if none is found
 * @throws JavaModelException
 *             if accessing the java model fails
 */
public static IField resolveField(CompletionProposal proposal, IJavaProject javaProject) throws JavaModelException {
	char[] declarationSignature = proposal.getDeclarationSignature();
	// for synthetic fields on arrays, declaration signatures may be null
	// TODO remove when https://bugs.eclipse.org/bugs/show_bug.cgi?id=84690 gets fixed
	if (declarationSignature == null) {
		return null;
	}
	String typeName = SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type = javaProject.findType(typeName);
	if (type != null) {
		String name = String.valueOf(proposal.getName());
		IField field = type.getField(name);
		if (field.exists()) {
			return field;
		}
	}

	return null;
}
 
Example 3
Source File: ProposalGeneratingCompletionRequestor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
protected ICompletionProposal createProposal(CompletionProposal javaProposal) {
  String completion = String.valueOf(javaProposal.getCompletion());
  int kind = javaProposal.getKind();
  if (kind == CompletionProposal.TYPE_REF) {
    // Make sure it is fully qualified
    completion = JavaContentAssistUtilities.getFullyQualifiedTypeName(javaProposal);
  }

  if (forceFullyQualifiedFieldNames
      && (kind == CompletionProposal.FIELD_IMPORT || kind == CompletionProposal.FIELD_REF)) {
    char[] decSig = javaProposal.getDeclarationSignature();
    if (decSig != null && decSig.length > 2) {
      // declaration signatures for objects are like Ljava.lang.String;, so lop off first
      // and last chars
      completion = new String(decSig, 1, decSig.length - 2) + "."
          + new String(javaProposal.getCompletion());
      completion = completion.replace('$', '.');
    }
  }

  ICompletionProposal jdtCompletionProposal = JavaContentAssistUtilities.getJavaCompletionProposal(
      javaProposal, context, javaProject);
  return ReplacementCompletionProposal.fromExistingCompletionProposal(completion,
      replaceOffset, replaceLength, jdtCompletionProposal);
}
 
Example 4
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
StyledString createAnonymousTypeLabel(CompletionProposal proposal) {
	char[] declaringTypeSignature= proposal.getDeclarationSignature();
	declaringTypeSignature= Signature.getTypeErasure(declaringTypeSignature);

	StyledString buffer= new StyledString();
	buffer.append(Signature.getSignatureSimpleName(declaringTypeSignature));
	buffer.append('(');
	appendUnboundedParameterList(buffer, proposal);
	buffer.append(')');
	buffer.append("  "); //$NON-NLS-1$
	buffer.append(JavaTextMessages.ResultCollector_anonymous_type);

	if (proposal.getRequiredProposals() != null) {
		char[] signatureQualifier= Signature.getSignatureQualifier(declaringTypeSignature);
		if (signatureQualifier.length > 0) {
			buffer.append(JavaElementLabels.CONCAT_STRING, StyledString.QUALIFIER_STYLER);
			buffer.append(signatureQualifier, StyledString.QUALIFIER_STYLER);
		}
	}

	return Strings.markJavaElementLabelLTR(buffer);
}
 
Example 5
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Extracts the fully qualified name of the declaring type of a method
 * reference.
 *
 * @param methodProposal a proposed method
 * @return the qualified name of the declaring type
 */
private String extractDeclaringTypeFQN(CompletionProposal methodProposal) {
	char[] declaringTypeSignature= methodProposal.getDeclarationSignature();
	// special methods may not have a declaring type: methods defined on arrays etc.
	// TODO remove when bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84690 gets fixed
	if (declaringTypeSignature == null)
	{
		return OBJECT;
	}
	return SignatureUtil.stripSignatureToFQN(String.valueOf(declaringTypeSignature));
}
 
Example 6
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void createLabelWithTypeAndDeclaration(CompletionProposal proposal, CompletionItem item) {
	char[] name= proposal.getCompletion();
	if (!isThisPrefix(name)) {
		name= proposal.getName();
	}
	StringBuilder buf= new StringBuilder();

	buf.append(name);
	item.setInsertText(buf.toString());
	char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
	if (typeName.length > 0) {
		buf.append(VAR_TYPE_SEPARATOR);
		buf.append(typeName);
	}
	item.setLabel(buf.toString());

	char[] declaration= proposal.getDeclarationSignature();
	StringBuilder detailBuf = new StringBuilder();
	if (declaration != null) {
		setDeclarationSignature(item, String.valueOf(declaration));
		declaration= Signature.getSignatureSimpleName(declaration);
		if (declaration.length > 0) {
			if (proposal.getRequiredProposals() != null) {
				String declaringType= extractDeclaringTypeFQN(proposal);
				String qualifier= Signature.getQualifier(declaringType);
				if (qualifier.length() > 0) {
					detailBuf.append(qualifier);
					detailBuf.append('.');
				}
			}
			detailBuf.append(declaration);
		}
	}
	if (detailBuf.length() > 0) {
		detailBuf.append('.');
	}
	detailBuf.append(buf);
	item.setDetail(detailBuf.toString());
	setName(item,String.valueOf(name));
}
 
Example 7
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 8
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extracts the fully qualified name of the declaring type of a method
 * reference.
 *
 * @param methodProposal a proposed method
 * @return the qualified name of the declaring type
 */
private String extractDeclaringTypeFQN(CompletionProposal methodProposal) {
	char[] declaringTypeSignature= methodProposal.getDeclarationSignature();
	// special methods may not have a declaring type: methods defined on arrays etc.
	// TODO remove when bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84690 gets fixed
	if (declaringTypeSignature == null)
		return "java.lang.Object"; //$NON-NLS-1$
	return SignatureUtil.stripSignatureToFQN(String.valueOf(declaringTypeSignature));
}
 
Example 9
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
StyledString createLabelWithTypeAndDeclaration(CompletionProposal proposal) {
	char[] name= proposal.getCompletion();
	if (!isThisPrefix(name))
		name= proposal.getName();

	StyledString buf= new StyledString();
	buf.append(name);
	char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
	if (typeName.length > 0) {
		buf.append(VAR_TYPE_SEPARATOR);
		buf.append(typeName);
	}
	char[] declaration= proposal.getDeclarationSignature();
	if (declaration != null) {
		declaration= Signature.getSignatureSimpleName(declaration);
		if (declaration.length > 0) {
			buf.append(QUALIFIER_SEPARATOR, StyledString.QUALIFIER_STYLER);
			if (proposal.getRequiredProposals() != null) {
				String declaringType= extractDeclaringTypeFQN(proposal);
				String qualifier= Signature.getQualifier(declaringType);
				if (qualifier.length() > 0) {
					buf.append(qualifier, StyledString.QUALIFIER_STYLER);
					buf.append('.', StyledString.QUALIFIER_STYLER);
				}
			}
			buf.append(declaration, StyledString.QUALIFIER_STYLER);
		}
	}

	return Strings.markJavaElementLabelLTR(buf);
}
 
Example 10
Source File: CompletionProposalRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * copied from
 * org.eclipse.jdt.ui.text.java.CompletionProposalCollector.getDeclaringType(CompletionProposal)
 */
protected final char[] getDeclaringType(CompletionProposal proposal) {
	switch (proposal.getKind()) {
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.JAVADOC_METHOD_REF:
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.FIELD_REF:
		case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.JAVADOC_FIELD_REF:
		case CompletionProposal.JAVADOC_VALUE_REF:
			char[] declaration = proposal.getDeclarationSignature();
			// special methods may not have a declaring type: methods defined on arrays etc.
			// Currently known: class literals don't have a declaring type - use Object
			if (declaration == null) {
				return "java.lang.Object".toCharArray(); //$NON-NLS-1$
			}
			return Signature.toCharArray(declaration);
		case CompletionProposal.PACKAGE_REF:
		case CompletionProposal.MODULE_REF:
		case CompletionProposal.MODULE_DECLARATION:
			return proposal.getDeclarationSignature();
		case CompletionProposal.JAVADOC_TYPE_REF:
		case CompletionProposal.TYPE_REF:
			return Signature.toCharArray(proposal.getSignature());
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
		case CompletionProposal.KEYWORD:
		case CompletionProposal.LABEL_REF:
		case CompletionProposal.JAVADOC_BLOCK_TAG:
		case CompletionProposal.JAVADOC_INLINE_TAG:
		case CompletionProposal.JAVADOC_PARAM_REF:
			return null;
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 11
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 12
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the type signature of the declaring type of a
 * <code>CompletionProposal</code>, or <code>null</code> for proposals
 * that do not have a declaring type. The return value is <em>not</em>
 * <code>null</code> for proposals of the following kinds:
 * <ul>
 * <li>METHOD_DECLARATION</li>
 * <li>METHOD_NAME_REFERENCE</li>
 * <li>METHOD_REF</li>
 * <li>ANNOTATION_ATTRIBUTE_REF</li>
 * <li>POTENTIAL_METHOD_DECLARATION</li>
 * <li>ANONYMOUS_CLASS_DECLARATION</li>
 * <li>FIELD_REF</li>
 * <li>PACKAGE_REF (returns the package, but no type)</li>
 * <li>TYPE_REF</li>
 * </ul>
 *
 * @param proposal the completion proposal to get the declaring type for
 * @return the type signature of the declaring type, or <code>null</code> if there is none
 * @see Signature#toCharArray(char[])
 */
protected final char[] getDeclaringType(CompletionProposal proposal) {
	switch (proposal.getKind()) {
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.JAVADOC_METHOD_REF:
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.FIELD_REF:
		case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.JAVADOC_FIELD_REF:
		case CompletionProposal.JAVADOC_VALUE_REF:
			char[] declaration= proposal.getDeclarationSignature();
			// special methods may not have a declaring type: methods defined on arrays etc.
			// Currently known: class literals don't have a declaring type - use Object
			if (declaration == null)
				return "java.lang.Object".toCharArray(); //$NON-NLS-1$
			return Signature.toCharArray(declaration);
		case CompletionProposal.PACKAGE_REF:
			return proposal.getDeclarationSignature();
		case CompletionProposal.JAVADOC_TYPE_REF:
		case CompletionProposal.TYPE_REF:
			return Signature.toCharArray(proposal.getSignature());
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
		case CompletionProposal.KEYWORD:
		case CompletionProposal.LABEL_REF:
		case CompletionProposal.JAVADOC_BLOCK_TAG:
		case CompletionProposal.JAVADOC_INLINE_TAG:
		case CompletionProposal.JAVADOC_PARAM_REF:
			return null;
		default:
			Assert.isTrue(false);
			return null;
	}
}