Example usage for org.eclipse.jdt.core.dom SingleMemberAnnotation getValue

List of usage examples for org.eclipse.jdt.core.dom SingleMemberAnnotation getValue

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom SingleMemberAnnotation getValue.

Prototype

public Expression getValue() 

Source Link

Document

Returns the value of this annotation.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(SingleMemberAnnotation node) {
    this.fBuffer.append("@");//$NON-NLS-1$
    node.getTypeName().accept(this);
    this.fBuffer.append("(");//$NON-NLS-1$
    node.getValue().accept(this);
    this.fBuffer.append(")");//$NON-NLS-1$
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(SingleMemberAnnotation node) {
    boa.types.Ast.Modifier.Builder b = getAnnotationBuilder(node);
    node.getValue().accept(this);
    if (expressions.empty()) {
        boa.types.Ast.Expression.Builder eb = boa.types.Ast.Expression.newBuilder();
        //         eb.setPosition(pos.build()); // FIXME
        eb.setKind(boa.types.Ast.Expression.ExpressionKind.ANNOTATION);
        eb.setAnnotation(modifiers.pop());
        b.addAnnotationMembers("value");
        b.addAnnotationValues(eb.build());
    } else {//from w ww . j  a  va2s .  co  m
        b.addAnnotationMembers("value");
        b.addAnnotationValues(expressions.pop());
    }
    modifiers.push(b.build());
    return false;
}

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

private void reportProblem(Annotation annotation, String member, int valueIndex,
        Collection<DSAnnotationProblem> problems, String message, String... args) {
    if (errorLevel.isNone())
        return;//from w ww  . j a  va2  s  .c om

    Expression memberValue = annotation;
    if (annotation.isNormalAnnotation() && member != null) {
        NormalAnnotation na = (NormalAnnotation) annotation;
        for (Object value : na.values()) {
            MemberValuePair pair = (MemberValuePair) value;
            if (member.equals(pair.getName().getIdentifier())) {
                memberValue = pair.getValue();
                break;
            }
        }
    } else if (annotation.isSingleMemberAnnotation()) {
        SingleMemberAnnotation sma = (SingleMemberAnnotation) annotation;
        memberValue = sma.getValue();
    }

    int start = memberValue.getStartPosition();
    int length = memberValue.getLength();

    if (valueIndex >= 0 && memberValue instanceof ArrayInitializer) {
        ArrayInitializer ai = (ArrayInitializer) memberValue;
        if (valueIndex < ai.expressions().size()) {
            Expression element = (Expression) ai.expressions().get(valueIndex);
            start = element.getStartPosition();
            length = element.getLength();
        }
    }

    if (start >= 0) {
        DSAnnotationProblem problem = new DSAnnotationProblem(errorLevel.isError(), message, args);
        problem.setSourceStart(start);
        problem.setSourceEnd(start + length - 1);
        problems.add(problem);
    }
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

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;//from w ww . j  a va  2  s .c om

    } 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;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(SingleMemberAnnotation node) {
    this.buffer.append("@");//$NON-NLS-1$
    node.getTypeName().accept(this);
    this.buffer.append("(");//$NON-NLS-1$
    node.getValue().accept(this);
    this.buffer.append(")");//$NON-NLS-1$
    return false;
}

From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
        BodyDeclaration declaration) throws CoreException {
    List modifiers = declaration.modifiers();
    SingleMemberAnnotation existing = null;
    for (Object o : modifiers) {
        if (o instanceof SingleMemberAnnotation) {
            SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
            String type = annotation.getTypeName().getFullyQualifiedName();
            if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
                existing = annotation;/*from   ww w.j av  a 2s . c  om*/
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
    AST ast = declaration.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    if (existing == null) {
        SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
        newAnnotation.setTypeName(ast.newSimpleName(local));
        StringLiteral value = ast.newStringLiteral();
        value.setLiteralValue(mId);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression existingValue = existing.getValue();
        if (existingValue instanceof StringLiteral) {
            StringLiteral stringLiteral = (StringLiteral) existingValue;
            if (mId.equals(stringLiteral.getLiteralValue())) {
                // Already contains the id
                return null;
            }
            // Create a new array initializer holding the old string plus the new id
            ArrayInitializer array = ast.newArrayInitializer();
            StringLiteral old = ast.newStringLiteral();
            old.setLiteralValue(stringLiteral.getLiteralValue());
            array.expressions().add(old);
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            array.expressions().add(value);
            rewriter.set(existing, VALUE_PROPERTY, array, null);
        } else if (existingValue instanceof ArrayInitializer) {
            // Existing array: just append the new string
            ArrayInitializer array = (ArrayInitializer) existingValue;
            List expressions = array.expressions();
            if (expressions != null) {
                for (Object o : expressions) {
                    if (o instanceof StringLiteral) {
                        if (mId.equals(((StringLiteral) o).getLiteralValue())) {
                            // Already contains the id
                            return null;
                        }
                    }
                }
            }
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
            listRewrite.insertLast(value, null);
        } else {
            assert false : existingValue;
            return null;
        }
    }

    TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
    TextEdit annotationEdits = rewriter.rewriteAST(document, null);

    // Apply to the document
    MultiTextEdit edit = new MultiTextEdit();
    // Create the edit to change the imports, only if
    // anything changed
    if (importEdits.hasChildren()) {
        edit.addChild(importEdits);
    }
    edit.addChild(annotationEdits);

    return edit;
}

From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
        BodyDeclaration declaration) throws CoreException {
    List modifiers = declaration.modifiers();
    SingleMemberAnnotation existing = null;
    for (Object o : modifiers) {
        if (o instanceof SingleMemberAnnotation) {
            SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
            String type = annotation.getTypeName().getFullyQualifiedName();
            if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
                existing = annotation;/*from  w  w w. j  a v  a  2  s.c o m*/
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
    AST ast = declaration.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    if (existing == null) {
        SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
        newAnnotation.setTypeName(ast.newSimpleName(local));
        StringLiteral value = ast.newStringLiteral();
        value.setLiteralValue(mId);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression existingValue = existing.getValue();
        if (existingValue instanceof StringLiteral) {
            // Create a new array initializer holding the old string plus the new id
            ArrayInitializer array = ast.newArrayInitializer();
            StringLiteral old = ast.newStringLiteral();
            StringLiteral stringLiteral = (StringLiteral) existingValue;
            old.setLiteralValue(stringLiteral.getLiteralValue());
            array.expressions().add(old);
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            array.expressions().add(value);
            rewriter.set(existing, VALUE_PROPERTY, array, null);
        } else if (existingValue instanceof ArrayInitializer) {
            // Existing array: just append the new string
            ArrayInitializer array = (ArrayInitializer) existingValue;
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
            listRewrite.insertLast(value, null);
        } else {
            assert false : existingValue;
            return null;
        }
    }

    TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
    TextEdit annotationEdits = rewriter.rewriteAST(document, null);

    // Apply to the document
    MultiTextEdit edit = new MultiTextEdit();
    // Create the edit to change the imports, only if
    // anything changed
    if (importEdits.hasChildren()) {
        edit.addChild(importEdits);
    }
    edit.addChild(annotationEdits);

    return edit;
}

From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addTargetApiAnnotation(IDocument document, ICompilationUnit compilationUnit,
        BodyDeclaration declaration) throws CoreException {
    List modifiers = declaration.modifiers();
    SingleMemberAnnotation existing = null;
    for (Object o : modifiers) {
        if (o instanceof SingleMemberAnnotation) {
            SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
            String type = annotation.getTypeName().getFullyQualifiedName();
            if (type.equals(FQCN_TARGET_API) || type.endsWith(TARGET_API)) {
                existing = annotation;//from  w w  w . java2s  . c  o m
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    String local = importRewrite.addImport(FQCN_TARGET_API);
    AST ast = declaration.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    if (existing == null) {
        SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
        newAnnotation.setTypeName(ast.newSimpleName(local));
        NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi));
        //value.setLiteralValue(mId);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression existingValue = existing.getValue();
        if (existingValue instanceof NumberLiteral) {
            // Change the value to the new value
            NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi));
            rewriter.set(existing, VALUE_PROPERTY, value, null);
        } else {
            assert false : existingValue;
            return null;
        }
    }

    TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
    TextEdit annotationEdits = rewriter.rewriteAST(document, null);
    MultiTextEdit edit = new MultiTextEdit();
    if (importEdits.hasChildren()) {
        edit.addChild(importEdits);
    }
    edit.addChild(annotationEdits);

    return edit;
}

From source file:com.google.devtools.j2objc.ast.DebugASTPrinter.java

License:Apache License

@Override
public boolean visit(SingleMemberAnnotation node) {
    sb.print("@");
    node.getTypeName().accept(this);
    sb.print("(");
    node.getValue().accept(this);
    sb.print(")");
    return false;
}

From source file:com.google.gdt.eclipse.core.JavaASTUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static boolean hasSuppressWarnings(BodyDeclaration decl, String warningType) {
    List<IExtendedModifier> modifiers = decl.modifiers();
    for (IExtendedModifier modifier : modifiers) {
        if (modifier.isAnnotation()) {
            assert (modifier instanceof Annotation);
            Annotation annotation = (Annotation) modifier;

            // Get the (simple) type name of the annotation (we can't resolve the
            // annotation's type if binding resolution is disabled on our AST).
            Name annotationType = annotation.getTypeName();
            if (annotationType.isQualifiedName()) {
                annotationType = ((QualifiedName) annotationType).getName();
            }/*from  w  ww .j a  va2 s  .c  o m*/
            assert (annotationType.isSimpleName());
            String annotationTypeName = ((SimpleName) annotationType).getIdentifier();

            // Look for @SuppressWarnings annotations
            if (annotationTypeName.equals(SuppressWarnings.class.getSimpleName())) {
                // Now extract the parameter representing the set of warnings that
                // should be suppressed
                if (annotation instanceof SingleMemberAnnotation) {
                    SingleMemberAnnotation suppressWarnings = (SingleMemberAnnotation) annotation;
                    Expression annotationValue = suppressWarnings.getValue();
                    return containsAnnotationValue(annotationValue, warningType);
                } else if (annotation instanceof NormalAnnotation) {
                    NormalAnnotation suppressWarnings = (NormalAnnotation) annotation;
                    List<MemberValuePair> annotationValues = suppressWarnings.values();
                    for (MemberValuePair annotationValue : annotationValues) {
                        SimpleName annotationValueName = annotationValue.getName();
                        if (annotationValueName.getIdentifier().equals("value")) {
                            return containsAnnotationValue(annotationValue.getValue(), warningType);
                        }
                    }
                }

                return false;
            }
        }
    }

    return false;
}