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

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

Introduction

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

Prototype

ASTNode.NodeList modifiers

To view the source code for org.eclipse.jdt.core.dom BodyDeclaration modifiers.

Click Source Link

Document

The extended modifiers (element type: IExtendedModifier ).

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginLine(BodyDeclaration node) {
    int beginLine = INVALID_LINE;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            beginLine = cu.getLineNumber(m.getStartPosition());
        }//from w  w  w .  j a va  2  s .co  m
    }

    if (beginLine == INVALID_LINE) {

        beginLine = cu.getLineNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getLineNumber(javadoc.getStartPosition());
        }

        if (beginLine == javadocBegin) {
            beginLine = cu.getLineNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }
    return beginLine;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginColunm(BodyDeclaration node) {

    int begincolumn = INVALID_COLUMN;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            begincolumn = cu.getColumnNumber(m.getStartPosition());
        }//from  w  w w. j a  v a 2s . com
    }

    if (begincolumn == INVALID_COLUMN) {

        begincolumn = cu.getColumnNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getColumnNumber(javadoc.getStartPosition());
        }

        if (begincolumn == javadocBegin) {
            begincolumn = cu.getColumnNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }

    return begincolumn;
}

From source file:changenodes.matching.BestLeafTreeMatcher.java

License:Apache License

private void markBodyDeclaration(ASTNode x, ASTNode y) {
    BodyDeclaration leftMethod = (BodyDeclaration) x;
    BodyDeclaration rightMethod = (BodyDeclaration) y;
    List<IExtendedModifier> leftModifiers = (List<IExtendedModifier>) leftMethod.modifiers();
    List<IExtendedModifier> rightModifiers = (List<IExtendedModifier>) rightMethod.modifiers();
    for (IExtendedModifier lm : leftModifiers) {
        if (lm.isModifier()) {
            for (IExtendedModifier rm : rightModifiers) {
                if (rm.isModifier()) {
                    Modifier clm = (Modifier) lm;
                    Modifier crm = (Modifier) rm;
                    if (crm.getKeyword().equals(clm.getKeyword())) {
                        leftMatching.put(clm, crm);
                        rightMatching.put(crm, clm);
                    }/*from   w  ww . j  a  va  2s . c o m*/
                }
            }
        }
    }
}

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;//w w  w  .j  a  va2  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) {
            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  ww  w.j a va 2  s .c  om*/
                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 a v  a2s  .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 ww. j a  va2s .  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.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * @flowerModelElementId _zVs8XZiOEd6aNMdNFvR5WQ
 *//* w w w .ja  va 2s.  c om*/
@SuppressWarnings("unchecked")
public static void updateVisibilityFromModelToJavaClass(BodyDeclaration bd, VisibilityKind visibilityKind) {// NamedElement
    // modelElement) {
    ModifierKeyword javaVisibFlag = getJavaVisibilityFlagFromJavaClass(bd);
    ModifierKeyword modelVisibFlag = convertModelToJavaVisibilityFlag(visibilityKind);

    if (modelVisibFlag != javaVisibFlag) {
        if (javaVisibFlag != null) { // /meaning the package visibility
            // Which to remove?
            Modifier toRemove = null;
            for (Iterator<Modifier> it = bd.modifiers().iterator(); it.hasNext();) {
                Modifier modif = it.next();
                if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                    toRemove = modif;
                    break;
                }
            }
            bd.modifiers().remove(toRemove);
        }
        // Adding
        if (modelVisibFlag != null) {
            Modifier newModif = bd.getAST().newModifier(modelVisibFlag);
            bd.modifiers().add(newModif);
        }
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * Updates java code according to the model value for abstract or static modifiers
 * /*from  w  w  w .jav  a 2 s. c o  m*/
 * @author Luiza
 * @param bd <code>{@link BodyDeclaration}</code> for a class, method or field.
 * @param isTrue value for the modifier
 * @param modifier flag indicating the modifier to update.
 * This should be one of the predefined flags:  
 * <ul>
 *    <li>{@link #MODIFIER_ABSTRACT}</li>
 *    <li>{@link #MODIFIER_STATIC}</li>
 * </ul>
 * @flowerModelElementId _zVs8cJiOEd6aNMdNFvR5WQ
 */
@SuppressWarnings("unchecked")
public static void updateModifierFromModelToJavaClass(BodyDeclaration bd, boolean isTrue, String modifier) {

    ModifierKeyword javaModifierFlag = null;
    ModifierKeyword modelModifierFlag = null;
    boolean isForAbstractModif = modifier.equals(MODIFIER_ABSTRACT);
    boolean isForStaticModif = modifier.equals(MODIFIER_STATIC);
    boolean isForFinalModif = modifier.equals(MODIFIER_FINAL);

    if (isForAbstractModif) {
        javaModifierFlag = Modifier.isAbstract(bd.getModifiers()) ? ModifierKeyword.ABSTRACT_KEYWORD : null;
        modelModifierFlag = isTrue ? ModifierKeyword.ABSTRACT_KEYWORD : null;
    } else if (isForStaticModif) {
        javaModifierFlag = Modifier.isStatic(bd.getModifiers()) ? ModifierKeyword.STATIC_KEYWORD : null;
        modelModifierFlag = isTrue ? ModifierKeyword.STATIC_KEYWORD : null;
    } else if (isForFinalModif) {
        javaModifierFlag = Modifier.isFinal(bd.getModifiers()) ? ModifierKeyword.FINAL_KEYWORD : null;
        modelModifierFlag = isTrue ? ModifierKeyword.FINAL_KEYWORD : null;
    } else
        throw new IllegalArgumentException(
                "updateModifierFromModelToJavaClass - can't update java code for modifier " + modifier);

    //if there are differences between the model and the java file
    if (modelModifierFlag != javaModifierFlag) {
        //if this modifier has been set before find it and remove it
        if (javaModifierFlag != null) {
            //Modifier toRemove = null;
            /* 
             * we use bd.modifiers() instead of bd.getModifiers(), which computes a bit-wise integer consisting in all the modifier values 
             * because it also iterates over the modifiers and method bd.setModifiers(modifiers) is deprecated
             */
            for (Iterator<Modifier> it = bd.modifiers().iterator(); it.hasNext();) {
                Modifier modif = it.next();
                if (isForAbstractModif && modif.isAbstract()) {
                    it.remove();
                    break;
                } else if (isForStaticModif && modif.isStatic()) {
                    it.remove();
                    break;
                } else if (isForFinalModif && modif.isFinal()) {
                    it.remove();
                    break;
                }
            }
            //bd.modifiers().remove(toRemove);
        }

        // add the new modifier value if changes have been made in the model
        if (modelModifierFlag != null) {
            Modifier newModif = bd.getAST().newModifier(modelModifierFlag);
            bd.modifiers().add(newModif);
        }
    }

}

From source file:com.google.devtools.j2objc.jdt.JdtJ2ObjCIncompatibleStripper.java

License:Apache License

private boolean visitBodyDeclaration(BodyDeclaration node) {
    @SuppressWarnings("unchecked")
    List<IExtendedModifier> modifiers = node.modifiers();
    for (IExtendedModifier modifier : modifiers) {
        if (isJ2ObjCIncompatible(modifier)) {
            nodesToStrip.add(node);/*from  w w  w  .j a  v  a 2s .c  o  m*/
            return false;
        }
    }
    return true;
}