Search in sources :

Example 11 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier in project lombok by rzwitserloot.

the class PatchValEclipse method addFinalAndValAnnotationToModifierList.

public static void addFinalAndValAnnotationToModifierList(Object converter, List<IExtendedModifier> modifiers, AST ast, LocalDeclaration in) {
    // First check that 'in' has the final flag on, and a @val / @lombok.val annotation.
    if ((in.modifiers & ClassFileConstants.AccFinal) == 0)
        return;
    if (in.annotations == null)
        return;
    boolean found = false;
    Annotation valAnnotation = null;
    for (Annotation ann : in.annotations) {
        if (couldBeVal(ann.type)) {
            found = true;
            valAnnotation = ann;
            break;
        }
    }
    if (!found)
        return;
    // This is null only if the project is 1.4 or less. Lombok doesn't work in that.
    if (modifiers == null)
        return;
    boolean finalIsPresent = false;
    boolean valIsPresent = false;
    for (Object present : modifiers) {
        if (present instanceof Modifier) {
            ModifierKeyword keyword = ((Modifier) present).getKeyword();
            if (keyword == null)
                continue;
            if (keyword.toFlagValue() == Modifier.FINAL)
                finalIsPresent = true;
        }
        if (present instanceof org.eclipse.jdt.core.dom.Annotation) {
            Name typeName = ((org.eclipse.jdt.core.dom.Annotation) present).getTypeName();
            if (typeName != null) {
                String fullyQualifiedName = typeName.getFullyQualifiedName();
                if ("val".equals(fullyQualifiedName) || "lombok.val".equals(fullyQualifiedName)) {
                    valIsPresent = true;
                }
            }
        }
    }
    if (!finalIsPresent) {
        modifiers.add(createModifier(ast, ModifierKeyword.FINAL_KEYWORD, valAnnotation.sourceStart, valAnnotation.sourceEnd));
    }
    if (!valIsPresent) {
        MarkerAnnotation newAnnotation = createValAnnotation(ast, valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd);
        try {
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation, valAnnotation);
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation.getTypeName(), valAnnotation.type);
        } catch (IllegalAccessException e) {
            throw Lombok.sneakyThrow(e);
        } catch (InvocationTargetException e) {
            throw Lombok.sneakyThrow(e.getCause());
        }
        modifiers.add(newAnnotation);
    }
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 12 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final SingleVariableDeclaration it) {
    if ((((it.getParent() instanceof MethodDeclaration) || (it.getParent() instanceof CatchClause)) || (it.getParent() instanceof EnhancedForStatement))) {
        final Function1<IExtendedModifier, Boolean> _function = (IExtendedModifier it_1) -> {
            return Boolean.valueOf(it_1.isAnnotation());
        };
        this.appendModifiers(it, IterableExtensions.<IExtendedModifier>filter(Iterables.<IExtendedModifier>filter(it.modifiers(), IExtendedModifier.class), _function));
    } else {
        this.appendModifiers(it, it.modifiers());
    }
    it.getType().accept(this);
    this.appendExtraDimensions(it.getExtraDimensions());
    boolean _isVarargs = it.isVarargs();
    if (_isVarargs) {
        this.appendToBuffer("...");
    }
    this.appendSpaceToBuffer();
    it.getName().accept(this);
    Expression _initializer = it.getInitializer();
    boolean _tripleNotEquals = (_initializer != null);
    if (_tripleNotEquals) {
        this.appendToBuffer("=");
        it.getInitializer().accept(this);
    }
    return false;
}
Also used : InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) CatchClause(org.eclipse.jdt.core.dom.CatchClause) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 13 with IExtendedModifier

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

the class RemoveUselessModifiersRefactoring method removeFinalModifier.

private boolean removeFinalModifier(List<IExtendedModifier> modifiers) {
    boolean result = VISIT_SUBTREE;
    for (Modifier m : getModifiersOnly(modifiers)) {
        if (m.isFinal()) {
            ctx.getRefactorings().remove(m);
            result = DO_NOT_VISIT_SUBTREE;
        }
    }
    return result;
}
Also used : IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 14 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier 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 15 with IExtendedModifier

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

the class SuppressWarningsQuickFix method apply.

@Override
protected boolean apply(final ASTNode node) {
    final ASTNode annotatableNode = findAnnotatableASTNode(node);
    if (annotatableNode != null) {
        final AST ast = node.getAST();
        final List<IExtendedModifier> modifiers = getModifiers(annotatableNode);
        final Annotation existingAnnotation = findExistingSuppressWarningsAnnotation(modifiers);
        final Annotation annotation = createReplacementSuppressWarningsAnnotation(existingAnnotation, ast);
        if (existingAnnotation == null) {
            final int position = findPosition(modifiers);
            modifiers.add(position, annotation);
        } else {
            ASTUtil.replace(existingAnnotation, annotation);
        }
        return !annotation.equals(existingAnnotation);
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Aggregations

IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)46 Modifier (org.eclipse.jdt.core.dom.Modifier)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)15 Annotation (org.eclipse.jdt.core.dom.Annotation)14 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)11 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 ArrayList (java.util.ArrayList)8 AST (org.eclipse.jdt.core.dom.AST)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)5 Statement (org.eclipse.jdt.core.dom.Statement)5 Type (org.eclipse.jdt.core.dom.Type)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 Expression (org.eclipse.jdt.core.dom.Expression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4