Search in sources :

Example 11 with SingleMemberAnnotation

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

the class SuppressWarningsQuickFix method createReplacementSuppressWarningsAnnotation.

private Annotation createReplacementSuppressWarningsAnnotation(final Annotation existingAnnotation, final AST ast) {
    final Annotation replacement;
    if (existingAnnotation == null || existingAnnotation.isMarkerAnnotation()) {
        final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
        annotation.setValue(createPMDLiteralValue(ast));
        replacement = annotation;
    } else if (existingAnnotation.isSingleMemberAnnotation()) {
        final SingleMemberAnnotation existingSingleMemberAnnotation = (SingleMemberAnnotation) existingAnnotation;
        final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
        annotation.setValue(createArrayInitializer(existingSingleMemberAnnotation.getValue()));
        replacement = annotation;
    } else if (existingAnnotation.isNormalAnnotation()) {
        final NormalAnnotation existingNormalAnnotation = (NormalAnnotation) existingAnnotation;
        final NormalAnnotation annotation = createAnnotation(ast, NormalAnnotation.class);
        createAnnotationValues(existingNormalAnnotation, annotation);
        replacement = annotation;
    } else {
        replacement = existingAnnotation;
    }
    return replacement;
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation)

Example 12 with SingleMemberAnnotation

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

the class ASTNodeFactory method newSingleMemberAnnotation.

/**
 * Builds a new {@link SingleMemberAnnotation} instance.
 *
 * @param typeName the annotation type name
 * @param value    the annotation single value
 * @return a new single member annotation
 */
public SingleMemberAnnotation newSingleMemberAnnotation(final Name typeName, final Expression value) {
    SingleMemberAnnotation sma = ast.newSingleMemberAnnotation();
    sma.setTypeName(typeName);
    sma.setValue(value);
    return sma;
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation)

Example 13 with SingleMemberAnnotation

use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project sts4 by spring-projects.

the class RequestMappingSymbolProvider method getMethod.

private String[] getMethod(Annotation node) {
    String[] methods = null;
    if (node.isNormalAnnotation()) {
        NormalAnnotation normNode = (NormalAnnotation) node;
        List<?> values = normNode.values();
        for (Iterator<?> iterator = values.iterator(); iterator.hasNext(); ) {
            Object object = iterator.next();
            if (object instanceof MemberValuePair) {
                MemberValuePair pair = (MemberValuePair) object;
                String valueName = pair.getName().getIdentifier();
                if (valueName != null && valueName.equals("method")) {
                    Expression expression = pair.getValue();
                    methods = ASTUtils.getExpressionValueAsArray(expression);
                    break;
                }
            }
        }
    } else if (node instanceof SingleMemberAnnotation) {
        methods = getRequestMethod((SingleMemberAnnotation) node);
    }
    if (methods == null && node.getParent() instanceof MethodDeclaration) {
        Annotation parentAnnotation = getParentAnnotation(node);
        if (parentAnnotation != null) {
            methods = getMethod(parentAnnotation);
        }
    }
    return methods;
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.eclipse.jdt.core.dom.Expression) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation)

Example 14 with SingleMemberAnnotation

use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project sts4 by spring-projects.

the class RequestMappingSymbolProvider method getPath.

private String[] getPath(Annotation node) {
    if (node.isNormalAnnotation()) {
        NormalAnnotation normNode = (NormalAnnotation) node;
        List<?> values = normNode.values();
        for (Iterator<?> iterator = values.iterator(); iterator.hasNext(); ) {
            Object object = iterator.next();
            if (object instanceof MemberValuePair) {
                MemberValuePair pair = (MemberValuePair) object;
                String valueName = pair.getName().getIdentifier();
                if (valueName != null && (valueName.equals("value") || valueName.equals("path"))) {
                    Expression expression = pair.getValue();
                    return ASTUtils.getExpressionValueAsArray(expression);
                }
            }
        }
    } else if (node.isSingleMemberAnnotation()) {
        SingleMemberAnnotation singleNode = (SingleMemberAnnotation) node;
        Expression expression = singleNode.getValue();
        return ASTUtils.getExpressionValueAsArray(expression);
    }
    return new String[] { "" };
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.eclipse.jdt.core.dom.Expression) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation)

Example 15 with SingleMemberAnnotation

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

the class TypeMismatchSubProcessor method addTypeMismatchProposals.

public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    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 (currBinding == null && nodeToCast instanceof MethodInvocation) {
        IMethodBinding methodBinding = ((MethodInvocation) nodeToCast).resolveMethodBinding();
        if (methodBinding != null) {
            currBinding = methodBinding.getReturnType();
        }
    }
    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()));
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE);
            ImportRewrite imports = proposal.createImportRewrite(astRoot);
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
            Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
            rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
            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;
        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, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) Assignment(org.eclipse.jdt.core.dom.Assignment) 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) 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)

Aggregations

SingleMemberAnnotation (org.eclipse.jdt.core.dom.SingleMemberAnnotation)15 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 Expression (org.eclipse.jdt.core.dom.Expression)6 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 AST (org.eclipse.jdt.core.dom.AST)4 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)4 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Name (org.eclipse.jdt.core.dom.Name)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 Type (org.eclipse.jdt.core.dom.Type)4 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Annotation (org.eclipse.jdt.core.dom.Annotation)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)3