From b7cc31ac6536d71c1de69bae07515db923963bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 7 Feb 2020 17:43:16 +0100 Subject: [PATCH] Abstract type param owners This introduces a TypeParamOwnerNode interface, for those nodes that declare a type parameter list. This will be useful later on to implement AST symbols. --- .../lang/java/ast/ASTAnyTypeDeclaration.java | 24 +++++++----- .../ASTMethodOrConstructorDeclaration.java | 18 +++------ .../pmd/lang/java/ast/TypeParamOwnerNode.java | 37 +++++++++++++++++++ 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypeParamOwnerNode.java diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java index 1c2ac05fa7..a66734c162 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java @@ -11,6 +11,7 @@ import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; +import net.sourceforge.pmd.internal.util.IteratorUtil; import net.sourceforge.pmd.lang.ast.NodeStream; import net.sourceforge.pmd.lang.java.qname.JavaTypeQualifiedName; @@ -23,6 +24,7 @@ public interface ASTAnyTypeDeclaration extends TypeNode, JavaQualifiableNode, AccessNode, + TypeParamOwnerNode, FinalizableNode { /** @@ -94,15 +96,6 @@ public interface ASTAnyTypeDeclaration return (ASTTypeBody) getLastChild(); } - default List getTypeParameters() { - ASTTypeParameters parameters = getFirstChildOfType(ASTTypeParameters.class); - if (parameters == null) { - return Collections.emptyList(); - } - - return parameters.asList(); - } - /** * Returns true if this type declaration is nested inside an interface, * class or annotation. @@ -162,4 +155,17 @@ public interface ASTAnyTypeDeclaration } + /** + * Returns the interfaces implemented by this class, or + * extended by this interface. Returns an empty list if + * none is specified. + */ + default List getSuperInterfaces() { + + Iterable it = isInterface() + ? getFirstChildOfType(ASTExtendsList.class) + : getFirstChildOfType(ASTImplementsList.class); + + return it == null ? Collections.emptyList() : IteratorUtil.toList(it.iterator()); + } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java index 9b5555a119..87080fbffc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java @@ -26,7 +26,11 @@ import net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName; * @see MethodLikeNode * @since 5.8.1 */ -public interface ASTMethodOrConstructorDeclaration extends MethodLikeNode, AccessNode, SignedNode { +public interface ASTMethodOrConstructorDeclaration + extends MethodLikeNode, + AccessNode, + SignedNode, + TypeParamOwnerNode { /** @@ -84,17 +88,6 @@ public interface ASTMethodOrConstructorDeclaration extends MethodLikeNode, Acces return last instanceof ASTBlock ? (ASTBlock) last : null; } - - /** - * Returns the type parameter declaration of this node, or null if - * there is none. - */ - @Nullable - default ASTTypeParameters getTypeParameters() { - return getFirstChildOfType(ASTTypeParameters.class); - } - - /** * Returns the {@code throws} clause of this declaration, or null * if there is none. @@ -104,7 +97,6 @@ public interface ASTMethodOrConstructorDeclaration extends MethodLikeNode, Acces return getFirstChildOfType(ASTThrowsList.class); } - /** * Returns true if this node's last formal parameter is varargs. */ diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypeParamOwnerNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypeParamOwnerNode.java new file mode 100644 index 0000000000..5f861e709e --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypeParamOwnerNode.java @@ -0,0 +1,37 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast; + +import java.util.Collections; +import java.util.List; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * @author Clément Fournier + */ +public interface TypeParamOwnerNode extends JavaNode { + + /** + * Returns the type parameter declaration of this node, or null if + * there is none. + */ + @Nullable + default ASTTypeParameters getTypeParameters() { + return getFirstChildOfType(ASTTypeParameters.class); + } + + + default List getTypeParameterList() { + ASTTypeParameters parameters = getTypeParameters(); + if (parameters == null) { + return Collections.emptyList(); + } + + return parameters.asList(); + } + + +}