Search in sources :

Example 46 with IMethodBinding

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(ClassInstanceCreation node) {
    Expression receiver = node.getExpression();
    Type createdType = node.getType();
    ConstraintVariable2 typeCv;
    if (node.getAnonymousClassDeclaration() == null) {
        typeCv = getConstraintVariable(createdType);
    } else {
        typeCv = fTCModel.makeImmutableTypeVariable(createdType.resolveBinding(), null);
        setConstraintVariable(createdType, typeCv);
    }
    setConstraintVariable(node, typeCv);
    IMethodBinding methodBinding = node.resolveConstructorBinding();
    Map<String, IndependentTypeVariable2> methodTypeVariables = createMethodTypeArguments(methodBinding);
    List<Expression> arguments = node.arguments();
    doVisitMethodInvocationArguments(methodBinding, arguments, receiver, methodTypeVariables, createdType);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) GenericType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType) ParameterizedType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ParameterizedType) WildcardType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.WildcardType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 47 with IMethodBinding

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    IMethodBinding methodBinding = node.resolveBinding();
    if (methodBinding == null)
        //TODO: emit error?
        return;
    int parameterCount = node.parameters().size();
    ConstraintVariable2[] parameterTypeCvs = new ConstraintVariable2[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) node.parameters().get(i);
        //parameterTypeVariable currently not used, but need to register in order to store source range
        ConstraintVariable2 parameterTypeCv = fTCModel.makeDeclaredParameterTypeVariable(methodBinding, i, fCU);
        parameterTypeCvs[i] = parameterTypeCv;
        if (parameterTypeCv == null)
            continue;
        //creating equals constraint between parameterTypeVariable's elements and the Type's elements
        ConstraintVariable2 typeCv = getConstraintVariable(paramDecl.getType());
        fTCModel.createElementEqualsConstraints(parameterTypeCv, typeCv);
        //TODO: should avoid having a VariableVariable as well as a ParameterVariable for a parameter
        ConstraintVariable2 nameCv = getConstraintVariable(paramDecl.getName());
        fTCModel.createElementEqualsConstraints(parameterTypeCv, nameCv);
    }
    ConstraintVariable2 returnTypeCv = null;
    if (!methodBinding.isConstructor()) {
        //TODO: should only create return type variable if type is generic?
        ConstraintVariable2 returnTypeBindingCv = fTCModel.makeDeclaredReturnTypeVariable(methodBinding, fCU);
        if (returnTypeBindingCv != null) {
            returnTypeCv = getConstraintVariable(node.getReturnType2());
            fTCModel.createElementEqualsConstraints(returnTypeBindingCv, returnTypeCv);
        }
    }
    if (MethodChecks.isVirtual(methodBinding)) {
        //TODO: RippleMethod constraints for corner cases: see testCuRippleMethods3, bug 41989
        addConstraintsForOverriding(methodBinding, returnTypeCv, parameterTypeCvs);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 48 with IMethodBinding

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(ReturnStatement node) {
    Expression expression = node.getExpression();
    if (expression == null)
        return;
    ConstraintVariable2 expressionCv = getConstraintVariable(expression);
    if (expressionCv == null)
        return;
    MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.getParent(node, ASTNode.METHOD_DECLARATION);
    if (methodDeclaration == null)
        return;
    IMethodBinding methodBinding = methodDeclaration.resolveBinding();
    if (methodBinding == null)
        return;
    ReturnTypeVariable2 returnTypeCv = fTCModel.makeReturnTypeVariable(methodBinding);
    if (returnTypeCv == null)
        return;
    fTCModel.createElementEqualsConstraints(returnTypeCv, expressionCv);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ReturnTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ReturnTypeVariable2) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 49 with IMethodBinding

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

the class InferTypeArgumentsConstraintCreator method addConstraintsForOverriding.

private void addConstraintsForOverriding(IMethodBinding methodBinding, ConstraintVariable2 returnTypeCv, ConstraintVariable2[] parameterTypeCvs) {
    boolean hasParameterElementCvs = false;
    for (int i = 0; i < parameterTypeCvs.length; i++) if (parameterTypeCvs[i] != null)
        hasParameterElementCvs = true;
    if (returnTypeCv == null && !hasParameterElementCvs)
        return;
    ITypeBinding[] allSuperTypes = Bindings.getAllSuperTypes(methodBinding.getDeclaringClass());
    for (int i = 0; i < allSuperTypes.length; i++) {
        ITypeBinding superType = allSuperTypes[i];
        IMethodBinding superMethod = Bindings.findOverriddenMethodInType(superType, methodBinding);
        if (superMethod == null)
            continue;
        for (int p = 0; p < parameterTypeCvs.length; p++) {
            if (parameterTypeCvs[p] == null)
                continue;
            ParameterTypeVariable2 parameterTypeCv = fTCModel.makeParameterTypeVariable(superMethod, p);
            fTCModel.createElementEqualsConstraints(parameterTypeCv, parameterTypeCvs[p]);
        }
        if (returnTypeCv != null) {
            ReturnTypeVariable2 superMethodReturnTypeCv = fTCModel.makeReturnTypeVariable(superMethod);
            fTCModel.createElementEqualsConstraints(superMethodReturnTypeCv, returnTypeCv);
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParameterTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterTypeVariable2) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ReturnTypeVariable2)

Example 50 with IMethodBinding

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

the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.

public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int relevance) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof MethodDeclaration) {
        selectedNode = ((MethodDeclaration) selectedNode).getName();
    }
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding != null) {
        String methodName = BasicElementLabels.getJavaElementName(binding.getName());
        String label = null;
        int problemId = problem.getProblemId();
        int excludedModifiers = 0;
        int includedModifiers = 0;
        switch(problemId) {
            case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
            case IProblem.UnexpectedStaticModifierForMethod:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodName);
                break;
            case IProblem.UnexpectedStaticModifierForField:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changefieldmodifiertononstatic_description, methodName);
                break;
            case IProblem.IllegalModifierCombinationFinalVolatileForField:
                excludedModifiers = Modifier.VOLATILE;
                label = CorrectionMessages.ModifierCorrectionSubProcessor_removevolatile_description;
                break;
            case IProblem.IllegalModifierForInterfaceMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
                break;
            case IProblem.IllegalModifierForInterfaceMethod18:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP | Modifier.DEFAULT | Modifier.STATIC);
                break;
            case IProblem.IllegalModifierForInterface:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForInterfaceField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
                break;
            case IProblem.IllegalModifierForMemberInterface:
            case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForMemberClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForLocalClass:
                excludedModifiers = ~(Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForArgument:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT);
                break;
            case IProblem.IllegalModifierForMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.STRICTFP | Modifier.SYNCHRONIZED);
                break;
            case IProblem.IllegalModifierForConstructor:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
                break;
            case IProblem.IllegalModifierForVariable:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForEnumConstant:
                excludedModifiers = ~Modifier.NONE;
                break;
            case IProblem.IllegalModifierForEnumConstructor:
                excludedModifiers = ~Modifier.PRIVATE;
                break;
            case IProblem.IllegalModifierForMemberEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC | Modifier.STRICTFP);
                break;
            default:
                //$NON-NLS-1$
                Assert.isTrue(false, "not supported");
                return;
        }
        if (label == null)
            label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_removeinvalidmodifiers_description, methodName);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance, image));
        if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
            proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1, image));
        }
        if (problemId == IProblem.UnexpectedStaticModifierForField && binding instanceof IVariableBinding) {
            ITypeBinding declClass = ((IVariableBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostaticfinal_description, cu, binding, selectedNode, Modifier.FINAL, Modifier.VOLATILE, relevance + 1, image));
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1, image));
                }
            }
        }
        if (problemId == IProblem.UnexpectedStaticModifierForMethod && binding instanceof IMethodBinding) {
            ITypeBinding declClass = ((IMethodBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1, image));
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Image(org.eclipse.swt.graphics.Image) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)173 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)110 ASTNode (org.eclipse.jdt.core.dom.ASTNode)46 Expression (org.eclipse.jdt.core.dom.Expression)36 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)29 ArrayList (java.util.ArrayList)27 IBinding (org.eclipse.jdt.core.dom.IBinding)25 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)21 CastExpression (org.eclipse.jdt.core.dom.CastExpression)20 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)19 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)17 Image (org.eclipse.swt.graphics.Image)16 AST (org.eclipse.jdt.core.dom.AST)15 Type (org.eclipse.jdt.core.dom.Type)15