Search in sources :

Example 86 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 87 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 88 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)

Example 89 with IMethodBinding

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

the class AnnotationRefactoring method toElementsMap.

private Map<String, IMethodBinding> toElementsMap(IAnnotationBinding annotBinding) {
    if (annotBinding == null) {
        return Collections.emptyMap();
    }
    ITypeBinding annotationType = annotBinding.getAnnotationType();
    IMethodBinding[] elements = annotationType.getDeclaredMethods();
    Map<String, IMethodBinding> results = new HashMap<String, IMethodBinding>();
    for (IMethodBinding element : elements) {
        results.put(element.getName(), element);
    }
    return results;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 90 with IMethodBinding

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

the class RemoveUnneededThisExpressionRefactoring method isCallingMethodDeclaredInEnclosingType.

private boolean isCallingMethodDeclaredInEnclosingType(MethodInvocation node) {
    final ASTNode currentType = getEnclosingType(node);
    final IMethodBinding mb = node.resolveMethodBinding();
    if (currentType instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration c = (AnonymousClassDeclaration) currentType;
        final ITypeBinding enclosingTypeBinding = c.resolveBinding();
        return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
    } else if (currentType instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration ed = (AbstractTypeDeclaration) currentType;
        final ITypeBinding enclosingTypeBinding = ed.resolveBinding();
        return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
    }
    throw new NotImplementedException(node, node);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

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