Java: Clarify that Annotatable predicates consider inherited annotations

Additionally changes `hasAnnotation()` to consider inherited annotations
for consistency.
This commit is contained in:
Marcono1234
2021-07-10 22:52:54 +02:00
committed by Chris Smowton
parent afb7462052
commit f69b6eef7a
2 changed files with 20 additions and 6 deletions

View File

@@ -166,19 +166,33 @@ private predicate sourceAnnotValue(Annotation a, Method m, Expr val) {
/** An abstract representation of language elements that can be annotated. */
class Annotatable extends Element {
/** Holds if this element has an annotation. */
predicate hasAnnotation() { exists(Annotation a | a.getAnnotatedElement() = this) }
/** Holds if this element has an annotation, including inherited annotations. */
predicate hasAnnotation() { exists(getAnAnnotation()) }
/** Holds if this element has the specified annotation. */
/** Holds if this element has a declared annotation, excluding inherited annotations. */
predicate hasDeclaredAnnotation() { exists(getADeclaredAnnotation()) }
/**
* Holds if this element has the specified annotation, including inherited
* annotations.
*/
predicate hasAnnotation(string package, string name) {
exists(AnnotationType at | at = this.getAnAnnotation().getType() |
at.nestedName() = name and at.getPackage().getName() = package
)
}
/** Gets an annotation that applies to this element. */
/**
* Gets an annotation that applies to this element, including inherited annotations.
*/
// This predicate is overridden by Class to consider inherited annotations
cached
Annotation getAnAnnotation() { result.getAnnotatedElement() = this }
Annotation getAnAnnotation() { result = getADeclaredAnnotation() }
/**
* Gets an annotation that is declared on this element, excluding inherited annotations.
*/
Annotation getADeclaredAnnotation() { result.getAnnotatedElement() = this }
/**
* Holds if this or any enclosing `Annotatable` has a `@SuppressWarnings("<category>")`

View File

@@ -120,7 +120,7 @@ private newtype TPrintAstNode =
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
} or
TAnnotationsNode(Annotatable ann) {
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
shouldPrint(ann, _) and ann.hasDeclaredAnnotation() and not partOfAnnotation(ann)
} or
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or