Search in sources :

Example 11 with Modifier

use of javax.lang.model.element.Modifier in project error-prone by google.

the class IncompatibleModifiersChecker method matchAnnotation.

@Override
public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
    Set<Modifier> incompatibleModifiers = getIncompatibleModifiers(tree, state);
    if (incompatibleModifiers.isEmpty()) {
        return Description.NO_MATCH;
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (!(parent instanceof ModifiersTree)) {
        // e.g. An annotated package name
        return Description.NO_MATCH;
    }
    Set<Modifier> incompatible = Sets.intersection(incompatibleModifiers, ((ModifiersTree) parent).getFlags());
    if (incompatible.isEmpty()) {
        return Description.NO_MATCH;
    }
    String annotationName = ASTHelpers.getAnnotationName(tree);
    String nameString = annotationName != null ? String.format("The annotation '@%s'", annotationName) : "This annotation";
    String customMessage = String.format(MESSAGE_TEMPLATE, nameString, incompatible.toString());
    return buildDescription(tree).setMessage(customMessage).build();
}
Also used : ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) ModifiersTree(com.sun.source.tree.ModifiersTree) Tree(com.sun.source.tree.Tree) Modifier(javax.lang.model.element.Modifier)

Example 12 with Modifier

use of javax.lang.model.element.Modifier in project error-prone by google.

the class SuggestedFixes method removeModifiers.

/** Remove modifiers from the given class, method, or field declaration. */
@Nullable
public static Fix removeModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
    Set<Modifier> toRemove = ImmutableSet.copyOf(modifiers);
    ModifiersTree originalModifiers = getModifiers(tree);
    if (originalModifiers == null) {
        return null;
    }
    SuggestedFix.Builder fix = SuggestedFix.builder();
    List<ErrorProneToken> tokens = state.getTokensForNode(originalModifiers);
    int basePos = ((JCTree) originalModifiers).getStartPosition();
    boolean empty = true;
    for (ErrorProneToken tok : tokens) {
        Modifier mod = getTokModifierKind(tok);
        if (toRemove.contains(mod)) {
            empty = false;
            fix.replace(basePos + tok.pos(), basePos + tok.endPos() + 1, "");
            break;
        }
    }
    if (empty) {
        return null;
    }
    return fix.build();
}
Also used : ModifiersTree(com.sun.source.tree.ModifiersTree) ErrorProneToken(com.google.errorprone.util.ErrorProneToken) JCTree(com.sun.tools.javac.tree.JCTree) Builder(com.google.errorprone.fixes.SuggestedFix.Builder) Modifier(javax.lang.model.element.Modifier) Nullable(javax.annotation.Nullable)

Example 13 with Modifier

use of javax.lang.model.element.Modifier in project error-prone by google.

the class RequiredModifiersChecker method matchAnnotation.

@Override
public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
    RequiredModifiers annotation = ASTHelpers.getAnnotation(tree, RequiredModifiers.class);
    if (annotation == null) {
        return Description.NO_MATCH;
    }
    Set<Modifier> requiredModifiers = ImmutableSet.copyOf(annotation.value());
    if (requiredModifiers.isEmpty()) {
        return Description.NO_MATCH;
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (!(parent instanceof ModifiersTree)) {
        // e.g. An annotated package name
        return Description.NO_MATCH;
    }
    Set<Modifier> missing = Sets.difference(requiredModifiers, ((ModifiersTree) parent).getFlags());
    if (missing.isEmpty()) {
        return Description.NO_MATCH;
    }
    String annotationName = ASTHelpers.getAnnotationName(tree);
    String nameString = annotationName != null ? String.format("The annotation '@%s'", annotationName) : "This annotation";
    String customMessage = String.format(MESSAGE_TEMPLATE, nameString, missing.toString());
    return buildDescription(tree).setMessage(customMessage).build();
}
Also used : ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) ModifiersTree(com.sun.source.tree.ModifiersTree) Tree(com.sun.source.tree.Tree) Modifier(javax.lang.model.element.Modifier) RequiredModifiers(com.google.errorprone.annotations.RequiredModifiers)

