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();
}
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();
}
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();
}
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;
}
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;
}
Aggregations