From 47e38952d16702f44ddb32c40b8ea12554a88da5 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sun, 27 Mar 2022 23:32:04 +0200 Subject: [PATCH] Java: Improve Annotation.getAnAssociatedAnnotation As suggested by smowton during review. --- java/ql/lib/semmle/code/java/Annotation.qll | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/java/ql/lib/semmle/code/java/Annotation.qll b/java/ql/lib/semmle/code/java/Annotation.qll index 2eef93537fe..fbab98c2c28 100644 --- a/java/ql/lib/semmle/code/java/Annotation.qll +++ b/java/ql/lib/semmle/code/java/Annotation.qll @@ -217,13 +217,15 @@ class Annotatable extends Element { ) } - private Annotation getAnAssociatedAnnotation(AnnotationType t) { - result.getType() = t and + private Annotation getADeclaredAssociatedAnnotation(AnnotationType t) { // Direct or indirect annotation - if getADeclaredAnnotation().getType() = t or getAnIndirectAnnotation().getType() = t - then ( - result = getADeclaredAnnotation() or result = getAnIndirectAnnotation() - ) else ( + result.getType() = t and result = [getADeclaredAnnotation(), getAnIndirectAnnotation()] + } + + private Annotation getAnAssociatedAnnotation(AnnotationType t) { + if exists(getADeclaredAssociatedAnnotation(t)) + then result = getADeclaredAssociatedAnnotation(t) + else ( // Only if neither a direct nor an indirect annotation is present look for an inherited one t.isInherited() and // @Inherited only works for classes; cast to Annotatable is necessary because predicate is private