Skip to content

Commit f69b6ee

Browse files
Marcono1234smowton
authored andcommitted
Java: Clarify that Annotatable predicates consider inherited annotations
Additionally changes `hasAnnotation()` to consider inherited annotations for consistency.
1 parent afb7462 commit f69b6ee

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

java/ql/lib/semmle/code/java/Annotation.qll

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,33 @@ private predicate sourceAnnotValue(Annotation a, Method m, Expr val) {
166166

167167
/** An abstract representation of language elements that can be annotated. */
168168
class Annotatable extends Element {
169-
/** Holds if this element has an annotation. */
170-
predicate hasAnnotation() { exists(Annotation a | a.getAnnotatedElement() = this) }
169+
/** Holds if this element has an annotation, including inherited annotations. */
170+
predicate hasAnnotation() { exists(getAnAnnotation()) }
171171

172-
/** Holds if this element has the specified annotation. */
172+
/** Holds if this element has a declared annotation, excluding inherited annotations. */
173+
predicate hasDeclaredAnnotation() { exists(getADeclaredAnnotation()) }
174+
175+
/**
176+
* Holds if this element has the specified annotation, including inherited
177+
* annotations.
178+
*/
173179
predicate hasAnnotation(string package, string name) {
174180
exists(AnnotationType at | at = this.getAnAnnotation().getType() |
175181
at.nestedName() = name and at.getPackage().getName() = package
176182
)
177183
}
178184

179-
/** Gets an annotation that applies to this element. */
185+
/**
186+
* Gets an annotation that applies to this element, including inherited annotations.
187+
*/
188+
// This predicate is overridden by Class to consider inherited annotations
180189
cached
181-
Annotation getAnAnnotation() { result.getAnnotatedElement() = this }
190+
Annotation getAnAnnotation() { result = getADeclaredAnnotation() }
191+
192+
/**
193+
* Gets an annotation that is declared on this element, excluding inherited annotations.
194+
*/
195+
Annotation getADeclaredAnnotation() { result.getAnnotatedElement() = this }
182196

183197
/**
184198
* Holds if this or any enclosing `Annotatable` has a `@SuppressWarnings("<category>")`

java/ql/lib/semmle/code/java/PrintAst.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private newtype TPrintAstNode =
120120
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
121121
} or
122122
TAnnotationsNode(Annotatable ann) {
123-
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
123+
shouldPrint(ann, _) and ann.hasDeclaredAnnotation() and not partOfAnnotation(ann)
124124
} or
125125
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
126126
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or

0 commit comments

Comments
 (0)