Search in sources :

Example 6 with SuperConstructorInvocation

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

the class ChangeSignatureProcessor method addExplicitSuperConstructorCall.

private void addExplicitSuperConstructorCall(MethodDeclaration constructor, CompilationUnitRewrite cuRewrite) {
    SuperConstructorInvocation superCall = constructor.getAST().newSuperConstructorInvocation();
    addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
    String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_super_call;
    TextEditGroup description = cuRewrite.createGroupDescription(msg);
    cuRewrite.getASTRewrite().getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY).insertFirst(superCall, description);
}
Also used : SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 7 with SuperConstructorInvocation

use of org.eclipse.jdt.core.dom.SuperConstructorInvocation 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 8 with SuperConstructorInvocation

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

the class ConvertAnonymousToNestedRefactoring method createNewConstructor.

private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
    ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
        return null;
    IJavaProject project = fCu.getJavaProject();
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = rewrite.getImportRewrite();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
    newConstructor.setName(ast.newSimpleName(fClassName));
    addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
    newConstructor.setBody(ast.newBlock());
    List<Statement> newStatements = newConstructor.getBody().statements();
    List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
    List<String> newParameterNames = new ArrayList<String>();
    // add parameters for elements passed with the instance creation
    if (!instanceCreation.arguments().isEmpty()) {
        IMethodBinding constructorBinding = getSuperConstructorBinding();
        if (constructorBinding != null) {
            SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
            ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
            String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
            for (int i = 0; i < parameterNames.length; i++) {
                String[] nameProposals = parameterNames[i];
                String paramName = nameProposals[0];
                SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
                newParameters.add(param);
                newParameterNames.add(paramName);
                SimpleName newSIArgument = ast.newSimpleName(paramName);
                superConstructorInvocation.arguments().add(newSIArgument);
                if (fLinkedProposalModel != null) {
                    LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
                    positionGroup.addPosition(astRewrite.track(param.getName()), false);
                    positionGroup.addPosition(astRewrite.track(newSIArgument), false);
                    for (int k = 0; k < nameProposals.length; k++) {
                        positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
                    }
                }
            }
            newStatements.add(superConstructorInvocation);
        }
    }
    // add parameters for all outer variables used
    boolean useThisAccess = useThisForFieldAccess();
    for (int i = 0; i < bindings.length; i++) {
        String baseName = StubUtility.getBaseName(bindings[i], project);
        String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
        String paramName = paramNameProposals[0];
        SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
        newParameters.add(param);
        newParameterNames.add(paramName);
        String fieldName = fieldNames[i];
        SimpleName fieldNameNode = ast.newSimpleName(fieldName);
        SimpleName paramNameNode = ast.newSimpleName(paramName);
        newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
            positionGroup.addPosition(astRewrite.track(param.getName()), false);
            positionGroup.addPosition(astRewrite.track(paramNameNode), false);
            for (int k = 0; k < paramNameProposals.length; k++) {
                positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
            }
            fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
        }
    }
    addExceptionsToNewConstructor(newConstructor, importRewrite);
    if (doAddComments()) {
        try {
            String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
            String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                newConstructor.setJavadoc(javadoc);
            }
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    }
    return newConstructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 9 with SuperConstructorInvocation

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

the class ASTNodes method getTargetType.

/**
	 * Derives the target type defined at the location of the given expression if the target context
	 * supports poly expressions.
	 * 
	 * @param expression the expression at whose location the target type is required
	 * @return the type binding of the target type defined at the location of the given expression
	 *         if the target context supports poly expressions, or <code>null</code> if the target
	 *         type could not be derived
	 * 
	 * @since 3.10
	 */
public static ITypeBinding getTargetType(Expression expression) {
    ASTNode parent = expression.getParent();
    StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
        return ((VariableDeclaration) parent).getName().resolveTypeBinding();
    } else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        return getTargetTypeForReturnStmt((ReturnStatement) parent);
    } else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
        return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
    } else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null) {
            return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
        }
    } else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
        IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
        if (superMethodBinding != null) {
            return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
        }
    } else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
        ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
        IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
        if (constructorBinding != null) {
            return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
        }
    } else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
        IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
        if (superConstructorBinding != null) {
            return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
        }
    } else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        ClassInstanceCreation creation = (ClassInstanceCreation) parent;
        IMethodBinding creationBinding = creation.resolveConstructorBinding();
        if (creationBinding != null) {
            return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
        }
    } else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
        EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
        IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
        if (enumConstructorBinding != null) {
            return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
        }
    } else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
        IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
        if (methodBinding != null) {
            return methodBinding.getReturnType();
        }
    } else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        return getTargetType((ConditionalExpression) parent);
    } else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
        return ((CastExpression) parent).getType().resolveBinding();
    } else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return getTargetType((ParenthesizedExpression) parent);
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 10 with SuperConstructorInvocation

use of org.eclipse.jdt.core.dom.SuperConstructorInvocation in project che 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)

Aggregations

SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)14 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)7 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)6 ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)6 AST (org.eclipse.jdt.core.dom.AST)5 Block (org.eclipse.jdt.core.dom.Block)5 Expression (org.eclipse.jdt.core.dom.Expression)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 Javadoc (org.eclipse.jdt.core.dom.Javadoc)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 ArrayList (java.util.ArrayList)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 CoreException (org.eclipse.core.runtime.CoreException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2