diff --git a/java/ql/lib/semmle/code/java/PrintAst.qll b/java/ql/lib/semmle/code/java/PrintAst.qll index 3093c9b2643..802ba19030b 100644 --- a/java/ql/lib/semmle/code/java/PrintAst.qll +++ b/java/ql/lib/semmle/code/java/PrintAst.qll @@ -677,10 +677,10 @@ final class AnnotationsNode extends PrintAstNode, TAnnotationsNode { override ElementNode getChild(int childIndex) { result.getElement() = - rank[childIndex](Element e, string file, int line, int column | - e = ann.getAnAnnotation() and locationSortKeys(e, file, line, column) + rank[childIndex](Element e, string file, int line, int column, string s | + e = ann.getAnAnnotation() and locationSortKeys(e, file, line, column) and s = e.toString() | - e order by file, line, column + e order by file, line, column, s ) } 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 18c14a30a4f..fc1d83bdc2b 100644 --- a/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected +++ b/java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected @@ -23,10 +23,18 @@ def.kt: # 0| [CompilationUnit] def # 0| 1: [Class] DefKt # 46| 2: [Method] fn +#-----| 1: (Annotations) +# 45| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 #-----| 2: (Generic Parameters) # 46| 0: [TypeVariable] T +# 46| 3: [TypeAccess] Unit #-----| 4: (Parameters) # 46| 0: [Parameter] a +#-----| -1: (Annotations) +# 46| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 +# 46| 0: [TypeAccess] Annot0k # 46| 5: [BlockStmt] { ... } # 47| 0: [ExprStmt] ; # 47| 0: [MethodAccess] println(...) @@ -43,43 +51,102 @@ def.kt: # 53| -1: [TypeAccess] int # 57| 0: [IntegerLiteral] 5 # 57| 4: [Method] getP +#-----| 1: (Annotations) +# 54| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 +# 57| 3: [TypeAccess] int # 57| 5: [BlockStmt] { ... } # 57| 0: [ReturnStmt] return ... -# 57| 0: [VarAccess] p -# 57| 4: [Method] setP +# 57| 0: [VarAccess] DefKt.p +# 57| -1: [TypeAccess] DefKt +# 57| 5: [Method] setP +#-----| 1: (Annotations) +# 55| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 +# 57| 3: [TypeAccess] Unit #-----| 4: (Parameters) # 57| 0: [Parameter] +# 57| 0: [TypeAccess] int # 57| 5: [BlockStmt] { ... } # 57| 0: [ExprStmt] ; # 57| 0: [AssignExpr] ...=... -# 57| 0: [VarAccess] p +# 57| 0: [VarAccess] DefKt.p +# 57| -1: [TypeAccess] DefKt # 57| 1: [VarAccess] # 59| 6: [ExtensionMethod] myExtension +# 59| 3: [TypeAccess] Unit #-----| 4: (Parameters) # 59| 0: [Parameter] +#-----| -1: (Annotations) +# 59| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 +# 59| 0: [TypeAccess] String # 59| 5: [BlockStmt] { ... } # 5| 2: [Interface] Annot0k #-----| -3: (Annotations) -# 5| 5: [Annotation] Target +# 0| 1: [Annotation] Retention +# 0| 1: [VarAccess] RetentionPolicy.RUNTIME +# 0| -1: [TypeAccess] RetentionPolicy +# 0| 2: [Annotation] Target # 0| 1: [ArrayInit] {...} -# 0| 1: [VarAccess] CLASS -# 0| 1: [VarAccess] ANNOTATION_CLASS -# 0| 1: [VarAccess] TYPE_PARAMETER -# 0| 1: [VarAccess] PROPERTY -# 0| 1: [VarAccess] FIELD -# 0| 1: [VarAccess] LOCAL_VARIABLE -# 0| 1: [VarAccess] VALUE_PARAMETER -# 0| 1: [VarAccess] CONSTRUCTOR -# 0| 1: [VarAccess] FUNCTION -# 0| 1: [VarAccess] PROPERTY_GETTER -# 0| 1: [VarAccess] PROPERTY_SETTER -# 0| 1: [VarAccess] TYPE -# 0| 1: [VarAccess] FILE -# 0| 1: [VarAccess] TYPEALIAS +# 0| 1: [VarAccess] ElementType.TYPE +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.FIELD +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.METHOD +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.PARAMETER +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.CONSTRUCTOR +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.LOCAL_VARIABLE +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.ANNOTATION_TYPE +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.TYPE_PARAMETER +# 0| -1: [TypeAccess] ElementType +# 0| 1: [VarAccess] ElementType.TYPE_USE +# 0| -1: [TypeAccess] ElementType +# 5| 3: [Annotation] Target +# 0| 1: [ArrayInit] {...} +# 0| 1: [VarAccess] AnnotationTarget.CLASS +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.ANNOTATION_CLASS +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.TYPE_PARAMETER +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.PROPERTY +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.FIELD +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.LOCAL_VARIABLE +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.VALUE_PARAMETER +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.CONSTRUCTOR +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.FUNCTION +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.PROPERTY_GETTER +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.PROPERTY_SETTER +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.TYPE +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.FILE +# 0| -1: [TypeAccess] AnnotationTarget +# 0| 1: [VarAccess] AnnotationTarget.TYPEALIAS +# 0| -1: [TypeAccess] AnnotationTarget # 21| 1: [Method] a +#-----| 1: (Annotations) +# 21| 1: [Annotation] JvmName +# 0| 1: [StringLiteral] "a" # 23| 3: [Interface] Annot1k #-----| -3: (Annotations) -# 23| 4: [Annotation] Annot0k +# 0| 1: [Annotation] Retention +# 0| 1: [VarAccess] RetentionPolicy.RUNTIME +# 0| -1: [TypeAccess] RetentionPolicy +# 23| 2: [Annotation] Annot0k # 0| 1: [IntegerLiteral] 0 # 25| 1: [Method] a # 26| 2: [Method] b @@ -93,33 +160,57 @@ def.kt: # 33| 0: [SuperConstructorInvocationStmt] super(...) # 33| 1: [BlockStmt] { ... } # 34| 5: [Class] Y -# 0| 2: [Method] values # 0| 2: [Method] valueOf +# 0| 3: [TypeAccess] Y #-----| 4: (Parameters) # 0| 0: [Parameter] value +# 0| 0: [TypeAccess] String +# 0| 3: [Method] values +# 0| 3: [TypeAccess] Y[] +# 0| 0: [TypeAccess] Y # 34| 4: [Constructor] Y # 34| 5: [BlockStmt] { ... } # 34| 0: [ExprStmt] ; # 34| 0: [ClassInstanceExpr] new Enum(...) -# 34| -3: [TypeAccess] Unit +# 34| -3: [TypeAccess] Enum # 34| 0: [TypeAccess] Y +# 34| 0: [NullLiteral] null +# 34| 1: [IntegerLiteral] 0 # 34| 1: [BlockStmt] { ... } +# 35| 5: [FieldDeclaration] Y A; +# 35| -1: [TypeAccess] Y +# 35| 0: [ClassInstanceExpr] new Y(...) +# 35| -3: [TypeAccess] Y +# 35| 6: [FieldDeclaration] Y B; +# 35| -1: [TypeAccess] Y +# 35| 0: [ClassInstanceExpr] new Y(...) +# 35| -3: [TypeAccess] Y +# 35| 7: [FieldDeclaration] Y C; +# 35| -1: [TypeAccess] Y +# 35| 0: [ClassInstanceExpr] new Y(...) +# 35| -3: [TypeAccess] Y # 38| 6: [Class] Z #-----| -3: (Annotations) # 38| 1: [Annotation] Annot0k # 0| 1: [IntegerLiteral] 1 # 39| 2: [Annotation] Annot1k # 0| 1: [IntegerLiteral] 2 -# 0| 1: [StringLiteral] ab +# 0| 1: [StringLiteral] "ab" # 0| 1: [TypeLiteral] X.class # 0| 0: [TypeAccess] X -# 0| 1: [VarAccess] B +# 0| 1: [VarAccess] Y.B +# 0| -1: [TypeAccess] Y # 0| 1: [ArrayInit] {...} -# 0| 1: [VarAccess] C -# 0| 1: [VarAccess] A +# 0| 1: [VarAccess] Y.C +# 0| -1: [TypeAccess] Y +# 0| 1: [VarAccess] Y.A +# 0| -1: [TypeAccess] Y # 0| 1: [Annotation] Annot0k # 0| 1: [IntegerLiteral] 1 # 42| 1: [Constructor] Z +#-----| 1: (Annotations) +# 41| 1: [Annotation] Annot0k +# 0| 1: [IntegerLiteral] 0 # 41| 5: [BlockStmt] { ... } # 42| 0: [SuperConstructorInvocationStmt] super(...) # 42| 1: [BlockStmt] { ... } diff --git a/java/ql/test/kotlin/library-tests/annotation_classes/classes.expected b/java/ql/test/kotlin/library-tests/annotation_classes/classes.expected index 9ef212e7840..b04dd33d2e7 100644 --- a/java/ql/test/kotlin/library-tests/annotation_classes/classes.expected +++ b/java/ql/test/kotlin/library-tests/annotation_classes/classes.expected @@ -14,12 +14,17 @@ annotationDeclarations | def.kt:23:1:31:1 | Annot1k | def.kt:29:5:29:32 | e | | def.kt:23:1:31:1 | Annot1k | def.kt:30:5:30:31 | f | annotations -| annotation_classes.testproj/test.class.files/Annot1k.class:0:0:0:0 | Annot0k | def.kt:23:1:31:1 | Annot1k | def.kt:5:1:21:60 | Annot0k | | def.kt:0:0:0:0 | Annot0k | def.kt:39:1:39:40 | Annot1k | def.kt:5:1:21:60 | Annot0k | | def.kt:23:1:23:8 | Annot0k | def.kt:23:1:31:1 | Annot1k | def.kt:5:1:21:60 | Annot0k | | def.kt:38:1:38:17 | Annot0k | def.kt:38:1:43:1 | Z | def.kt:5:1:21:60 | Annot0k | | def.kt:39:1:39:40 | Annot1k | def.kt:38:1:43:1 | Z | def.kt:23:1:31:1 | Annot1k | +| def.kt:41:5:41:12 | Annot0k | def.kt:42:5:42:19 | Z | def.kt:5:1:21:60 | Annot0k | +| def.kt:45:1:45:8 | Annot0k | def.kt:46:1:51:1 | fn | def.kt:5:1:21:60 | Annot0k | +| def.kt:46:21:46:28 | Annot0k | def.kt:46:21:46:39 | a | def.kt:5:1:21:60 | Annot0k | +| def.kt:54:1:54:12 | Annot0k | def.kt:57:1:57:23 | getP | def.kt:5:1:21:60 | Annot0k | +| def.kt:55:1:55:12 | Annot0k | def.kt:57:1:57:23 | setP | def.kt:5:1:21:60 | Annot0k | | def.kt:56:1:56:14 | Annot0k | def.kt:53:1:57:23 | p | def.kt:5:1:21:60 | Annot0k | +| def.kt:59:5:59:21 | Annot0k | def.kt:59:5:59:28 | | def.kt:5:1:21:60 | Annot0k | | use.java:10:5:10:21 | Annot0j | use.java:14:18:14:18 | Z | Annot0j.java:1:19:1:25 | Annot0j | | use.java:11:5:11:90 | Annot1j | use.java:14:18:14:18 | Z | Annot1j.java:1:19:1:25 | Annot1j | | use.java:11:73:11:89 | Annot0j | use.java:11:5:11:90 | Annot1j | Annot0j.java:1:19:1:25 | Annot0j | @@ -28,16 +33,26 @@ annotations | use.java:13:73:13:87 | Annot0k | use.java:13:5:13:88 | Annot1k | def.kt:5:1:21:60 | Annot0k | annotationValues | def.kt:0:0:0:0 | Annot0k | def.kt:0:0:0:0 | 1 | +| def.kt:0:0:0:0 | Retention | def.kt:0:0:0:0 | RetentionPolicy.RUNTIME | +| def.kt:0:0:0:0 | Retention | def.kt:0:0:0:0 | RetentionPolicy.RUNTIME | +| def.kt:0:0:0:0 | Target | def.kt:0:0:0:0 | {...} | | def.kt:5:1:20:1 | Target | def.kt:0:0:0:0 | {...} | +| def.kt:21:26:21:42 | JvmName | def.kt:0:0:0:0 | "a" | | def.kt:23:1:23:8 | Annot0k | def.kt:0:0:0:0 | 0 | | def.kt:38:1:38:17 | Annot0k | def.kt:0:0:0:0 | 1 | | def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | 2 | +| def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | "ab" | | def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | Annot0k | -| def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | B | | def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | X.class | -| def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | ab | +| def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | Y.B | | def.kt:39:1:39:40 | Annot1k | def.kt:0:0:0:0 | {...} | +| def.kt:41:5:41:12 | Annot0k | def.kt:0:0:0:0 | 0 | +| def.kt:45:1:45:8 | Annot0k | def.kt:0:0:0:0 | 0 | +| def.kt:46:21:46:28 | Annot0k | def.kt:0:0:0:0 | 0 | +| def.kt:54:1:54:12 | Annot0k | def.kt:0:0:0:0 | 0 | +| def.kt:55:1:55:12 | Annot0k | def.kt:0:0:0:0 | 0 | | def.kt:56:1:56:14 | Annot0k | def.kt:0:0:0:0 | 0 | +| def.kt:59:5:59:21 | Annot0k | def.kt:0:0:0:0 | 0 | | use.java:10:5:10:21 | Annot0j | use.java:10:20:10:20 | 1 | | use.java:11:5:11:90 | Annot1j | use.java:11:18:11:18 | 1 | | use.java:11:5:11:90 | Annot1j | use.java:11:25:11:28 | "ac" | diff --git a/java/ql/test/kotlin/library-tests/annotation_classes/classes.ql b/java/ql/test/kotlin/library-tests/annotation_classes/classes.ql index fc3db6f0c02..936165cf023 100644 --- a/java/ql/test/kotlin/library-tests/annotation_classes/classes.ql +++ b/java/ql/test/kotlin/library-tests/annotation_classes/classes.ql @@ -16,5 +16,5 @@ query predicate annotations(Annotation a, Element e, AnnotationType at) { } query predicate annotationValues(Annotation a, Expr v) { - a.getAValue() = v and v.getFile().isSourceFile() + a.getValue(_) = v and v.getFile().isSourceFile() } diff --git a/java/ql/test/kotlin/library-tests/annotation_classes/def.kt b/java/ql/test/kotlin/library-tests/annotation_classes/def.kt index f499d1026fc..2fb107c1263 100644 --- a/java/ql/test/kotlin/library-tests/annotation_classes/def.kt +++ b/java/ql/test/kotlin/library-tests/annotation_classes/def.kt @@ -60,3 +60,6 @@ fun @receiver:Annot0k String.myExtension() { } @Annot0k typealias AAA = Z + +// Diagnostic Matches: Incomplete annotation: @kotlin.annotation.Target(%) +// Diagnostic Matches: Incomplete annotation: @kotlin.jvm.JvmName(%)