Java: PrintAst: Supprt generic parameters

This commit is contained in:
Joe
2020-09-10 15:42:06 +01:00
parent 19af3e5e30
commit c20f802666
2 changed files with 62 additions and 5 deletions

View File

@@ -180,6 +180,8 @@ class TypeVariable extends BoundedType, @typevariable {
or
result = getASuppliedType().(TypeVariable).getAnUltimatelySuppliedType()
}
override string getAPrimaryQlClass() { result = "TypeVariable" }
}
/**

View File

@@ -89,7 +89,9 @@ private newtype TPrintAstNode =
TElementNode(Element el) { shouldPrint(el, _) } or
TAnnotationsNode(Annotatable ann) { shouldPrint(ann, _) and ann.hasAnnotation() } or
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) }
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) }
/**
* A node in the output tree.
@@ -221,12 +223,15 @@ final class CallableNode extends ElementNode {
result.(AnnotationsNode).getAnnotated() = callable
or
childIndex = 1 and
result.(ElementNode).getElement().(Expr).isNthChildOf(callable, -1) // return type
result.(GenericCallableNode).getCallable() = callable
or
childIndex = 2 and
result.(ParametersNode).getCallable() = callable
result.(ElementNode).getElement().(Expr).isNthChildOf(callable, -1) // return type
or
childIndex = 3 and
result.(ParametersNode).getCallable() = callable
or
childIndex = 4 and
result.(ElementNode).getElement() = callable.getBody()
}
}
@@ -273,10 +278,13 @@ final class ClassInterfaceNode extends ElementNode {
}
override PrintAstNode getChild(int childIndex) {
// TODO: generic params, javadoc?
childIndex = -2 and
// TODO: javadoc
childIndex = -3 and
result.(AnnotationsNode).getAnnotated() = ty
or
childIndex = -2 and
result.(GenericTypeNode).getType() = ty
or
childIndex = -1 and
result.(BaseTypesNode).getClassOrInterface() = ty
or
@@ -341,6 +349,17 @@ final class ImportNode extends ElementNode {
ImportNode() { element instanceof Import }
}
/**
* A node representing a `TypeVariable`.
*/
final class TypeVariableNode extends ElementNode {
TypeVariableNode() { element instanceof TypeVariable }
override ElementNode getChild(int childIndex) {
result.getElement().(Expr).isNthChildOf(element, childIndex)
}
}
/**
* A node representing the annotations of an `Annotatable`.
* Only rendered if there is at least one annotation.
@@ -408,6 +427,42 @@ final class BaseTypesNode extends PrintAstNode, TBaseTypesNode {
ClassOrInterface getClassOrInterface() { result = ty }
}
/**
* A node representing the type parameters of a `Class` or `Interface`.
* Only rendered when the type in question is indeed generic.
*/
final class GenericTypeNode extends PrintAstNode, TGenericTypeNode {
GenericType ty;
GenericTypeNode() { this = TGenericTypeNode(ty) }
override string toString() { result = "(Generic Parameters)" }
override ElementNode getChild(int childIndex) {
result.getElement().(TypeVariable) = ty.getTypeParameter(childIndex)
}
GenericType getType() { result = ty }
}
/**
* A node representing the type parameters of a `Callable`.
* Only rendered when the callable in question is indeed generic.
*/
final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
GenericCallable c;
GenericCallableNode() { this = TGenericCallableNode(c) }
override string toString() { result = "(Generic Parameters)" }
override ElementNode getChild(int childIndex) {
result.getElement().(TypeVariable) = c.getTypeParameter(childIndex)
}
GenericCallable getCallable() { result = c }
}
/** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) }