Search in sources :

Example 21 with IMethodBinding

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

the class IntroduceIndirectionRefactoring method findMethodBindingInHierarchy.

// ********* SMALL HELPERS ********************
/*
	 * Helper method for finding an IMethod inside a binding hierarchy
	 */
private IMethodBinding findMethodBindingInHierarchy(ITypeBinding currentTypeBinding, IMethod methodDeclaration) {
    IMethodBinding[] bindings = currentTypeBinding.getDeclaredMethods();
    for (int i = 0; i < bindings.length; i++) if (methodDeclaration.equals(bindings[i].getJavaElement()))
        return bindings[i];
    ITypeBinding superClass = currentTypeBinding.getSuperclass();
    if (superClass != null) {
        IMethodBinding b = findMethodBindingInHierarchy(superClass, methodDeclaration);
        if (b != null)
            return b;
    }
    ITypeBinding[] interfaces = currentTypeBinding.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        IMethodBinding b = findMethodBindingInHierarchy(interfaces[i], methodDeclaration);
        if (b != null)
            return b;
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 22 with IMethodBinding

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

the class IntroduceIndirectionRefactoring method qualifyThisExpression.

/**
	 * Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
	 * The invoked method is analyzed according to the following specs:
	 *
	 * 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
	 *
	 * 1) The method is declared somewhere outside of the cu of the invocation
	 *      1a) inside a supertype of the current type
	 *      1b) inside a supertype of an enclosing type
	 * 2) The method is declared inside of the cu of the invocation
	 * 		2a) inside the type of the invocation
	 * 		2b) outside the type of the invocation
	 *
	 * In case of 1a) and 2b), qualify with the enclosing type.
	 * @param expr a {@link ThisExpression}
	 * @param originalInvocation the original method invocation
	 * @param enclosing the enclosing member of the original method invocation
	 * @param unitRewriter the rewrite
	 * @return resulting status
	 *
	 */
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {
    RefactoringStatus status = new RefactoringStatus();
    IMethodBinding methodBinding = originalInvocation.resolveMethodBinding();
    MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());
    ITypeBinding currentTypeBinding = null;
    if (methodDeclaration != null) {
        // Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
        if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
            currentTypeBinding = methodBinding.getDeclaringClass();
        else
            currentTypeBinding = ASTNodes.getEnclosingType(originalInvocation);
    } else {
        // Case 2) : Declaring type is outside of this cu => find subclass in this cu
        ASTNode currentTypeDeclaration = getEnclosingTypeDeclaration(originalInvocation);
        currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
            currentTypeDeclaration = getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
            currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        }
    }
    if (currentTypeBinding == null) {
        status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
        return status;
    }
    currentTypeBinding = currentTypeBinding.getTypeDeclaration();
    ITypeBinding typeOfCall = ASTNodes.getEnclosingType(originalInvocation);
    if (!typeOfCall.equals(currentTypeBinding)) {
        if (currentTypeBinding.isAnonymous()) {
            // Cannot qualify, see bug 115277
            status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
        } else {
            expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
        }
    } else {
    // do not qualify, only use "this.".
    }
    return status;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 23 with IMethodBinding

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

the class ExtractMethodAnalyzer method initReturnType.

private void initReturnType(ImportRewrite rewriter) {
    AST ast = fEnclosingBodyDeclaration.getAST();
    fReturnType = null;
    fReturnTypeBinding = null;
    switch(fReturnKind) {
        case ACCESS_TO_LOCAL:
            VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
            fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
            if (declaration.resolveBinding() != null) {
                fReturnTypeBinding = declaration.resolveBinding().getType();
            }
            break;
        case EXPRESSION:
            Expression expression = (Expression) getFirstSelectedNode();
            if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
                fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
            } else {
                fExpressionBinding = expression.resolveTypeBinding();
            }
            if (fExpressionBinding != null) {
                if (fExpressionBinding.isNullType()) {
                    getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
                } else {
                    ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
                    if (normalizedBinding != null) {
                        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
                        fReturnType = rewriter.addImport(normalizedBinding, ast, context);
                        fReturnTypeBinding = normalizedBinding;
                    }
                }
            } else {
                fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
                //$NON-NLS-1$
                fReturnTypeBinding = ast.resolveWellKnownType("void");
                getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
            }
            break;
        case RETURN_STATEMENT_VALUE:
            LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
            if (enclosingLambdaExpr != null) {
                fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
                IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
                fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
            } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
                fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
                fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
            }
            break;
        default:
            fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
            //$NON-NLS-1$
            fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
    if (fReturnType == null) {
        fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
        //$NON-NLS-1$
        fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 24 with IMethodBinding

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

the class InlineMethodRefactoring method resolveSourceProvider.

private static SourceProvider resolveSourceProvider(RefactoringStatus status, ITypeRoot typeRoot, ASTNode invocation) {
    CompilationUnit root = (CompilationUnit) invocation.getRoot();
    IMethodBinding methodBinding = Invocations.resolveBinding(invocation);
    if (methodBinding == null) {
        status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
        return null;
    }
    MethodDeclaration declaration = (MethodDeclaration) root.findDeclaringNode(methodBinding);
    if (declaration != null) {
        return new SourceProvider(typeRoot, declaration);
    }
    IMethod method = (IMethod) methodBinding.getJavaElement();
    if (method != null) {
        CompilationUnit methodDeclarationAstRoot;
        ICompilationUnit methodCu = method.getCompilationUnit();
        if (methodCu != null) {
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(methodCu, true);
        } else {
            IClassFile classFile = method.getClassFile();
            if (!JavaElementUtil.isSourceAvailable(classFile)) {
                String methodLabel = JavaElementLabels.getTextLabel(method, JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES);
                status.addFatalError(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_error_classFile, methodLabel));
                return null;
            }
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(classFile, true);
        }
        ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getMethodDeclaration().getKey());
        if (node instanceof MethodDeclaration) {
            return new SourceProvider(methodDeclarationAstRoot.getTypeRoot(), (MethodDeclaration) node);
        }
    }
    status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 25 with IMethodBinding

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

the class Invocations method getInferredTypeArguments.

public static ITypeBinding[] getInferredTypeArguments(Expression invocation) {
    IMethodBinding methodBinding;
    switch(invocation.getNodeType()) {
        case ASTNode.METHOD_INVOCATION:
            methodBinding = ((MethodInvocation) invocation).resolveMethodBinding();
            return methodBinding == null ? null : methodBinding.getTypeArguments();
        case ASTNode.SUPER_METHOD_INVOCATION:
            methodBinding = ((SuperMethodInvocation) invocation).resolveMethodBinding();
            return methodBinding == null ? null : methodBinding.getTypeArguments();
        case ASTNode.CLASS_INSTANCE_CREATION:
            Type type = ((ClassInstanceCreation) invocation).getType();
            ITypeBinding typeBinding = type.resolveBinding();
            return typeBinding == null ? null : typeBinding.getTypeArguments();
        default:
            throw new IllegalArgumentException(invocation.toString());
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Aggregations

IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)164 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)46 Expression (org.eclipse.jdt.core.dom.Expression)34 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)28 ArrayList (java.util.ArrayList)27 IBinding (org.eclipse.jdt.core.dom.IBinding)24 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)18 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)18 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