Search in sources :

Example 21 with SuperConstructorInvocation

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

the class UnresolvedElementsSubProcessor method getConstructorProposals.

public static void getConstructorProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    ITypeBinding targetBinding = null;
    List<Expression> arguments = null;
    IMethodBinding recursiveConstructor = null;
    int type = selectedNode.getNodeType();
    if (type == ASTNode.CLASS_INSTANCE_CREATION) {
        ClassInstanceCreation creation = (ClassInstanceCreation) selectedNode;
        IBinding binding = creation.getType().resolveBinding();
        if (binding instanceof ITypeBinding) {
            targetBinding = (ITypeBinding) binding;
            arguments = creation.arguments();
        }
    } else if (type == ASTNode.SUPER_CONSTRUCTOR_INVOCATION) {
        ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
        if (typeBinding != null && !typeBinding.isAnonymous()) {
            targetBinding = typeBinding.getSuperclass();
            arguments = ((SuperConstructorInvocation) selectedNode).arguments();
        }
    } else if (type == ASTNode.CONSTRUCTOR_INVOCATION) {
        ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
        if (typeBinding != null && !typeBinding.isAnonymous()) {
            targetBinding = typeBinding;
            arguments = ((ConstructorInvocation) selectedNode).arguments();
            recursiveConstructor = ASTResolving.findParentMethodDeclaration(selectedNode).resolveBinding();
        }
    }
    if (targetBinding == null) {
        return;
    }
    IMethodBinding[] methods = targetBinding.getDeclaredMethods();
    ArrayList<IMethodBinding> similarElements = new ArrayList<>();
    for (int i = 0; i < methods.length; i++) {
        IMethodBinding curr = methods[i];
        if (curr.isConstructor() && recursiveConstructor != curr) {
            // similar elements can contain a implicit default constructor
            similarElements.add(curr);
        }
    }
    addParameterMissmatchProposals(context, problem, similarElements, selectedNode, arguments, proposals);
    if (targetBinding.isFromSource()) {
        ITypeBinding targetDecl = targetBinding.getTypeDeclaration();
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, targetDecl);
        if (targetCU != null) {
            String[] args = new String[] { org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getTypeSignature(targetDecl), getParameterTypes(arguments), false) };
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconstructor_description, args);
            proposals.add(new NewMethodCorrectionProposal(label, targetCU, selectedNode, arguments, targetDecl, IProposalRelevance.CREATE_CONSTRUCTOR));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 22 with SuperConstructorInvocation

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

the class JDTUtils 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 if (node instanceof LambdaExpression) {
        return ((LambdaExpression) node).resolveMethodBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) 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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 23 with SuperConstructorInvocation

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

the class ChangeSignatureProcessor method containsImplicitCallToSuperConstructor.

private static boolean containsImplicitCallToSuperConstructor(MethodDeclaration constructor) {
    Assert.isTrue(constructor.isConstructor());
    Block body = constructor.getBody();
    if (body == null) {
        return false;
    }
    if (body.statements().size() == 0) {
        return true;
    }
    if (body.statements().get(0) instanceof ConstructorInvocation) {
        return false;
    }
    if (body.statements().get(0) instanceof SuperConstructorInvocation) {
        return false;
    }
    return true;
}
Also used : SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 24 with SuperConstructorInvocation

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

the class ChangeSignatureProcessor method addNewConstructorToSubclass.

private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) {
    AST ast = subclass.getAST();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier()));
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass)));
    newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    Block body = ast.newBlock();
    newConstructor.setBody(body);
    SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation();
    addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
    body.statements().add(superCall);
    String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor;
    TextEditGroup description = cuRewrite.createGroupDescription(msg);
    cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description);
// TODO use AbstractTypeDeclaration
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 25 with SuperConstructorInvocation

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

the class ConstructorFromSuperclassProposal method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
    String name = fTypeNode.getName().getIdentifier();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.setConstructor(true);
    decl.setName(ast.newSimpleName(name));
    Block body = ast.newBlock();
    decl.setBody(body);
    SuperConstructorInvocation invocation = null;
    List<SingleVariableDeclaration> parameters = decl.parameters();
    String[] paramNames = getArgumentNames(binding);
    ITypeBinding enclosingInstance = getEnclosingInstance();
    if (enclosingInstance != null) {
        invocation = addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
    }
    if (binding == null) {
        decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    } else {
        decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));
        ITypeBinding[] params = binding.getParameterTypes();
        for (int i = 0; i < params.length; i++) {
            SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
            var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE));
            var.setName(ast.newSimpleName(paramNames[i]));
            parameters.add(var);
        }
        List<Type> thrownExceptions = decl.thrownExceptionTypes();
        ITypeBinding[] excTypes = binding.getExceptionTypes();
        for (int i = 0; i < excTypes.length; i++) {
            Type excType = getImportRewrite().addImport(excTypes[i], ast, importRewriteContext, TypeLocation.EXCEPTION);
            thrownExceptions.add(excType);
        }
        if (invocation == null) {
            invocation = ast.newSuperConstructorInvocation();
        }
        List<Expression> arguments = invocation.arguments();
        for (int i = 0; i < paramNames.length; i++) {
            Name argument = ast.newSimpleName(paramNames[i]);
            arguments.add(argument);
            // $NON-NLS-1$
            addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]);
        }
    }
    // $NON-NLS-1$
    String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getOptions(true));
    String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
    if (placeHolder != null) {
        ASTNode todoNode = rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
        body.statements().add(todoNode);
    }
    if (commentSettings != null) {
        String string = CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Name(org.eclipse.jdt.core.dom.Name) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Aggregations

SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)25 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 Block (org.eclipse.jdt.core.dom.Block)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)9 ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)9 Expression (org.eclipse.jdt.core.dom.Expression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 AST (org.eclipse.jdt.core.dom.AST)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)8 Javadoc (org.eclipse.jdt.core.dom.Javadoc)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ArrayList (java.util.ArrayList)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 Name (org.eclipse.jdt.core.dom.Name)4 Type (org.eclipse.jdt.core.dom.Type)4 TextEditGroup (org.eclipse.text.edits.TextEditGroup)4