Search in sources :

Example 21 with SuperMethodInvocation

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

the class SuperCallRatherThanUselessOverridingRefactoring method visit.

@Override
public boolean visit(MethodDeclaration node) {
    if (node.getBody() == null) {
        return VISIT_SUBTREE;
    }
    List<Statement> bodyStmts = statements(node.getBody());
    if (bodyStmts.size() == 1) {
        SuperMethodInvocation bodyMi = asExpression(bodyStmts.get(0), SuperMethodInvocation.class);
        if (bodyMi != null) {
            IMethodBinding bodyMethodBinding = bodyMi.resolveMethodBinding();
            IMethodBinding declMethodBinding = node.resolveBinding();
            if (declMethodBinding != null && bodyMethodBinding != null && declMethodBinding.overrides(bodyMethodBinding) && !hasSignificantAnnotations(declMethodBinding) && haveSameModifiers(bodyMethodBinding, declMethodBinding)) {
                if (Modifier.isProtected(declMethodBinding.getModifiers()) && !declaredInSamePackage(bodyMethodBinding, declMethodBinding)) {
                    // protected also means package visibility, so check if it is required
                    if (!isMethodUsedInItsPackage(declMethodBinding, node)) {
                        this.ctx.getRefactorings().remove(node);
                        return DO_NOT_VISIT_SUBTREE;
                    }
                } else {
                    this.ctx.getRefactorings().remove(node);
                    return DO_NOT_VISIT_SUBTREE;
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 22 with SuperMethodInvocation

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

the class ASTBuilder method superInvoke.

/**
 * Builds a new super method invocation.
 *
 * @param methodName name of the method to be invoked
 * @return expression with a method invocation
 */
public Expression superInvoke(String methodName) {
    SuperMethodInvocation smi = ast.newSuperMethodInvocation();
    smi.setName(simpleName(methodName));
    return smi;
}
Also used : SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 23 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method convertMethodRefernceToLambda.

/**
 * Converts and replaces the given method reference with corresponding lambda
 * expression in the given ASTRewrite.
 *
 * @param methodReference
 *            the method reference to convert
 * @param functionalMethod
 *            the non-generic functional interface method to be implemented by
 *            the lambda expression
 * @param astRoot
 *            the AST root
 * @param rewrite
 *            the ASTRewrite
 * @param linkedProposalModel
 *            to create linked proposals for lambda's parameters or
 *            <code>null</code> if linked proposals are not required
 * @param createBlockBody
 *            <code>true</code> if lambda expression's body should be a block
 *
 * @return lambda expression used to replace the method reference in the given
 *         ASTRewrite
 * @throws JavaModelException
 *             if an exception occurs while accessing the Java element
 *             corresponding to the <code>functionalMethod</code>
 */
public static LambdaExpression convertMethodRefernceToLambda(MethodReference methodReference, IMethodBinding functionalMethod, CompilationUnit astRoot, ASTRewrite rewrite, LinkedProposalModel linkedProposalModel, boolean createBlockBody) throws JavaModelException {
    AST ast = astRoot.getAST();
    LambdaExpression lambda = ast.newLambdaExpression();
    String[] lambdaParamNames = getUniqueParameterNames(methodReference, functionalMethod);
    List<VariableDeclaration> lambdaParameters = lambda.parameters();
    for (int i = 0; i < lambdaParamNames.length; i++) {
        String paramName = lambdaParamNames[i];
        VariableDeclarationFragment lambdaParameter = ast.newVariableDeclarationFragment();
        SimpleName name = ast.newSimpleName(paramName);
        lambdaParameter.setName(name);
        lambdaParameters.add(lambdaParameter);
        if (linkedProposalModel != null) {
            linkedProposalModel.getPositionGroup(name.getIdentifier(), true).addPosition(rewrite.track(name), i == 0);
        }
    }
    int noOfLambdaParameters = lambdaParamNames.length;
    lambda.setParentheses(noOfLambdaParameters != 1);
    ITypeBinding returnTypeBinding = functionalMethod.getReturnType();
    // too often null, see bug 440000, bug 440344, bug 333665
    IMethodBinding referredMethodBinding = methodReference.resolveMethodBinding();
    if (methodReference instanceof CreationReference) {
        CreationReference creationRef = (CreationReference) methodReference;
        Type type = creationRef.getType();
        if (type instanceof ArrayType) {
            ArrayCreation arrayCreation = ast.newArrayCreation();
            if (createBlockBody) {
                Block blockBody = getBlockBodyForLambda(arrayCreation, returnTypeBinding, ast);
                lambda.setBody(blockBody);
            } else {
                lambda.setBody(arrayCreation);
            }
            ArrayType arrayType = (ArrayType) type;
            Type copiedElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType());
            arrayCreation.setType(ast.newArrayType(copiedElementType, arrayType.getDimensions()));
            SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
            arrayCreation.dimensions().add(name);
            if (linkedProposalModel != null) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        } else {
            ClassInstanceCreation cic = ast.newClassInstanceCreation();
            if (createBlockBody) {
                Block blockBody = getBlockBodyForLambda(cic, returnTypeBinding, ast);
                lambda.setBody(blockBody);
            } else {
                lambda.setBody(cic);
            }
            ITypeBinding typeBinding = type.resolveBinding();
            if (!(type instanceof ParameterizedType) && typeBinding != null && typeBinding.getTypeDeclaration().isGenericType()) {
                cic.setType(ast.newParameterizedType((Type) rewrite.createCopyTarget(type)));
            } else {
                cic.setType((Type) rewrite.createCopyTarget(type));
            }
            List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
            cic.arguments().addAll(invocationArgs);
            if (linkedProposalModel != null) {
                for (SimpleName name : invocationArgs) {
                    linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
                }
            }
            cic.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
        }
    } else if (referredMethodBinding != null && Modifier.isStatic(referredMethodBinding.getModifiers())) {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(methodInvocation);
        }
        Expression expr = null;
        boolean hasConflict = hasConflict(methodReference.getStartPosition(), referredMethodBinding, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY, astRoot);
        if (hasConflict || !Bindings.isSuperType(referredMethodBinding.getDeclaringClass(), ASTNodes.getEnclosingType(methodReference)) || methodReference.typeArguments().size() != 0) {
            if (methodReference instanceof ExpressionMethodReference) {
                ExpressionMethodReference expressionMethodReference = (ExpressionMethodReference) methodReference;
                expr = (Expression) rewrite.createCopyTarget(expressionMethodReference.getExpression());
            } else if (methodReference instanceof TypeMethodReference) {
                Type type = ((TypeMethodReference) methodReference).getType();
                ITypeBinding typeBinding = type.resolveBinding();
                if (typeBinding != null) {
                    ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true);
                    expr = ast.newName(importRewrite.addImport(typeBinding));
                }
            }
        }
        methodInvocation.setExpression(expr);
        SimpleName methodName = getMethodInvocationName(methodReference);
        methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
        methodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    } else if (methodReference instanceof SuperMethodReference) {
        SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(superMethodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(superMethodInvocation);
        }
        Name superQualifier = ((SuperMethodReference) methodReference).getQualifier();
        if (superQualifier != null) {
            superMethodInvocation.setQualifier((Name) rewrite.createCopyTarget(superQualifier));
        }
        SimpleName methodName = getMethodInvocationName(methodReference);
        superMethodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
        superMethodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        superMethodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    } else {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(methodInvocation);
        }
        boolean isTypeReference = isTypeReferenceToInstanceMethod(methodReference);
        if (isTypeReference) {
            SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
            methodInvocation.setExpression(name);
            if (linkedProposalModel != null) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        } else {
            Expression expr = ((ExpressionMethodReference) methodReference).getExpression();
            if (!(expr instanceof ThisExpression && methodReference.typeArguments().size() == 0)) {
                methodInvocation.setExpression((Expression) rewrite.createCopyTarget(expr));
            }
        }
        SimpleName methodName = getMethodInvocationName(methodReference);
        methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, isTypeReference ? 1 : 0, noOfLambdaParameters, lambdaParamNames);
        methodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    }
    rewrite.replace(methodReference, lambda, null);
    return lambda;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) CreationReference(org.eclipse.jdt.core.dom.CreationReference) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) TypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference) List(java.util.List) ArrayList(java.util.ArrayList) SuperMethodReference(org.eclipse.jdt.core.dom.SuperMethodReference) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) ExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) UnionType(org.eclipse.jdt.core.dom.UnionType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 24 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project whole by wholeplatform.

the class CompilationUnitBuilder method newCloneMethod.

public MethodDeclaration newCloneMethod() {
    MethodDeclaration cloneMethod = ast.newMethodDeclaration();
    cloneMethod.setConstructor(false);
    cloneMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    cloneMethod.setReturnType2(ast.newSimpleType(newSimpleName("java.lang.Object")));
    cloneMethod.setName(ast.newSimpleName("clone"));
    Block body = newBlock();
    CastExpression castExp = ast.newCastExpression();
    castExp.setType(newType(typeDec.getName().getIdentifier()));
    SuperMethodInvocation superCall = ast.newSuperMethodInvocation();
    superCall.setName(ast.newSimpleName("clone"));
    castExp.setExpression(superCall);
    VariableDeclarationFragment varDec = ast.newVariableDeclarationFragment();
    varDec.setName(ast.newSimpleName("obj"));
    varDec.setInitializer(castExp);
    VariableDeclarationStatement varDecStm = ast.newVariableDeclarationStatement(varDec);
    varDecStm.setType(newType(typeDec.getName().getIdentifier()));
    body.statements().add(varDecStm);
    ReturnStatement returnStm = ast.newReturnStatement();
    returnStm.setExpression(ast.newSimpleName("obj"));
    body.statements().add(returnStm);
    cloneMethod.setBody(body);
    return cloneMethod;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 25 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project whole by wholeplatform.

the class CompilationUnitBuilder method newSuperMethodInvocation.

public SuperMethodInvocation newSuperMethodInvocation(String method, Expression arg0) {
    SuperMethodInvocation callExp = newSuperMethodInvocation(method);
    callExp.arguments().add(arg0);
    return callExp;
}
Also used : SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Aggregations

SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)29 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 IBinding (org.eclipse.jdt.core.dom.IBinding)8 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)7 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)7 Name (org.eclipse.jdt.core.dom.Name)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 AST (org.eclipse.jdt.core.dom.AST)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)5 Block (org.eclipse.jdt.core.dom.Block)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)4