Search in sources :

Example 56 with IMethodBinding

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

the class ConvertIterableLoopOperation method convert.

@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
    final AST ast = cuRewrite.getAST();
    final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
    final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
    final ImportRemover remover = cuRewrite.getImportRemover();
    fEnhancedForLoop = ast.newEnhancedForStatement();
    String[] names = getVariableNameProposals();
    String name;
    if (fElementVariable != null) {
        name = fElementVariable.getName();
    } else {
        name = names[0];
    }
    final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
    if (fElementVariable != null)
        pg.addProposal(name, null, 10);
    for (int i = 0; i < names.length; i++) {
        pg.addProposal(names[i], null, 10);
    }
    final Statement body = getForStatement().getBody();
    if (body != null) {
        final ListRewrite list;
        if (body instanceof Block) {
            list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
            for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
                final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
                if (parent != null && list.getRewrittenList().contains(parent)) {
                    list.remove(parent, null);
                    remover.registerRemovedNode(parent);
                }
            }
        } else {
            list = null;
        }
        final String text = name;
        body.accept(new ASTVisitor() {

            private boolean replace(final Expression expression) {
                final SimpleName node = ast.newSimpleName(text);
                astRewrite.replace(expression, node, group);
                remover.registerRemovedNode(expression);
                pg.addPosition(astRewrite.track(node), false);
                return false;
            }

            @Override
            public final boolean visit(final MethodInvocation node) {
                final IMethodBinding binding = node.resolveMethodBinding();
                if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    final Expression expression = node.getExpression();
                    if (expression instanceof Name) {
                        final IBinding result = ((Name) expression).resolveBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    } else if (expression instanceof FieldAccess) {
                        final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    }
                }
                return super.visit(node);
            }

            @Override
            public final boolean visit(final SimpleName node) {
                if (fElementVariable != null) {
                    final IBinding binding = node.resolveBinding();
                    if (binding != null && binding.equals(fElementVariable)) {
                        final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
                        if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
                            pg.addPosition(astRewrite.track(node), false);
                    }
                }
                return false;
            }
        });
        fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
    }
    final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
    final SimpleName simple = ast.newSimpleName(name);
    pg.addPosition(astRewrite.track(simple), true);
    declaration.setName(simple);
    final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
    declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
    if (fMakeFinal) {
        ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
    }
    remover.registerAddedImport(elementType.getQualifiedName());
    fEnhancedForLoop.setParameter(declaration);
    fEnhancedForLoop.setExpression(getExpression(astRewrite));
    for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
            remover.registerRemovedNode(variableDeclarationExpression.getType());
        } else {
            remover.registerRemovedNode(node);
        }
    }
    for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        remover.registerRemovedNode(node);
    }
    return fEnhancedForLoop;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover) AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 57 with IMethodBinding

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

the class LambdaExpressionsFix method isFunctionalAnonymous.

static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
    ITypeBinding typeBinding = node.resolveTypeBinding();
    if (typeBinding == null)
        return false;
    ITypeBinding[] interfaces = typeBinding.getInterfaces();
    if (interfaces.length != 1)
        return false;
    if (interfaces[0].getFunctionalInterfaceMethod() == null)
        return false;
    AnonymousClassDeclaration anonymTypeDecl = node.getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
        return false;
    List<BodyDeclaration> bodyDeclarations = anonymTypeDecl.bodyDeclarations();
    // cannot convert if there are fields or additional methods
    if (bodyDeclarations.size() != 1)
        return false;
    BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
    if (!(bodyDeclaration instanceof MethodDeclaration))
        return false;
    MethodDeclaration methodDecl = (MethodDeclaration) bodyDeclaration;
    IMethodBinding methodBinding = methodDecl.resolveBinding();
    if (methodBinding == null)
        return false;
    // generic lambda expressions are not allowed
    if (methodBinding.isGenericMethod())
        return false;
    // lambda cannot refer to 'this'/'super' literals
    if (SuperThisReferenceFinder.hasReference(methodDecl))
        return false;
    if (!isInTargetTypeContext(node))
        return false;
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 58 with IMethodBinding

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

the class Checks method checkMethodInType.

//---- New method name checking -------------------------------------------------------------
/**
	 * Checks if the new method is already used in the given type.
	 * @param type
	 * @param methodName
	 * @param parameters
	 * @return the status
	 */
public static RefactoringStatus checkMethodInType(ITypeBinding type, String methodName, ITypeBinding[] parameters) {
    RefactoringStatus result = new RefactoringStatus();
    IMethodBinding method = org.eclipse.jdt.internal.corext.dom.Bindings.findMethodInType(type, methodName, parameters);
    if (method != null) {
        if (method.isConstructor()) {
            result.addWarning(Messages.format(RefactoringCoreMessages.Checks_methodName_constructor, new Object[] { BasicElementLabels.getJavaElementName(type.getName()) }));
        } else {
            result.addError(Messages.format(RefactoringCoreMessages.Checks_methodName_exists, new Object[] { BasicElementLabels.getJavaElementName(methodName), BasicElementLabels.getJavaElementName(type.getName()) }), JavaStatusContext.create(method));
        }
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 59 with IMethodBinding

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

the class SelfEncapsulateFieldRefactoring method checkName.

private static boolean checkName(String name, List<IMethodBinding> usedNames) {
    for (Iterator<IMethodBinding> iter = usedNames.iterator(); iter.hasNext(); ) {
        IMethodBinding method = iter.next();
        String selector = method.getName();
        if (selector.equals(name)) {
            return true;
        }
    }
    return false;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding)

Example 60 with IMethodBinding

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

the class SelfEncapsulateFieldRefactoring method checkMethodInHierarchy.

public static void checkMethodInHierarchy(ITypeBinding type, String methodName, ITypeBinding returnType, ITypeBinding[] parameters, RefactoringStatus result, boolean reUseMethod) {
    IMethodBinding method = Bindings.findMethodInHierarchy(type, methodName, parameters);
    if (method != null) {
        boolean returnTypeClash = false;
        ITypeBinding methodReturnType = method.getReturnType();
        if (returnType != null && methodReturnType != null) {
            String returnTypeKey = returnType.getKey();
            String methodReturnTypeKey = methodReturnType.getKey();
            if (returnTypeKey == null && methodReturnTypeKey == null) {
                returnTypeClash = returnType != methodReturnType;
            } else if (returnTypeKey != null && methodReturnTypeKey != null) {
                returnTypeClash = !returnTypeKey.equals(methodReturnTypeKey);
            }
        }
        ITypeBinding dc = method.getDeclaringClass();
        if (returnTypeClash) {
            result.addError(Messages.format(RefactoringCoreMessages.Checks_methodName_returnTypeClash, new Object[] { BasicElementLabels.getJavaElementName(methodName), BasicElementLabels.getJavaElementName(dc.getName()) }), JavaStatusContext.create(method));
        } else {
            if (!reUseMethod)
                result.addError(Messages.format(RefactoringCoreMessages.Checks_methodName_overrides, new Object[] { BasicElementLabels.getJavaElementName(methodName), BasicElementLabels.getJavaElementName(dc.getName()) }), JavaStatusContext.create(method));
        }
    } else {
        if (reUseMethod) {
            result.addError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_nosuchmethod_status_fatalError, BasicElementLabels.getJavaElementName(methodName)), JavaStatusContext.create(method));
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

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