[java] Restore compatibility of SingularFieldRule

This commit is contained in:
Andreas Dangel
2024-04-04 15:20:51 +02:00
parent 4fe50978b6
commit e47eb352d5
2 changed files with 22 additions and 7 deletions

View File

@@ -79,7 +79,7 @@ public class SingularFieldRule extends AbstractJavaRulechainRule {
DataflowResult dataflow = null;
for (ASTFieldDeclaration fieldDecl : enclosingType.getDeclarations(ASTFieldDeclaration.class)) {
if (!mayBeSingular(fieldDecl)
if (!isPrivateNotFinal(fieldDecl)
|| JavaAstUtils.hasAnyAnnotation(fieldDecl, getProperty(IGNORED_FIELD_ANNOTATIONS))) {
continue;
}
@@ -95,13 +95,23 @@ public class SingularFieldRule extends AbstractJavaRulechainRule {
return null;
}
private static boolean mayBeSingular(ModifierOwner varId) {
/**
* This method is only relevant for this rule. It will be removed in the future.
*
* @deprecated This method will be removed. Don't use it.
*/
@Deprecated //(since = "7.1.0", forRemoval = true)
public static boolean mayBeSingular(ModifierOwner varId) {
return isPrivateNotFinal(varId);
}
private static boolean isPrivateNotFinal(ModifierOwner varId) {
return varId.getEffectiveVisibility().isAtMost(Visibility.V_PRIVATE)
// We ignore final variables for a reason:
// - if they're static they are there to share a value and that is not a mistake
// - if they're not static then if this rule matches, then so does FinalFieldCouldBeStatic,
// and that rule has the better fix.
&& !varId.hasModifiers(JModifier.FINAL);
// We ignore final variables for a reason:
// - if they're static they are there to share a value and that is not a mistake
// - if they're not static then if this rule matches, then so does FinalFieldCouldBeStatic,
// and that rule has the better fix.
&& !varId.hasModifiers(JModifier.FINAL);
}
private boolean isSingularField(ASTTypeDeclaration fieldOwner, ASTVariableId varId, DataflowResult dataflow) {