Search in sources :

Example 1 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.

public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ReturnStatementCollector eval = new ReturnStatementCollector();
        decl.accept(eval);
        AST ast = astRoot.getAST();
        ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
        typeBinding = Bindings.normalizeTypeBinding(typeBinding);
        if (typeBinding == null) {
            //$NON-NLS-1$
            typeBinding = ast.resolveWellKnownType("void");
        }
        if (typeBinding.isWildcardType()) {
            typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
        }
        ASTRewrite rewrite = ASTRewrite.create(ast);
        String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
        ImportRewrite imports = proposal.createImportRewrite(astRoot);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        Type type = imports.addImport(typeBinding, ast, importRewriteContext);
        rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
        rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null && typeBinding != null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            TextElement commentStart = ast.newTextElement();
            newTag.fragments().add(commentStart);
            JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
            //$NON-NLS-1$
            proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
        }
        //$NON-NLS-1$
        String key = "return_type";
        proposal.addLinkedPosition(rewrite.track(type), true, key);
        if (typeBinding != null) {
            ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
            for (int i = 0; i < bindings.length; i++) {
                proposal.addLinkedPositionProposal(key, bindings[i]);
            }
        }
        proposals.add(proposal);
        // change to constructor
        ASTNode parentType = ASTResolving.findParentType(decl);
        if (parentType instanceof AbstractTypeDeclaration) {
            boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
            if (!isInterface) {
                String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
                ASTNode nameNode = methodDeclaration.getName();
                label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
                proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) TextElement(org.eclipse.jdt.core.dom.TextElement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 2 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class GenerateForLoopAssistProposal method getIndexBasedForBodyAssignment.

/**
	 * Creates an {@link Assignment} as first expression appearing in an index based
	 * <code>for</code> loop's body. This Assignment declares a local variable and initializes it
	 * using the {@link List}'s current element identified by the loop index.
	 * 
	 * @param rewrite the current {@link ASTRewrite} instance
	 * @param loopVariableName the name of the index variable in String representation
	 * @return a completed {@link Assignment} containing the mentioned declaration and
	 *         initialization
	 */
private Expression getIndexBasedForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
    AST ast = rewrite.getAST();
    ITypeBinding loopOverType = extractElementType(ast);
    Assignment assignResolvedVariable = ast.newAssignment();
    // left hand side
    SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
    VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment();
    resolvedVariableDeclarationFragment.setName(resolvedVariableName);
    VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
    resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
    assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
    // right hand side
    MethodInvocation invokeGetExpression = ast.newMethodInvocation();
    //$NON-NLS-1$
    invokeGetExpression.setName(ast.newSimpleName("get"));
    SimpleName indexVariableName = ast.newSimpleName(loopVariableName.getIdentifier());
    addLinkedPosition(rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier());
    invokeGetExpression.arguments().add(indexVariableName);
    invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
    assignResolvedVariable.setRightHandSide(invokeGetExpression);
    assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
    return assignResolvedVariable;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 3 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class NewAnnotationMemberProposal method getNewType.

private Type getNewType(ASTRewrite rewrite) {
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding binding = null;
    if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
        Expression value = ((MemberValuePair) fInvocationNode.getParent()).getValue();
        binding = value.resolveTypeBinding();
    } else if (fInvocationNode instanceof Expression) {
        binding = ((Expression) fInvocationNode).resolveTypeBinding();
    }
    if (binding != null) {
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
        newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
    }
    if (newTypeNode == null) {
        //$NON-NLS-1$
        newTypeNode = ast.newSimpleType(ast.newSimpleName("String"));
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 4 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class NewMethodCorrectionProposal method getNewMethodType.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
	 */
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
    ASTNode node = getInvocationNode();
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding[] otherProposals = null;
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
    if (node.getParent() instanceof MethodInvocation) {
        MethodInvocation parent = (MethodInvocation) node.getParent();
        if (parent.getExpression() == node) {
            ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
            if (bindings.length > 0) {
                newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
                otherProposals = bindings;
            }
        }
    }
    if (newTypeNode == null) {
        ITypeBinding binding = ASTResolving.guessBindingForReference(node);
        if (binding != null && binding.isWildcardType()) {
            binding = ASTResolving.normalizeWildcardType(binding, false, ast);
        }
        if (binding != null) {
            newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
        } else {
            ASTNode parent = node.getParent();
            if (parent instanceof ExpressionStatement) {
                newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
            } else {
                newTypeNode = ASTResolving.guessTypeForReference(ast, node);
                if (newTypeNode == null) {
                    //$NON-NLS-1$
                    newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
                }
            }
        }
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    if (otherProposals != null) {
        for (int i = 0; i < otherProposals.length; i++) {
            addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
        }
    }
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 5 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class NewMethodCorrectionProposal method addNewParameters.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
	 */
@Override
protected void addNewParameters(ASTRewrite rewrite, List<String> takenNames, List<SingleVariableDeclaration> params) throws CoreException {
    AST ast = rewrite.getAST();
    List<Expression> arguments = fArguments;
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(getInvocationNode()), getImportRewrite());
    for (int i = 0; i < arguments.size(); i++) {
        Expression elem = arguments.get(i);
        SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
        // argument type
        //$NON-NLS-1$
        String argTypeKey = "arg_type_" + i;
        Type type = evaluateParameterType(ast, elem, argTypeKey, context);
        param.setType(type);
        // argument name
        //$NON-NLS-1$
        String argNameKey = "arg_name_" + i;
        String name = evaluateParameterName(takenNames, elem, type, argNameKey);
        param.setName(ast.newSimpleName(name));
        params.add(param);
        addLinkedPosition(rewrite.track(param.getType()), false, argTypeKey);
        addLinkedPosition(rewrite.track(param.getName()), false, argNameKey);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Aggregations

ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)57 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)47 AST (org.eclipse.jdt.core.dom.AST)33 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)32 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)30 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)21 Type (org.eclipse.jdt.core.dom.Type)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)18 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)17 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)15 Image (org.eclipse.swt.graphics.Image)14 Expression (org.eclipse.jdt.core.dom.Expression)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8