Search in sources :

Example 16 with ArrayInitializer

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

the class CodeAnalyzer method checkSelectedNodes.

@Override
protected final void checkSelectedNodes() {
    super.checkSelectedNodes();
    RefactoringStatus status = getStatus();
    if (status.hasFatalError())
        return;
    ASTNode node = getFirstSelectedNode();
    if (node instanceof ArrayInitializer) {
        status.addFatalError(RefactoringCoreMessages.CodeAnalyzer_array_initializer, JavaStatusContext.create(fCUnit, node));
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 17 with ArrayInitializer

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

the class TypeMismatchSubProcessor method addTypeMismatchProposals.

public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    String[] args = problem.getProblemArguments();
    if (args.length != 2) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    AST ast = astRoot.getAST();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (!(selectedNode instanceof Expression)) {
        return;
    }
    Expression nodeToCast = (Expression) selectedNode;
    Name receiverNode = null;
    ITypeBinding castTypeBinding = null;
    int parentNodeType = selectedNode.getParent().getNodeType();
    if (parentNodeType == ASTNode.ASSIGNMENT) {
        Assignment assign = (Assignment) selectedNode.getParent();
        Expression leftHandSide = assign.getLeftHandSide();
        if (selectedNode.equals(leftHandSide)) {
            nodeToCast = assign.getRightHandSide();
        }
        castTypeBinding = assign.getLeftHandSide().resolveTypeBinding();
        if (leftHandSide instanceof Name) {
            receiverNode = (Name) leftHandSide;
        } else if (leftHandSide instanceof FieldAccess) {
            receiverNode = ((FieldAccess) leftHandSide).getName();
        }
    } else if (parentNodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) selectedNode.getParent();
        if (selectedNode.equals(frag.getName()) || selectedNode.equals(frag.getInitializer())) {
            nodeToCast = frag.getInitializer();
            castTypeBinding = ASTNodes.getType(frag).resolveBinding();
            receiverNode = frag.getName();
        }
    } else if (parentNodeType == ASTNode.MEMBER_VALUE_PAIR) {
        receiverNode = ((MemberValuePair) selectedNode.getParent()).getName();
        castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
    } else if (parentNodeType == ASTNode.SINGLE_MEMBER_ANNOTATION) {
        // use the type name
        receiverNode = ((SingleMemberAnnotation) selectedNode.getParent()).getTypeName();
        castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
    } else {
        // try to find the binding corresponding to 'castTypeName'
        castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
    }
    if (castTypeBinding == null) {
        return;
    }
    ITypeBinding currBinding = nodeToCast.resolveTypeBinding();
    if (!(nodeToCast instanceof ArrayInitializer)) {
        ITypeBinding castFixType = null;
        if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) {
            castFixType = castTypeBinding;
        } else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
            ITypeBinding boxUnboxedTypeBinding = boxUnboxPrimitives(castTypeBinding, currBinding, ast);
            if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) {
                castFixType = boxUnboxedTypeBinding;
            }
        }
        if (castFixType != null) {
            proposals.add(createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CREATE_CAST));
        }
    }
    //$NON-NLS-1$
    boolean nullOrVoid = currBinding == null || "void".equals(currBinding.getName());
    // change method return statement to actual type
    if (!nullOrVoid && parentNodeType == ASTNode.RETURN_STATEMENT) {
        BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
        if (decl instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
            currBinding = Bindings.normalizeTypeBinding(currBinding);
            if (currBinding == null) {
                //$NON-NLS-1$
                currBinding = ast.resolveWellKnownType("java.lang.Object");
            }
            if (currBinding.isWildcardType()) {
                currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
            }
            ASTRewrite rewrite = ASTRewrite.create(ast);
            String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturntype_description, BasicElementLabels.getJavaElementName(currBinding.getName()));
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE, image);
            ImportRewrite imports = proposal.createImportRewrite(astRoot);
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
            Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext);
            rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
            //$NON-NLS-1$
            String returnKey = "return";
            proposal.addLinkedPosition(rewrite.track(newReturnType), true, returnKey);
            ITypeBinding[] typeSuggestions = ASTResolving.getRelaxingTypes(ast, currBinding);
            for (int i = 0; i < typeSuggestions.length; i++) {
                proposal.addLinkedPositionProposal(returnKey, typeSuggestions[i]);
            }
            proposals.add(proposal);
        }
    }
    if (!nullOrVoid && receiverNode != null) {
        currBinding = Bindings.normalizeTypeBinding(currBinding);
        if (currBinding == null) {
            //$NON-NLS-1$
            currBinding = ast.resolveWellKnownType("java.lang.Object");
        }
        if (currBinding.isWildcardType()) {
            currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
        }
        addChangeSenderTypeProposals(context, receiverNode, currBinding, true, IProposalRelevance.CHANGE_TYPE_OF_RECEIVER_NODE, proposals);
    }
    addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, IProposalRelevance.CHANGE_TYPE_OF_NODE_TO_CAST, proposals);
    if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) {
        //$NON-NLS-1$
        String label = CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
        InfixExpression expression = ast.newInfixExpression();
        expression.setLeftOperand((Expression) rewrite.createMoveTarget(nodeToCast));
        expression.setRightOperand(ast.newNullLiteral());
        expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
        rewrite.replace(nodeToCast, expression, null);
        proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK, image));
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) 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) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 18 with ArrayInitializer

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

