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.
This commit is contained in:
Clément Fournier
2020-02-07 17:43:16 +01:00
parent 131f2488c5
commit b7cc31ac65
3 changed files with 57 additions and 22 deletions

View File

@ -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<ASTTypeParameter> 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<ASTClassOrInterfaceType> getSuperInterfaces() {
Iterable<ASTClassOrInterfaceType> it = isInterface()
? getFirstChildOfType(ASTExtendsList.class)
: getFirstChildOfType(ASTImplementsList.class);
return it == null ? Collections.emptyList() : IteratorUtil.toList(it.iterator());
}
}

View File

@ -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<ASTMethodOrConstructorDeclaration> {
public interface ASTMethodOrConstructorDeclaration
extends MethodLikeNode,
AccessNode,
SignedNode<ASTMethodOrConstructorDeclaration>,
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.
*/

View File

@ -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<ASTTypeParameter> getTypeParameterList() {
ASTTypeParameters parameters = getTypeParameters();
if (parameters == null) {
return Collections.emptyList();
}
return parameters.asList();
}
}