Java Code Examples for org.eclipse.jdt.core.Signature#getTypeArguments()

The following examples show how to use org.eclipse.jdt.core.Signature#getTypeArguments() . 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: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * The type and method signatures received in
 * <code>CompletionProposals</code> of type <code>METHOD_REF</code>
 * contain concrete type bounds. When comparing parameters of the signature
 * with an <code>IMethod</code>, we have to make sure that we match the
 * case where the formal method declaration uses a type variable which in
 * the signature is already substituted with a concrete type (bound).
 * <p>
 * This method creates a map from type variable names to type signatures
 * based on the position they appear in the type declaration. The type
 * signatures are filtered through
 * {@link SignatureUtil#getLowerBound(char[])}.
 * </p>
 *
 * @param type the type to get the variables from
 * @return a map from type variables to concrete type signatures
 * @throws JavaModelException if accessing the java model fails
 */
private Map<String, char[]> computeTypeVariables(IType type) throws JavaModelException {
	Map<String, char[]> map= new HashMap<String, char[]>();
	char[] declarationSignature= fProposal.getDeclarationSignature();
	if (declarationSignature == null) // array methods don't contain a declaration signature
		return map;
	char[][] concreteParameters= Signature.getTypeArguments(declarationSignature);

	ITypeParameter[] typeParameters= type.getTypeParameters();
	for (int i= 0; i < typeParameters.length; i++) {
		String variable= typeParameters[i].getElementName();
		if (concreteParameters.length > i)
			// use lower bound since method equality is only parameter based
			map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i]));
		else
			// fProposal.getDeclarationSignature() is a raw type - use Object
			map.put(variable, "Ljava.lang.Object;".toCharArray()); //$NON-NLS-1$
	}

	return map;
}
 
Example 2
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void computeParameterizedTypeName(String signature, StringBuilder uriBuilder) {
	computeTypeName(signature, uriBuilder);
	String[] typeArguments = Signature.getTypeArguments(signature);
	if (typeArguments.length != 0) {
		uriBuilder.append('<');
		for (int i = 0; i < typeArguments.length; i++) {
			if (i != 0)
				uriBuilder.append(',');
			computeParameter(typeArguments[i], uriBuilder);
		}
		uriBuilder.append('>');
	}
}
 
Example 3
Source File: TypeProposalUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
static int findMatchingTypeArgumentIndex(String signature, String argument) {
	String[] typeArguments= Signature.getTypeArguments(signature);
	for (int i= 0; i < typeArguments.length; i++) {
		if (Signature.getSignatureSimpleName(typeArguments[i]).equals(argument)) {
			return i;
		}
	}
	return -1;
}
 
Example 4
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Appends the type parameter list to <code>buffer</code>.
 *
 * @param buffer the buffer to append to
 * @param typeProposal the type proposal
 * @return the modified <code>buffer</code>
 */
private StringBuilder appendTypeParameterList(StringBuilder buffer, CompletionProposal typeProposal) {
	// TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
	// gets fixed.
	char[] signature= SignatureUtil.fix83600(typeProposal.getSignature());
	char[][] typeParameters= Signature.getTypeArguments(signature);
	for (int i= 0; i < typeParameters.length; i++) {
		char[] param= typeParameters[i];
		typeParameters[i]= Signature.toCharArray(param);
	}
	return appendParameterSignature(buffer, typeParameters, null);
}
 
Example 5
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extracts the type variables from a signature
 *
 * @param signature
 *        the signature to extract the type variables from
 * @param variables
 *        the set of variables to fill in
 */
private static void extractTypeVariables(final String signature, final Set<String> variables) {
	Assert.isNotNull(signature);
	Assert.isNotNull(variables);

	final String[] arguments= Signature.getTypeArguments(signature);
	if (arguments.length == 0) {
		variables.add(Signature.toString(signature));
	} else {
		for (int index= 0; index < arguments.length; index++)
			variables.add(Signature.toString(arguments[index]));
	}
}
 
Example 6
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the type variable signatures of the specified parameterized type signature, or an empty array if none.
 *
 * @param signature
 *        the signature to get its type variable signatures from. The signature must be a parameterized type signature.
 * @return a possibly empty array of type variable signatures
 * @see Signature#getTypeArguments(String)
 */
