in legacy/java/piranha/src/main/java/com/uber/piranha/XPFlagCleaner.java [983:1039]
public Description matchVariable(VariableTree tree, VisitorState state) {
if (shouldSkip(state)) return Description.NO_MATCH;
Symbol identifier = FindIdentifiers.findIdent(xpFlagName, state);
// Check if this is the flag definition and remove it.
if (identifier != null
&& identifier.isEnum()
&& identifier.equals(ASTHelpers.getSymbol(tree))) {
xpSym = identifier;
return removeEnumValue(
tree, state, state.getSourceForNode(tree), isOnlyEnumConstant(xpSym, state));
} else if (identifier == null
&& ASTHelpers.getSymbol(tree) != null
&& xpFlagName.equals(ASTHelpers.getSymbol(tree).getConstantValue())) {
return buildDescription(tree).addFix(SuggestedFix.delete(tree)).build();
}
Symbol sym = ASTHelpers.getSymbol(tree);
// Also checks if this is the flag definition. However, this is for the case where xpFlagName
// does not match the enum constant itself, but instead, a String value in the enum constructor
// (e.g. STALE_FLAG("stale.flag"), where xpFlagName is "stale.flag")
if (sym != null && sym.isEnum() && tree.getInitializer().getKind() == Tree.Kind.NEW_CLASS) {
// Enum constructor
NewClassTree nct = (NewClassTree) tree.getInitializer();
String enumClassName = sym.enclClass().getSimpleName().toString();
ImmutableCollection<PiranhaEnumRecord> enumRecords =
this.config.getEnumRecordsForName(enumClassName);
boolean containsFlagName =
enumConstructorArgsContainsFlagNameMatcher(enumRecords).matches(nct, state);
if (containsFlagName) {
return removeEnumValue(
tree, state, state.getSourceForNode(tree), isOnlyEnumConstant(sym, state));
}
}
// We also match the enum constant previous to the one being removed in some cases,
// to fix delimiters.
if (sym != null && sym.isEnum()) {
EnumEnding enumEnding = getEndingOfLastEnumConstantIfRemoved(sym, state);
if (enumEnding == EnumEnding.IGNORE) {
return Description.NO_MATCH;
}
// The next enum constant in the list will be removed by Piranha
// Let's replace the current enum constant's ending with the previous one
return buildDescription(tree)
.addFix(
SuggestedFix.replace(tree, state.getSourceForNode(tree) + enumEnding.getChar(), 0, 1))
.build();
}
return Description.NO_MATCH;
}