[java] Push isStatic() down to declaration

Only on ASTMethodDeclaration, ASTFieldDeclaration and ASTAnyTypeDeclaration.
This makes it possible to remove it
from AccessNode.
This commit is contained in:
Andreas Dangel committed 2023-12-11 15:19:12 +01:00
1 parent e4f2307b06
commit 9869dccf4b
5 files changed
+25 -2

No files matched your search

@@ -135,6 +135,14 @@ public interface ASTAnyTypeDeclaration
return hasModifiers(ABSTRACT);
}
/**
* Returns true if this type is static. Only inner types can be static.
*/
@Override
default boolean isStatic() {
return hasModifiers(JModifier.STATIC);
}
/**
* Returns the enum constants declared by this enum. If this is not
@@ -73,4 +73,11 @@ public final class ASTFieldDeclaration extends AbstractJavaNode
return getFirstChildOfType(ASTType.class);
}
/**
* Returns true if this field is static.
*/
@Override
public boolean isStatic() {
return hasModifiers(JModifier.STATIC);
}
}
@@ -132,6 +132,12 @@ public final class ASTMethodDeclaration extends AbstractMethodOrConstructorDecla
return getResultTypeNode().isVoid();
}
/** Returns true if this method is static. */
@Override
public boolean isStatic() {
return hasModifiers(JModifier.STATIC);
}
/**
* Returns the default clause, if this is an annotation method declaration
@@ -16,7 +16,8 @@ import net.sourceforge.pmd.lang.ast.NodeStream;
* A node that owns a {@linkplain ASTModifierList modifier list}.
*
* <p>{@link AccessNode} methods take into account the syntactic context of the
* declaration, e.g. {@link #isPublic()} will always return true for a field
* declaration, e.g. {@link #hasModifiers(JModifier, JModifier...) hasModifiers(JModifier.PUBLIC)}
* will always return true for a field
* declared inside an interface, regardless of whether the {@code public}
* modifier was specified explicitly or not. If you want to know whether
* the modifier was explicitly stated, use {@link #hasExplicitModifiers(JModifier, JModifier...)}.
@@ -12,6 +12,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.ast.JModifier;
import net.sourceforge.pmd.lang.java.rule.AbstractIgnoredAnnotationRule;
import net.sourceforge.pmd.lang.java.rule.design.UseUtilityClassRule;
@@ -50,7 +51,7 @@ public class AtLeastOneConstructorRule extends AbstractIgnoredAnnotationRule {
NodeStream<AccessNode> members = node.getDeclarations()
.filterIs(AccessNode.class)
.filterNot(it -> it instanceof ASTAnyTypeDeclaration);
if (members.isEmpty() || members.any(it -> !it.isStatic())) {
if (members.isEmpty() || members.any(it -> !it.hasModifiers(JModifier.STATIC))) {
// Do we have any non-static members?
addViolation(data, node);
}