private static String[] getVariableSignatures(final String signature) {
	Assert.isNotNull(signature);

	String[] result= null;
	try {
		result= Signature.getTypeArguments(signature);
	} catch (IllegalArgumentException exception) {
		result= new String[0];
	}
	return result;
}
 
Example 7
Source File: SignatureUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the lower bound of a type signature. Returns the null type
 * signature if <code>signature</code> is a wildcard or upper bound (<code>? extends T</code>);
 * returns the signature of the type <code>T</code> of a lower bound (<code>? super T</code>)
 * or <code>signature</code> itself if it is not a bound signature.
 *
 * @param signature the signature
 * @return the lower bound signature of <code>signature</code>
 */
public static char[] getLowerBound(char[] signature) {
	if (signature.length < 1)
		return signature;

	if (signature.length == 1 && signature[0] == Signature.C_STAR)
		return signature;

	int superIndex= indexOf(signature, Signature.C_EXTENDS);
	if (superIndex == 0)
		return NULL_TYPE_SIGNATURE_ARRAY;

	if (superIndex != -1) {
		char afterSuper= signature[superIndex + 1];
		if (afterSuper == Signature.C_STAR || afterSuper == Signature.C_EXTENDS)
			// impossible captured type
			return NULL_TYPE_SIGNATURE_ARRAY;
	}

	char[][] typeArguments= Signature.getTypeArguments(signature);
	for (int i= 0; i < typeArguments.length; i++)
		if (Arrays.equals(typeArguments[i], NULL_TYPE_SIGNATURE_ARRAY))
			return NULL_TYPE_SIGNATURE_ARRAY;

	if (signature[0] == Signature.C_SUPER) {
		char[] type= new char[signature.length - 1];
		System.arraycopy(signature, 1, type, 0, signature.length - 1);
		return type;
	}

	return signature;
}
 
Example 8
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Appends the type parameter list to <code>buffer</code>.
 *
 * @param buffer the buffer to append to
 * @param typeProposal the type proposal
 * @return the modified <code>buffer</code>
 * @since 3.2
 */
private StyledString appendTypeParameterList(StyledString buffer, CompletionProposal typeProposal) {
	// TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
	// gets fixed.
	char[] signature= SignatureUtil.fix83600(typeProposal.getSignature());
	char[][] typeParameters= Signature.getTypeArguments(signature);
	for (int i= 0; i < typeParameters.length; i++) {
		char[] param= typeParameters[i];
		typeParameters[i]= Signature.toCharArray(param);
	}
	return appendParameterSignature(buffer, typeParameters, null);
}
 
Example 9
Source File: LazyJavaTypeCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected IContextInformation computeContextInformation() {
	char[] signature= fProposal.getSignature();
	char[][] typeParameters= Signature.getTypeArguments(signature);
	if (typeParameters.length == 0)
		return super.computeContextInformation();

	ProposalContextInformation contextInformation= new ProposalContextInformation(fProposal);
	if (fContextInformationPosition != 0 && fProposal.getCompletion().length == 0)
		contextInformation.setContextInformationPosition(fContextInformationPosition);
	return contextInformation;

}
 
Example 10
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
	case Signature.BASE_TYPE_SIGNATURE:
		fBuilder.append(Signature.toString(typeSig));
		break;
	case Signature.ARRAY_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
		for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
			fBuilder.append('[').append(']');
		}
		break;
	case Signature.CLASS_TYPE_SIGNATURE:
		String baseType= getSimpleTypeName(enclosingElement, typeSig);
		fBuilder.append(baseType);

		String[] typeArguments= Signature.getTypeArguments(typeSig);
		appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
		break;
	case Signature.TYPE_VARIABLE_SIGNATURE:
		fBuilder.append(getSimpleTypeName(enclosingElement, typeSig));
		break;
	case Signature.WILDCARD_TYPE_SIGNATURE:
		char ch= typeSig.charAt(0);
		if (ch == Signature.C_STAR) { //workaround for bug 85713
			fBuilder.append('?');
		} else {
			if (ch == Signature.C_EXTENDS) {
				fBuilder.append("? extends "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			} else if (ch == Signature.C_SUPER) {
				fBuilder.append("? super "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			}
		}
		break;
	case Signature.CAPTURE_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
		break;
	case Signature.INTERSECTION_TYPE_SIGNATURE:
		String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
		appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
		break;
	default:
		// unknown
	}
}
 
