Search in sources :

Example 6 with MemberValuePair

use of org.eclipse.jdt.core.dom.MemberValuePair 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)

Aggregations

MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)3 Expression (org.eclipse.jdt.core.dom.Expression)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 Type (org.eclipse.jdt.core.dom.Type)3 AST (org.eclipse.jdt.core.dom.AST)2 Annotation (org.eclipse.jdt.core.dom.Annotation)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)2 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)2 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)2 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)2 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)2 SimpleType (org.eclipse.jdt.core.dom.SimpleType)2