use of com.sun.source.tree.ModifiersTree in project error-prone by google.
the class SuggestedFixes method addModifiers.
/** Add modifiers to the given class, method, or field declaration. */
@Nullable
public static SuggestedFix addModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
ModifiersTree originalModifiers = getModifiers(tree);
if (originalModifiers == null) {
return null;
}
Set<Modifier> toAdd = Sets.difference(new TreeSet<>(Arrays.asList(modifiers)), originalModifiers.getFlags());
if (originalModifiers.getFlags().isEmpty()) {
int pos = state.getEndPosition(originalModifiers) != Position.NOPOS ? state.getEndPosition(originalModifiers) + 1 : ((JCTree) tree).getStartPosition();
int base = ((JCTree) tree).getStartPosition();
java.util.Optional<Integer> insert = state.getTokensForNode(tree).stream().map(token -> token.pos() + base).filter(thisPos -> thisPos >= pos).findFirst();
// shouldn't ever be able to get to the else
int insertPos = insert.orElse(pos);
return SuggestedFix.replace(insertPos, insertPos, Joiner.on(' ').join(toAdd) + " ");
}
// a map from modifiers to modifier position (or -1 if the modifier is being added)
// modifiers are sorted in Google Java Style order
Map<Modifier, Integer> modifierPositions = new TreeMap<>();
for (Modifier mod : toAdd) {
modifierPositions.put(mod, -1);
}
List<ErrorProneToken> tokens = state.getTokensForNode(originalModifiers);
int base = ((JCTree) originalModifiers).getStartPosition();
for (ErrorProneToken tok : tokens) {
Modifier mod = getTokModifierKind(tok);
if (mod != null) {
modifierPositions.put(mod, base + tok.pos());
}
}
SuggestedFix.Builder fix = SuggestedFix.builder();
// walk the map of all modifiers, and accumulate a list of new modifiers to insert
// beside an existing modifier
List<Modifier> modifiersToWrite = new ArrayList<>();
for (Modifier mod : modifierPositions.keySet()) {
int p = modifierPositions.get(mod);
if (p == -1) {
modifiersToWrite.add(mod);
} else if (!modifiersToWrite.isEmpty()) {
fix.replace(p, p, Joiner.on(' ').join(modifiersToWrite) + " ");
modifiersToWrite.clear();
}
}
if (!modifiersToWrite.isEmpty()) {
fix.postfixWith(originalModifiers, " " + Joiner.on(' ').join(modifiersToWrite));
}
return fix.build();
}
use of com.sun.source.tree.ModifiersTree 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 com.sun.source.tree.ModifiersTree 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 com.sun.source.tree.ModifiersTree 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();
}
Aggregations