Example 11
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			fBuffer.append(Signature.toString(typeSig));
			break;
		case Signature.ARRAY_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
			for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
				fBuffer.append('[').append(']');
			}
			break;
		case Signature.CLASS_TYPE_SIGNATURE:
			String baseType= getSimpleTypeName(enclosingElement, typeSig);
			fBuffer.append(baseType);

			String[] typeArguments= Signature.getTypeArguments(typeSig);
			appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
			break;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			fBuffer.append(getSimpleTypeName(enclosingElement, typeSig));
			break;
		case Signature.WILDCARD_TYPE_SIGNATURE:
			char ch= typeSig.charAt(0);
			if (ch == Signature.C_STAR) { //workaround for bug 85713
				fBuffer.append('?');
			} else {
				if (ch == Signature.C_EXTENDS) {
					fBuffer.append("? extends "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				} else if (ch == Signature.C_SUPER) {
					fBuffer.append("? super "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				}
			}
			break;
		case Signature.CAPTURE_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			break;
		case Signature.INTERSECTION_TYPE_SIGNATURE:
			String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
			appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
			break;
		default:
			// unknown
	}
}
 
Example 12
Source File: ImportRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a new import to the rewriter's record and returns a {@link Type} node that can be used
 * in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard.
 * If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
 * arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
 * of wildcards are ignored.
 * 	<p>
	 * No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
 * </p>
 * <p>
 * The content of the compilation unit itself is actually not modified
 * in any way by this method; rather, the rewriter just records that a new import has been added.
 * </p>
 * @param typeSig the signature of the type to be added.
 * @param ast the AST to create the returned type for.
 * @param context an optional context that knows about types visible in the current scope or <code>null</code>
 * to use the default context only using the available imports.
 * @return a type node for the given type signature. Type names are simple names if an import could be used,
 * or else qualified names if an import conflict prevented an import.
 */
public Type addImportFromSignature(String typeSig, AST ast, ImportRewriteContext context) {
	if (typeSig == null || typeSig.length() == 0) {
		throw new IllegalArgumentException("Invalid type signature: empty or null"); //$NON-NLS-1$
	}
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
		case Signature.ARRAY_TYPE_SIGNATURE:
			Type elementType= addImportFromSignature(Signature.getElementType(typeSig), ast, context);
			return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
		case Signature.CLASS_TYPE_SIGNATURE:
			String erasureSig= Signature.getTypeErasure(typeSig);

			String erasureName= Signature.toString(erasureSig);
			if (erasureSig.charAt(0) == Signature.C_RESOLVED) {
				erasureName= internalAddImport(erasureName, context);
			}
			Type baseType= ast.newSimpleType(ast.newName(erasureName));
			String[] typeArguments= Signature.getTypeArguments(typeSig);
			if (typeArguments.length > 0) {
				ParameterizedType type= ast.newParameterizedType(baseType);
				List argNodes= type.typeArguments();
				for (int i= 0; i < typeArguments.length; i++) {
					String curr= typeArguments[i];
					if (containsNestedCapture(curr)) { // see bug 103044
						argNodes.add(ast.newWildcardType());
					} else {
						argNodes.add(addImportFromSignature(curr, ast, context));
					}
				}
				return type;
			}
			return baseType;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
		case Signature.WILDCARD_TYPE_SIGNATURE:
			WildcardType wildcardType= ast.newWildcardType();
			char ch= typeSig.charAt(0);
			if (ch != Signature.C_STAR) {
				Type bound= addImportFromSignature(typeSig.substring(1), ast, context);
				wildcardType.setBound(bound, ch == Signature.C_EXTENDS);
			}
			return wildcardType;
		case Signature.CAPTURE_TYPE_SIGNATURE:
			return addImportFromSignature(typeSig.substring(1), ast, context);
		default:
			throw new IllegalArgumentException("Unknown type signature kind: " + typeSig); //$NON-NLS-1$
	}
}
 
