Search in sources :

Example 16 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project flux by eclipse.

the class CompletionProposalReplacementProvider method computeTypeArgumentProposals.

private String[] computeTypeArgumentProposals(CompletionProposal proposal) {
    try {
        IType type = (IType) resolveJavaElement(compilationUnit.getJavaProject(), proposal);
        if (type == null)
            return new String[0];
        ITypeParameter[] parameters = type.getTypeParameters();
        if (parameters.length == 0)
            return new String[0];
        String[] arguments = new String[parameters.length];
        ITypeBinding expectedTypeBinding = getExpectedTypeForGenericParameters();
        if (expectedTypeBinding != null && expectedTypeBinding.isParameterizedType()) {
            // in this case, the type arguments we propose need to be compatible
            // with the corresponding type parameters to declared type
            IType expectedType = (IType) expectedTypeBinding.getJavaElement();
            IType[] path = TypeProposalUtils.computeInheritancePath(type, expectedType);
            if (path == null)
                // to instantiate -> do not add any type arguments
                return new String[0];
            int[] indices = new int[parameters.length];
            for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) {
                indices[paramIdx] = TypeProposalUtils.mapTypeParameterIndex(path, path.length - 1, paramIdx);
            }
            // for type arguments that are mapped through to the expected type's
            // parameters, take the arguments of the expected type
            ITypeBinding[] typeArguments = expectedTypeBinding.getTypeArguments();
            for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) {
                if (indices[paramIdx] != -1) {
                    // type argument is mapped through
                    ITypeBinding binding = typeArguments[indices[paramIdx]];
                    arguments[paramIdx] = computeTypeProposal(binding, parameters[paramIdx]);
                }
            }
        }
        // take the lower bound of the type parameter
        for (int i = 0; i < arguments.length; i++) {
            if (arguments[i] == null) {
                arguments[i] = computeTypeProposal(parameters[i]);
            }
        }
        return arguments;
    } catch (JavaModelException e) {
        return new String[0];
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IType(org.eclipse.jdt.core.IType)

Aggregations

ITypeParameter (org.eclipse.jdt.core.ITypeParameter)16 IType (org.eclipse.jdt.core.IType)14 IMethod (org.eclipse.jdt.core.IMethod)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 SourceRange (org.eclipse.jdt.core.SourceRange)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 RenameTypeParameterProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)2 Point (org.eclipse.swt.graphics.Point)2 HashSet (java.util.HashSet)1 IAnnotation (org.eclipse.jdt.core.IAnnotation)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IField (org.eclipse.jdt.core.IField)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1