the class SuppressWarningsSubProcessor method addRemoveUnusedSuppressWarningProposals.

public static void addRemoveUnusedSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
    if (!(coveringNode instanceof StringLiteral))
        return;
    StringLiteral literal = (StringLiteral) coveringNode;
    if (coveringNode.getParent() instanceof MemberValuePair) {
        coveringNode = coveringNode.getParent();
    }
    ASTNode parent = coveringNode.getParent();
    ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
    if (parent instanceof SingleMemberAnnotation) {
        rewrite.remove(parent, null);
    } else if (parent instanceof NormalAnnotation) {
        NormalAnnotation annot = (NormalAnnotation) parent;
        if (annot.values().size() == 1) {
            rewrite.remove(annot, null);
        } else {
            rewrite.remove(coveringNode, null);
        }
    } else if (parent instanceof ArrayInitializer) {
        rewrite.remove(coveringNode, null);
    } else {
        return;
    }
    String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_remove_annotation_label, literal.getLiteralValue());
    //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Image(org.eclipse.swt.graphics.Image) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 19 with ArrayInitializer

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

the class AnnotationRefactoring method visit.

@Override
public boolean visit(NormalAnnotation node) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    final List<MemberValuePair> values = values(node);
    if (values.isEmpty()) {
        r.replace(node, b.markerAnnotation(b.move(node.getTypeName())));
        return DO_NOT_VISIT_SUBTREE;
    } else if (values.size() == 1) {
        MemberValuePair pair = values.get(0);
        if ("value".equals(pair.getName().getIdentifier())) {
            r.replace(node, b.singleValueAnnotation(b.move(node.getTypeName()), b.move(pair.getValue())));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    boolean result = VISIT_SUBTREE;
    Map<String, IMethodBinding> elements = toElementsMap(node.resolveAnnotationBinding());
    for (MemberValuePair pair : values) {
        IMethodBinding elementBinding = elements.get(pair.getName().getIdentifier());
        if (equal(elementBinding.getReturnType(), pair.getValue(), elementBinding.getDefaultValue())) {
            r.remove(pair);
            result = DO_NOT_VISIT_SUBTREE;
        } else if (pair.getValue().getNodeType() == ARRAY_INITIALIZER) {
            ArrayInitializer arrayInit = (ArrayInitializer) pair.getValue();
            List<Expression> exprs = expressions(arrayInit);
            if (exprs.size() == 1) {
                r.replace(arrayInit, b.move(exprs.get(0)));
                result = DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Refactorings(org.autorefactor.refactoring.Refactorings) List(java.util.List) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 20 with ArrayInitializer

use of org.eclipse.jdt.core.dom.ArrayInitializer in project eclipse-pmd by acanda.

the class SuppressWarningsQuickFix method createArrayInitializer.

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializer(final Expression value) {
    final AST ast = value.getAST();
    final ArrayInitializer array;
    if (value instanceof ArrayInitializer) {
        array = createArrayInitializerAndCopyExpressions(ast, (ArrayInitializer) value);
    } else {
        array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
        array.expressions().add(ASTUtil.copy(value));
    }
    array.expressions().add(createPMDLiteralValue(ast));
    return array;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)27 Expression (org.eclipse.jdt.core.dom.Expression)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)10 AST (org.eclipse.jdt.core.dom.AST)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 Assignment (org.eclipse.jdt.core.dom.Assignment)7 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 Type (org.eclipse.jdt.core.dom.Type)6 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 List (java.util.List)4 ArrayType (org.eclipse.jdt.core.dom.ArrayType)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4