diff --git a/java/ql/lib/semmle/code/java/PrintAst.qll b/java/ql/lib/semmle/code/java/PrintAst.qll index d6420f2e4ca..3093c9b2643 100644 --- a/java/ql/lib/semmle/code/java/PrintAst.qll +++ b/java/ql/lib/semmle/code/java/PrintAst.qll @@ -120,7 +120,12 @@ private newtype TPrintAstNode = shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent } or TAnnotationsNode(Annotatable ann) { - shouldPrint(ann, _) and ann.hasDeclaredAnnotation() and not partOfAnnotation(ann) + shouldPrint(ann, _) and + ann.hasDeclaredAnnotation() and + not partOfAnnotation(ann) and + // The Kotlin compiler might add annotations that are only present in byte code, although the annotatable element is + // present in source code. + exists(Annotation a | a.getAnnotatedElement() = ann and shouldPrint(a, _)) } or TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or diff --git a/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected b/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected index 57a8b571306..36ab7c99887 100644 --- a/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected +++ b/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected @@ -11,15 +11,11 @@ def.kt: # 14| 0: [MethodAccess] abc(...) # 14| -1: [VarAccess] a # 1| 2: [Interface] SomeAnnotation -#-----| -3: (Annotations) # 1| 1: [Method] abc -#-----| 1: (Annotations) # 1| 2: [Method] y # 3| 3: [Interface] ReplaceWith -#-----| -3: (Annotations) # 3| 1: [Method] expression # 5| 4: [Interface] Deprecated -#-----| -3: (Annotations) # 6| 1: [Method] message # 7| 2: [Method] replaceWith # 9| 5: [Class] X