Search in sources :

Example 11 with ITypeParameter

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

the class MethodOverrideTester method getMethodSubstitions.

/*
	 * Returns the substitutions for a method's type parameters
	 */
private Substitutions getMethodSubstitions(IMethod method) throws JavaModelException {
    if (fMethodSubstitutions == null) {
        fMethodSubstitutions = new LRUMap<IMethod, Substitutions>(3);
    }
    Substitutions s = fMethodSubstitutions.get(method);
    if (s == null) {
        ITypeParameter[] typeParameters = method.getTypeParameters();
        if (typeParameters.length == 0) {
            s = Substitutions.EMPTY_SUBST;
        } else {
            IType instantiatedType = method.getDeclaringType();
            s = new Substitutions();
            for (int i = 0; i < typeParameters.length; i++) {
                ITypeParameter curr = typeParameters[i];
                s.addSubstitution(curr.getElementName(), '+' + String.valueOf(i), getTypeParameterErasure(curr, instantiatedType));
            }
        }
        fMethodSubstitutions.put(method, s);
    }
    return s;
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 12 with ITypeParameter

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

the class SourceMapper method enterType.

/**
     * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor
     */
public void enterType(TypeInfo typeInfo) {
    this.typeDepth++;
    if (this.typeDepth == this.types.length) {
        // need to grow
        System.arraycopy(this.types, 0, this.types = new IType[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.typeNameRanges, 0, this.typeNameRanges = new SourceRange[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.typeDeclarationStarts, 0, this.typeDeclarationStarts = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberName, 0, this.memberName = new String[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberDeclarationStart, 0, this.memberDeclarationStart = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberNameRange, 0, this.memberNameRange = new SourceRange[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.methodParameterTypes, 0, this.methodParameterTypes = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.methodParameterNames, 0, this.methodParameterNames = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.typeModifiers, 0, this.typeModifiers = new int[this.typeDepth * 2], 0, this.typeDepth);
    }
    if (typeInfo.name.length == 0) {
        this.anonymousCounter++;
        if (this.anonymousCounter == this.anonymousClassName) {
            this.types[this.typeDepth] = getType(this.binaryType.getElementName());
        } else {
            this.types[this.typeDepth] = getType(new String(typeInfo.name));
        }
    } else {
        this.types[this.typeDepth] = getType(new String(typeInfo.name));
    }
    this.typeNameRanges[this.typeDepth] = new SourceRange(typeInfo.nameSourceStart, typeInfo.nameSourceEnd - typeInfo.nameSourceStart + 1);
    this.typeDeclarationStarts[this.typeDepth] = typeInfo.declarationStart;
    IType currentType = this.types[this.typeDepth];
    // type parameters
    if (typeInfo.typeParameters != null) {
        for (int i = 0, length = typeInfo.typeParameters.length; i < length; i++) {
            TypeParameterInfo typeParameterInfo = typeInfo.typeParameters[i];
            ITypeParameter typeParameter = currentType.getTypeParameter(new String(typeParameterInfo.name));
            setSourceRange(typeParameter, new SourceRange(typeParameterInfo.declarationStart, typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1), new SourceRange(typeParameterInfo.nameSourceStart, typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
        }
    }
    // type modifiers
    this.typeModifiers[this.typeDepth] = typeInfo.modifiers;
    // categories
    addCategories(currentType, typeInfo.categories);
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IType(org.eclipse.jdt.core.IType)

Example 13 with ITypeParameter

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

the class SourceMapper method enterAbstractMethod.

private void enterAbstractMethod(MethodInfo methodInfo) {
    if (this.typeDepth >= 0) {
        this.memberName[this.typeDepth] = new String(methodInfo.name);
        this.memberNameRange[this.typeDepth] = new SourceRange(methodInfo.nameSourceStart, methodInfo.nameSourceEnd - methodInfo.nameSourceStart + 1);
        this.memberDeclarationStart[this.typeDepth] = methodInfo.declarationStart;
        IType currentType = this.types[this.typeDepth];
        int currenTypeModifiers = this.typeModifiers[this.typeDepth];
        char[][] parameterTypes = methodInfo.parameterTypes;
        if (methodInfo.isConstructor && currentType.getDeclaringType() != null && !Flags.isStatic(currenTypeModifiers)) {
            IType declaringType = currentType.getDeclaringType();
            String declaringTypeName = declaringType.getElementName();
            if (declaringTypeName.length() == 0) {
                IClassFile classFile = declaringType.getClassFile();
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                declaringTypeName = classFile.getElementName();
                declaringTypeName = declaringTypeName.substring(0, declaringTypeName.indexOf('.'));
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            } else {
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            }
        } else {
            this.methodParameterTypes[this.typeDepth] = parameterTypes;
        }
        this.methodParameterNames[this.typeDepth] = methodInfo.parameterNames;
        IMethod method = currentType.getMethod(this.memberName[this.typeDepth], convertTypeNamesToSigs(this.methodParameterTypes[this.typeDepth]));
        // type parameters
        if (methodInfo.typeParameters != null) {
            for (int i = 0, length = methodInfo.typeParameters.length; i < length; i++) {
                TypeParameterInfo typeParameterInfo = methodInfo.typeParameters[i];
                ITypeParameter typeParameter = method.getTypeParameter(new String(typeParameterInfo.name));
                setSourceRange(typeParameter, new SourceRange(typeParameterInfo.declarationStart, typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1), new SourceRange(typeParameterInfo.nameSourceStart, typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
            }
        }
        // parameters infos
        if (methodInfo.parameterInfos != null) {
            for (int i = 0, length = methodInfo.parameterInfos.length; i < length; i++) {
                ParameterInfo parameterInfo = methodInfo.parameterInfos[i];
                LocalVariableElementKey key = new LocalVariableElementKey(method, new String(parameterInfo.name));
                SourceRange[] allRanges = new SourceRange[] { new SourceRange(parameterInfo.declarationStart, parameterInfo.declarationEnd - parameterInfo.declarationStart + 1), new SourceRange(parameterInfo.nameSourceStart, parameterInfo.nameSourceEnd - parameterInfo.nameSourceStart + 1) };
                this.parametersRanges.put(key, allRanges);
                if (parameterInfo.modifiers != 0) {
                    if (this.finalParameters == null) {
                        this.finalParameters = new HashSet();
                    }
                    this.finalParameters.add(key);
                }
            }
        }
        // categories
        addCategories(method, methodInfo.categories);
    }
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) HashSet(java.util.HashSet)

Example 14 with ITypeParameter

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

the class LazyGenericTypeProposal method computeTypeArgumentProposals.

/**
     * Computes the type argument proposals for this type proposals. If there is
     * an expected type binding that is a super type of the proposed type, the
     * wildcard type arguments of the proposed type that can be mapped through
     * to type the arguments of the expected type binding are bound accordingly.
     * <p>
     * For type arguments that cannot be mapped to arguments in the expected
     * type, or if there is no expected type, the upper bound of the type
     * argument is proposed.
     * </p>
     * <p>
     * The argument proposals have their <code>isAmbiguos</code> flag set to
     * <code>false</code> if the argument can be mapped to a non-wildcard type
     * argument in the expected type, otherwise the proposal is ambiguous.
     * </p>
     *
     * @return the type argument proposals for the proposed type
     * @throws org.eclipse.jdt.core.JavaModelException if accessing the java model fails
     */
private TypeArgumentProposal[] computeTypeArgumentProposals() throws JavaModelException {
    if (fTypeArgumentProposals == null) {
        IType type = (IType) getJavaElement();
        if (type == null)
            return new TypeArgumentProposal[0];
        ITypeParameter[] parameters = type.getTypeParameters();
        if (parameters.length == 0)
            return new TypeArgumentProposal[0];
        TypeArgumentProposal[] arguments = new TypeArgumentProposal[parameters.length];
        ITypeBinding expectedTypeBinding = getExpectedType();
        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 = computeInheritancePath(type, expectedType);
            if (path == null)
                // to instantiate -> do not add any type arguments
                return new TypeArgumentProposal[0];
            int[] indices = new int[parameters.length];
            for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) {
                indices[paramIdx] = 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]);
            }
        }
        fTypeArgumentProposals = arguments;
    }
    return fTypeArgumentProposals;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) Point(org.eclipse.swt.graphics.Point) IType(org.eclipse.jdt.core.IType)

Example 15 with ITypeParameter

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

the class StubCreator method appendTypeParameters.

protected void appendTypeParameters(final ITypeParameter[] parameters) throws JavaModelException {
    final int length = parameters.length;
    if (length > 0)
        //$NON-NLS-1$
        fBuffer.append("<");
    for (int index = 0; index < length; index++) {
        if (index > 0)
            //$NON-NLS-1$
            fBuffer.append(",");
        final ITypeParameter parameter = parameters[index];
        fBuffer.append(parameter.getElementName());
        final String[] bounds = parameter.getBounds();
        final int size = bounds.length;
        if (size > 0)
            //$NON-NLS-1$
            fBuffer.append(" extends ");
        for (int offset = 0; offset < size; offset++) {
            if (offset > 0)
                //$NON-NLS-1$
                fBuffer.append(" & ");
            fBuffer.append(bounds[offset]);
        }
    }
    if (length > 0)
        //$NON-NLS-1$
        fBuffer.append(">");
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter)

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