Example usage for org.eclipse.jdt.core.dom BodyDeclaration getModifiersProperty

List of usage examples for org.eclipse.jdt.core.dom BodyDeclaration getModifiersProperty

Introduction

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

Prototype

public final ChildListPropertyDescriptor getModifiersProperty() 

Source Link

Document

Returns structural property descriptor for the "modifiers" property of this node as used in JLS3 (element type: IExtendedModifier ).

Usage

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 2 s  .  co 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) {
            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.adt.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  ww .j  a  v a2  s . co m*/
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    importRewrite.addImport("android.os.Build"); //$NON-NLS-1$
    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));
        Expression value = createLiteral(ast);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression value = createLiteral(ast);
        rewriter.set(existing, VALUE_PROPERTY, value, 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.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;//  w w w  .j ava2  s .  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) {
            // 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;//  w w w  .j a  v a  2 s  .co  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.gdt.eclipse.core.JavaASTUtils.java

License:Open Source License

/**
 * Finds an annotation of the given type (as a fully-qualified name) on a
 * declaration (type, method, field, etc.). If no such annotation exists,
 * returns <code>null</code>.
 *///from www .  j  av a 2s.  c  o m
@SuppressWarnings("unchecked")
public static Annotation findAnnotation(BodyDeclaration decl, String annotationTypeName) {
    if (annotationTypeName == null) {
        throw new IllegalArgumentException("annotationTypeName cannot be null");
    }

    List<ASTNode> modifiers = (List<ASTNode>) decl.getStructuralProperty(decl.getModifiersProperty());
    for (ASTNode modifier : modifiers) {
        if (modifier instanceof Annotation) {
            Annotation annotation = (Annotation) modifier;
            String typeName = getAnnotationTypeName(annotation);
            if (annotationTypeName.equals(typeName)) {
                return annotation;
            }
        }
    }
    return null;
}

From source file:com.halware.nakedide.eclipse.ext.annot.ast.AbstractAnnotationEvaluatorOrModifier.java

License:Open Source License

private void addNormalAnnotation(ICompilationUnit compilationUnit, BodyDeclaration declaration,
        Map<String, Object> memberValues) throws MalformedTreeException, BadLocationException, CoreException {

    if (annotationExists(declaration)) {
        return;/*ww  w  .j a  v  a  2s.  com*/
    }

    AST ast = declaration.getAST();

    NormalAnnotation normalAnnotation = AstUtils.createNormalAnnotation(ast, getAnnotationFullyQualifiedName(),
            memberValues);
    normalAnnotation.setSourceRange(AstUtils.calculateOffset(declaration, true).offset,
            normalAnnotation.getLength());

    ImportDeclaration importDeclaration = AstUtils.createImportStatement(ast,
            getAnnotationFullyQualifiedName());

    String source = compilationUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);

    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());

    ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();

    ListRewrite listRewrite = rewrite.getListRewrite(declaration, modifiersProperty);
    listRewrite.insertFirst(normalAnnotation, null);

    maintainValuesProperty(normalAnnotation, memberValues, ast, rewrite);

    TextEdit importEdits = null;
    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    importRewrite.addImport(importDeclaration.getName().getFullyQualifiedName());
    importEdits = importRewrite.rewriteImports(null);

    Map options = compilationUnit.getJavaProject().getOptions(true);
    TextEdit edits = rewrite.rewriteAST(document, options);

    // computation of the new source code
    edits.apply(document);
    if (importEdits != null) {
        importEdits.apply(document);
    }
    String newSource = document.get();

    // update of the compilation unit
    compilationUnit.getBuffer().setContents(newSource);
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java

License:Open Source License

/**
 * Returns the offset and length for the {@link MethodDeclaration}, optionally
 * including any modifiers (including annotations and visibility).
 * //w  ww. j  a va2 s .  c  o  m
 * @param declaration
 * @param includingModifiers - whether to include modifiers in the offset and length.
 */
public final static OffsetAndLength calculateOffset(BodyDeclaration declaration, boolean includingModifiers) {
    if (!(declaration instanceof MethodDeclaration) && !(declaration instanceof TypeDeclaration)) {
        throw new IllegalArgumentException("declaration must be TypeDeclaration or MethodDeclaration");
    }
    SimpleName methodName = nameFor(declaration);
    if (!includingModifiers) {
        return new OffsetAndLength(methodName.getStartPosition(), methodName.getLength());
    } else {
        // including modifiers
        int endOfMethodName = methodName.getStartPosition() + methodName.getLength();
        ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();
        List<ASTNode> methodModifiers = Generics.asT(declaration.getStructuralProperty(modifiersProperty));
        if (methodModifiers.size() > 0) {
            int startOfModifiers = methodModifiers.get(0).getStartPosition();
            return new OffsetAndLength(startOfModifiers, endOfMethodName - startOfModifiers);
        }
        if (declaration instanceof MethodDeclaration) {
            Type returnType2 = ((MethodDeclaration) declaration).getReturnType2();
            if (returnType2 != null) {
                int startOfReturnType = returnType2.getStartPosition();
                return new OffsetAndLength(startOfReturnType, endOfMethodName - startOfReturnType);
            }
        }
        int startOfMethodName = declaration.getStartPosition();
        return new OffsetAndLength(startOfMethodName, endOfMethodName - startOfMethodName);
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java

License:Open Source License

public static void rewriteAddFirst(ICompilationUnit compilationUnit, BodyDeclaration declaration,
        Annotation annotation, ImportDeclaration importDeclaration)
        throws MalformedTreeException, BadLocationException, CoreException {

    String source = compilationUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);/*from  ww  w.  j  a v a  2s.  c o m*/

    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());

    ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();

    ListRewrite listRewrite = rewrite.getListRewrite(declaration, modifiersProperty);
    listRewrite.insertFirst(annotation, null);

    TextEdit importEdits = null;
    if (importDeclaration != null) {
        ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
        importRewrite.addImport(importDeclaration.getName().getFullyQualifiedName());
        importEdits = importRewrite.rewriteImports(null);
    }

    Map options = compilationUnit.getJavaProject().getOptions(true);
    TextEdit edits = rewrite.rewriteAST(document, options);

    // computation of the new source code
    edits.apply(document);
    if (importEdits != null) {
        importEdits.apply(document);
    }
    String newSource = document.get();

    // update of the compilation unit
    compilationUnit.getBuffer().setContents(newSource);
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <T extends Annotation> T addAnnotation(BodyDeclaration bodyDeclaration, T annotation) {
    List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
    list.add(annotationLocation(bodyDeclaration), annotation);
    return annotation;
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static void removeAnnotation(BodyDeclaration bodyDeclaration, Annotation annotation) {
    List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
    list.remove(annotation);/*  w w w.  j a v a2 s .  c o m*/
}