Example 13
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Type createType(String typeSig, AST ast) {
	int sigKind = Signature.getTypeSignatureKind(typeSig);
       switch (sigKind) {
           case Signature.BASE_TYPE_SIGNATURE:
               return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
           case Signature.ARRAY_TYPE_SIGNATURE:
               Type elementType = createType(Signature.getElementType(typeSig), ast);
               return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
           case Signature.CLASS_TYPE_SIGNATURE:
               String erasureSig = Signature.getTypeErasure(typeSig);

               String erasureName = Signature.toString(erasureSig);
               if (erasureSig.charAt(0) == Signature.C_RESOLVED) {
                   erasureName = addImport(erasureName);
               }
               
               Type baseType= ast.newSimpleType(ast.newName(erasureName));
               String[] typeArguments = Signature.getTypeArguments(typeSig);
               if (typeArguments.length > 0) {
                   ParameterizedType type = ast.newParameterizedType(baseType);
                   List argNodes = type.typeArguments();
                   for (int i = 0; i < typeArguments.length; i++) {
                       String curr = typeArguments[i];
                       if (containsNestedCapture(curr)) {
                           argNodes.add(ast.newWildcardType());
                       } else {
                           argNodes.add(createType(curr, ast));
                       }
                   }
                   return type;
               }
               return baseType;
           case Signature.TYPE_VARIABLE_SIGNATURE:
               return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
           case Signature.WILDCARD_TYPE_SIGNATURE:
               WildcardType wildcardType= ast.newWildcardType();
               char ch = typeSig.charAt(0);
               if (ch != Signature.C_STAR) {
                   Type bound= createType(typeSig.substring(1), ast);
                   wildcardType.setBound(bound, ch == Signature.C_EXTENDS);
               }
               return wildcardType;
           case Signature.CAPTURE_TYPE_SIGNATURE:
               return createType(typeSig.substring(1), ast);
       }
       
       return ast.newSimpleType(ast.newName("java.lang.Object"));
}
 
Example 14
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Finds and returns the type argument with index <code>index</code>
 * in the given type super type signature. If <code>signature</code>
 * is a generic signature, the type parameter at <code>index</code> is
 * extracted. If the type parameter is an upper bound (<code>? super SomeType</code>),
 * the type signature of <code>java.lang.Object</code> is returned.
 * <p>
 * Also, if <code>signature</code> has no type parameters (i.e. is a
 * reference to the raw type), the type signature of
 * <code>java.lang.Object</code> is returned.
 * </p>
 *
 * @param signature the super type signature from a type's
 *        <code>extends</code> or <code>implements</code> clause
 * @param index the index of the type parameter to extract from
 *        <code>signature</code>
 * @param context the type context inside which unqualified types should
 *        be resolved
 * @return the type argument signature of the type parameter at
 *         <code>index</code> in <code>signature</code>
 * @throws IndexOutOfBoundsException if the index is not valid
 */
private String findMatchingTypeArgument(String signature, int index, IType context) throws IndexOutOfBoundsException {
	String[] typeArguments= Signature.getTypeArguments(signature);
	if (typeArguments.length > 0 && typeArguments.length <= index)
		throw new IndexOutOfBoundsException();
	if (typeArguments.length == 0) {
		// raw binding - bound to Object
		return OBJECT_SIGNATURE;
	} else {
		String bound= SignatureUtil.getUpperBound(typeArguments[index]);
		return SignatureUtil.qualifySignature(bound, context);
	}
}
 
Example 15
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Finds and returns the index of the type argument named
 * <code>argument</code> in the given super type signature.
 * <p>
 * If <code>signature</code> does not contain a corresponding type
 * argument, or if <code>signature</code> has no type parameters (i.e. is
 * a reference to a non-parameterized type or a raw type), -1 is returned.
 * </p>
 *
 * @param signature the super type signature from a type's
 *        <code>extends</code> or <code>implements</code> clause
 * @param argument the name of the type argument to find
 * @return the index of the given type argument, or -1 if there is none
 */
private int findMatchingTypeArgumentIndex(String signature, String argument) {
	String[] typeArguments= Signature.getTypeArguments(signature);
	for (int i= 0; i < typeArguments.length; i++) {
		if (Signature.getSignatureSimpleName(typeArguments[i]).equals(argument))
			return i;
	}
	return -1;
}