Search in sources :

Example 21 with Modifier

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

the class ObsoleteRedundantModifiersCleanUp method removeProtectedModifier.

private boolean removeProtectedModifier(final BodyDeclaration visited) {
    List<IExtendedModifier> modifiers = visited.modifiers();
    for (Modifier modifier : getModifiersOnly(modifiers)) {
        if (modifier.isProtected()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(modifier, group);
            return false;
        }
    }
    return true;
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 22 with Modifier

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

the class LocalVariableCouldBeFinalQuickFix method apply.

/**
 * Adds the final modifier to the variable declaration.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final VariableDeclarationStatement node) {
    final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class);
    modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
    node.modifiers().add(modifier);
    return true;
}
Also used : Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 23 with Modifier

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

the class UseUtilityClassQuickFix method addFinalIfNecessary.

private void addFinalIfNecessary(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
    @SuppressWarnings("unchecked") final List<IExtendedModifier> modifiers = typeDeclaration.modifiers();
    if (!Iterables.any(modifiers, isFinal())) {
        final ListRewrite modifierRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.MODIFIERS2_PROPERTY);
        final Modifier modifier = (Modifier) typeDeclaration.getAST().createInstance(Modifier.class);
        modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
        modifierRewrite.insertLast(modifier, null);
    }
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 24 with Modifier

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

the class UseUtilityClassQuickFix method addPrivateConstructor.

@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
    final AST ast = typeDeclaration.getAST();
    final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
    constructor.setConstructor(true);
    final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
    modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
    constructor.modifiers().add(modifier);
    constructor.setName(ASTUtil.copy(typeDeclaration.getName()));
    final Block body = (Block) ast.createInstance(Block.class);
    constructor.setBody(body);
    final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
    final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT);
    statementRewrite.insertFirst(comment, null);
    final int position = findConstructorPosition(typeDeclaration);
    final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    bodyDeclarationRewrite.insertAt(constructor, position, null);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 25 with Modifier

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

the class MethodArgumentCouldBeFinalQuickFix method apply.

/**
 * Adds the final modifier to the variable declaration.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final SingleVariableDeclaration node) {
    final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class);
    modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
    node.modifiers().add(modifier);
    return true;
}
Also used : Modifier(org.eclipse.jdt.core.dom.Modifier)

Aggregations

Modifier (org.eclipse.jdt.core.dom.Modifier)49 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)39 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 AST (org.eclipse.jdt.core.dom.AST)7 ArrayList (java.util.ArrayList)6 Block (org.eclipse.jdt.core.dom.Block)6 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 Type (org.eclipse.jdt.core.dom.Type)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 Expression (org.eclipse.jdt.core.dom.Expression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4