Search in sources :

Example 16 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method normalizeForDeclarationUse.

/**
     * Normalizes the binding so that it can be used as a type inside a declaration (e.g. variable
     * declaration, method return type, parameter type, ...).
     * For null bindings, java.lang.Object is returned.
     * For void bindings, <code>null</code> is returned.
     *
     * @param binding binding to normalize
     * @param ast current AST
     * @return the normalized type to be used in declarations, or <code>null</code>
     */
public static ITypeBinding normalizeForDeclarationUse(ITypeBinding binding, AST ast) {
    if (binding.isNullType())
        //$NON-NLS-1$
        return ast.resolveWellKnownType("java.lang.Object");
    if (binding.isPrimitive())
        return binding;
    binding = normalizeTypeBinding(binding);
    if (binding == null || !binding.isWildcardType())
        return binding;
    ITypeBinding bound = binding.getBound();
    if (bound == null || !binding.isUpperbound()) {
        ITypeBinding[] typeBounds = binding.getTypeBounds();
        if (typeBounds.length > 0) {
            return typeBounds[0];
        } else {
            return binding.getErasure();
        }
    } else {
        return bound;
    }
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 17 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method findOverriddenMethodInHierarchy.

/**
     * Finds a method in the hierarchy of <code>type</code> that is overridden by </code>binding</code>.
     * Returns <code>null</code> if no such method exists. If the method is defined in more than one super type only the first match is
     * returned. First the super class is examined and then the implemented interfaces.
     * @param type The type to search the method in
     * @param binding The method that overrides
     * @return the method binding overridden the method
     */
public static IMethodBinding findOverriddenMethodInHierarchy(ITypeBinding type, IMethodBinding binding) {
    IMethodBinding method = findOverriddenMethodInType(type, binding);
    if (method != null)
        return method;
    ITypeBinding superClass = type.getSuperclass();
    if (superClass != null) {
        method = findOverriddenMethodInHierarchy(superClass, binding);
        if (method != null)
            return method;
    }
    ITypeBinding[] interfaces = type.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        method = findOverriddenMethodInHierarchy(interfaces[i], binding);
        if (method != null)
            return method;
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 18 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method getTopLevelType.

public static ITypeBinding getTopLevelType(ITypeBinding type) {
    ITypeBinding parent = type.getDeclaringClass();
    while (parent != null) {
        type = parent;
        parent = type.getDeclaringClass();
    }
    return type;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 19 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method isSubsignature.

/**
     * @param overriding overriding method (m1)
     * @param overridden overridden method (m2)
     * @return <code>true</code> iff the method <code>m1</code> is a subsignature of the method <code>m2</code>.
     * 		This is one of the requirements for m1 to override m2.
     * 		Accessibility and return types are not taken into account.
     * 		Note that subsignature is <em>not</em> symmetric!
     */
public static boolean isSubsignature(IMethodBinding overriding, IMethodBinding overridden) {
    //TODO: use IMethodBinding#isSubsignature(..) once it is tested and fixed (only erasure of m1's parameter types, considering type variable counts, doing type variable substitution
    if (!overriding.getName().equals(overridden.getName()))
        return false;
    ITypeBinding[] m1Params = overriding.getParameterTypes();
    ITypeBinding[] m2Params = overridden.getParameterTypes();
    if (m1Params.length != m2Params.length)
        return false;
    ITypeBinding[] m1TypeParams = overriding.getTypeParameters();
    ITypeBinding[] m2TypeParams = overridden.getTypeParameters();
    if (m1TypeParams.length != m2TypeParams.length && //non-generic m1 can override a generic m2
    m1TypeParams.length != 0)
        return false;
    //m1TypeParameters.length == (m2TypeParameters.length || 0)
    if (m2TypeParams.length != 0) {
        //Compare type parameter bounds:
        for (int i = 0; i < m1TypeParams.length; i++) {
            // loop over m1TypeParams, which is either empty, or equally long as m2TypeParams
            Set<ITypeBinding> m1Bounds = getTypeBoundsForSubsignature(m1TypeParams[i]);
            Set<ITypeBinding> m2Bounds = getTypeBoundsForSubsignature(m2TypeParams[i]);
            if (!m1Bounds.equals(m2Bounds))
                return false;
        }
        //Compare parameter types:
        if (equals(m2Params, m1Params))
            return true;
        for (int i = 0; i < m1Params.length; i++) {
            ITypeBinding m1Param = m1Params[i];
            ITypeBinding m2Param = m2Params[i];
            if (containsTypeVariables(m1Param) || m1Param.isRawType())
                // try to achieve effect of "rename type variables"
                m1Param = m1Param.getErasure();
            if (!(equals(m1Param, m2Param) || equals(m1Param, m2Param.getErasure())))
                return false;
        }
        return true;
    } else {
        // m1TypeParams.length == m2TypeParams.length == 0
        if (equals(m1Params, m2Params))
            return true;
        for (int i = 0; i < m1Params.length; i++) {
            ITypeBinding m1Param = m1Params[i];
            ITypeBinding m2Param = m2Params[i];
            if (m1Param.isRawType())
                m1Param = m1Param.getTypeDeclaration();
            if (!(equals(m1Param, m2Param) || equals(m1Param, m2Param.getErasure())))
                return false;
        }
        return true;
    }
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 20 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method areSubTypeCompatible.

private static boolean areSubTypeCompatible(IMethodBinding overridden, IMethodBinding overridable) {
    if (overridden.getParameterTypes().length != overridable.getParameterTypes().length)
        return false;
    ITypeBinding overriddenReturn = overridden.getReturnType();
    ITypeBinding overridableReturn = overridable.getReturnType();
    if (overriddenReturn == null || overridableReturn == null)
        return false;
    if (!overriddenReturn.getErasure().isSubTypeCompatible(overridableReturn.getErasure()))
        return false;
    ITypeBinding[] overriddenTypes = overridden.getParameterTypes();
    ITypeBinding[] overridableTypes = overridable.getParameterTypes();
    Assert.isTrue(overriddenTypes.length == overridableTypes.length);
    for (int index = 0; index < overriddenTypes.length; index++) {
        final ITypeBinding overridableErasure = overridableTypes[index].getErasure();
        final ITypeBinding overriddenErasure = overriddenTypes[index].getErasure();
        if (!overridableErasure.isSubTypeCompatible(overriddenErasure) || !overridableErasure.getKey().equals(overriddenErasure.getKey()))
            return false;
    }
    ITypeBinding[] overriddenExceptions = overridden.getExceptionTypes();
    ITypeBinding[] overridableExceptions = overridable.getExceptionTypes();
    boolean checked = false;
    for (int index = 0; index < overriddenExceptions.length; index++) {
        checked = false;
        for (int offset = 0; offset < overridableExceptions.length; offset++) {
            if (overriddenExceptions[index].isSubTypeCompatible(overridableExceptions[offset]))
                checked = true;
        }
        if (!checked)
            return false;
    }
    return true;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Aggregations

ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)359 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)73 Expression (org.eclipse.jdt.core.dom.Expression)63 SimpleName (org.eclipse.jdt.core.dom.SimpleName)51 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Type (org.eclipse.jdt.core.dom.Type)49 ArrayList (java.util.ArrayList)48 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)48 AST (org.eclipse.jdt.core.dom.AST)43 IBinding (org.eclipse.jdt.core.dom.IBinding)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)40 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)34 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)34 CastExpression (org.eclipse.jdt.core.dom.CastExpression)32 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)30 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 Name (org.eclipse.jdt.core.dom.Name)28 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)28 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)26