Search in sources :

Example 61 with IMethodBinding

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

the class SelfEncapsulateFieldRefactoring method checkName.

private static void checkName(RefactoringStatus status, String name, List<IMethodBinding> usedNames, IType type, boolean reUseExistingField, IField field) {
    if ("".equals(name)) {
        //$NON-NLS-1$
        status.addFatalError(RefactoringCoreMessages.Checks_Choose_name);
        return;
    }
    boolean isStatic = false;
    try {
        isStatic = Flags.isStatic(field.getFlags());
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    status.merge(Checks.checkMethodName(name, field));
    for (Iterator<IMethodBinding> iter = usedNames.iterator(); iter.hasNext(); ) {
        IMethodBinding method = iter.next();
        String selector = method.getName();
        if (selector.equals(name)) {
            if (!reUseExistingField) {
                status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_method_exists, new String[] { BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(type.getElementName()) }));
            } else {
                boolean methodIsStatic = Modifier.isStatic(method.getModifiers());
                if (methodIsStatic && !isStatic)
                    status.addWarning(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_static_method_but_nonstatic_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
                if (!methodIsStatic && isStatic)
                    status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_nonstatic_method_but_static_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
                return;
            }
        }
    }
    if (reUseExistingField)
        status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_methoddoesnotexist_status_fatalError, new String[] { BasicElementLabels.getJavaElementName(name), BasicElementLabels.getJavaElementName(type.getElementName()) }));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException)

Example 62 with IMethodBinding

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

the class JavadocFinder method resolveBinding.

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }
        return simpleName.resolveBinding();
    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 63 with IMethodBinding

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

the class CallContext method getReceiverType.

public ITypeBinding getReceiverType() {
    Expression expression = Invocations.getExpression(invocation);
    if (expression != null) {
        return expression.resolveTypeBinding();
    }
    IMethodBinding method = Invocations.resolveBinding(invocation);
    if (method != null) {
        return method.getDeclaringClass();
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Expression(org.eclipse.jdt.core.dom.Expression)

Example 64 with IMethodBinding

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

the class CallInliner method needsExplicitCast.

/**
	 * @param status the status
	 * @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
	 */
private boolean needsExplicitCast(RefactoringStatus status) {
    // returned expression then we don't need an explicit cast.
    if (fSourceProvider.returnTypeMatchesReturnExpressions())
        return false;
    List<Expression> returnExprs = fSourceProvider.getReturnExpressions();
    // method invocations
    if (returnExprs.size() != 1)
        return false;
    if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) fTargetNode.getParent();
        if (methodInvocation.getExpression() == fTargetNode)
            return false;
        IMethodBinding method = methodInvocation.resolveMethodBinding();
        if (method == null) {
            status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error, JavaStatusContext.create(fCUnit, methodInvocation));
            return false;
        }
        ITypeBinding[] parameters = method.getParameterTypes();
        int argumentIndex = methodInvocation.arguments().indexOf(fInvocation);
        ITypeBinding parameterType = returnExprs.get(0).resolveTypeBinding();
        if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
            argumentIndex = parameters.length - 1;
            parameterType = parameterType.createArrayType(1);
        }
        parameters[argumentIndex] = parameterType;
        ITypeBinding type = ASTNodes.getReceiverTypeBinding(methodInvocation);
        TypeBindingVisitor visitor = new AmbiguousMethodAnalyzer(fTypeEnvironment, method, fTypeEnvironment.create(parameters));
        if (!visitor.visit(type)) {
            return true;
        } else if (type.isInterface()) {
            return !Bindings.visitInterfaces(type, visitor);
        } else if (Modifier.isAbstract(type.getModifiers())) {
            return !Bindings.visitHierarchy(type, visitor);
        } else {
            // it is not needed to visit interfaces if receiver is a concrete class
            return !Bindings.visitSuperclasses(type, visitor);
        }
    } else {
        ITypeBinding explicitCast = ASTNodes.getExplicitCast(returnExprs.get(0), (Expression) fTargetNode);
        return explicitCast != null;
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeBindingVisitor(org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 65 with IMethodBinding

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

the class CallInliner method replaceCall.

private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
    // Inline empty body
    if (blocks.length == 0 && fTargetNode != null) {
        if (fNeedsStatement) {
            fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
        } else {
            fRewrite.remove(fTargetNode, textEditGroup);
        }
    } else {
        ASTNode node = null;
        for (int i = 0; i < blocks.length - 1; i++) {
            node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
            fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
        }
        String block = blocks[blocks.length - 1];
        // returned expression must be evaluated.
        if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
            if (fSourceProvider.mustEvaluateReturnedExpression()) {
                if (fSourceProvider.returnValueNeedsLocalVariable()) {
                    IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
                    node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                } else {
                    node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                }
            } else {
                node = null;
            }
        } else if (fTargetNode instanceof Expression) {
            node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
            // fixes bug #24941
            if (needsExplicitCast(status)) {
                AST ast = node.getAST();
                CastExpression castExpression = ast.newCastExpression();
                Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
                castExpression.setType(returnType);
                if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
                castExpression.setExpression((Expression) node);
                node = castExpression;
                if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
            } else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
                pExp.setExpression((Expression) node);
                node = pExp;
            }
        } else {
            node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
        }
        // Now replace the target node with the source node
        if (node != null) {
            if (fTargetNode == null) {
                fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
            } else {
                fRewrite.replace(fTargetNode, node, textEditGroup);
            }
        } else {
            if (fTargetNode != null) {
                fRewrite.remove(fTargetNode, textEditGroup);
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

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