Example 14 with Modifier

use of javax.lang.model.element.Modifier in project error-prone by google.

the class Util method makeConcreteClassAbstract.

/**
   * Returns a fix that changes a concrete class to an abstract class.
   *
   * <ul>
   * <li>Removes {@code final} if it was there.
   * <li>Adds {@code abstract} if it wasn't there.
   * <li>Adds a private empty constructor if the class was {@code final} and had only a default
   *     constructor.
   * </ul>
   */
static SuggestedFix.Builder makeConcreteClassAbstract(ClassTree classTree, VisitorState state) {
    Set<Modifier> flags = EnumSet.noneOf(Modifier.class);
    flags.addAll(classTree.getModifiers().getFlags());
    boolean wasFinal = flags.remove(FINAL);
    boolean wasAbstract = !flags.add(ABSTRACT);
    if (classTree.getKind().equals(INTERFACE) || (!wasFinal && wasAbstract)) {
        // no-op
        return SuggestedFix.builder();
    }
    ImmutableList.Builder<Object> modifiers = ImmutableList.builder();
    for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) {
        modifiers.add(state.getSourceForNode(annotation));
    }
    modifiers.addAll(flags);
    SuggestedFix.Builder makeAbstract = SuggestedFix.builder();
    if (((JCModifiers) classTree.getModifiers()).pos == -1) {
        makeAbstract.prefixWith(classTree, Joiner.on(' ').join(modifiers.build()));
    } else {
        makeAbstract.replace(classTree.getModifiers(), Joiner.on(' ').join(modifiers.build()));
    }
    if (wasFinal && HAS_GENERATED_CONSTRUCTOR.matches(classTree, state)) {
        makeAbstract.merge(addPrivateConstructor(classTree));
    }
    return makeAbstract;
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ImmutableList(com.google.common.collect.ImmutableList) AnnotationTree(com.sun.source.tree.AnnotationTree) Modifier(javax.lang.model.element.Modifier) Matchers.hasModifier(com.google.errorprone.matchers.Matchers.hasModifier)

Example 15 with Modifier

use of javax.lang.model.element.Modifier in project immutables by immutables.

the class ValueType method getConstructorAnnotations.

public List<CharSequence> getConstructorAnnotations() {
    if (constructorAnnotations == null) {
        List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements());
        for (ExecutableElement c : constructors) {
            if (c.getParameters().isEmpty()) {
                Set<Modifier> modifiers = c.getModifiers();
                if (modifiers.contains(Modifier.PRIVATE)) {
                    report().withElement(c).error("Constructor in an abstract value type should not be private");
                }
                constructorAnnotations = Annotations.getAnnotationLines(c, Collections.<String>emptySet(), true, false, ElementType.CONSTRUCTOR, newTypeStringResolver());
            }
        }
        if (constructorAnnotations == null) {
            for (ExecutableElement c : constructors) {
                report().withElement(c).error("Constructor should not have parameters in an abstract value type to be extended");
            }
            constructorAnnotations = ImmutableList.of();
        }
    }
    return constructorAnnotations;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) Modifier(javax.lang.model.element.Modifier)

Aggregations

Modifier (javax.lang.model.element.Modifier)21 TypeMirror (javax.lang.model.type.TypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)6 Tree (com.sun.source.tree.Tree)5 AnnotationTree (com.sun.source.tree.AnnotationTree)4 ArrayList (java.util.ArrayList)4 Element (javax.lang.model.element.Element)4 ElementKind (javax.lang.model.element.ElementKind)4 ImmutableList (com.google.common.collect.ImmutableList)3 MethodTree (com.sun.source.tree.MethodTree)3 ModifiersTree (com.sun.source.tree.ModifiersTree)3 VariableTree (com.sun.source.tree.VariableTree)3 Nullable (javax.annotation.Nullable)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 Function (com.google.common.base.Function)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Sets (com.google.common.collect.Sets)2 VisitorState (com.google.errorprone.VisitorState)2 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)2 ErrorProneToken (com.google.errorprone.util.ErrorProneToken)2