Skip to content

Commit 4ef2d15

Browse files
Marcono1234smowton
authored andcommitted
Java: Deprecate error-prone and rarely used annotation predicates
1 parent e3c1b96 commit 4ef2d15

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ class Annotation extends @annotation, Expr {
4545
}
4646

4747
/**
48+
* DEPRECATED: Getting the value of _any_ annotation element is error-prone because
49+
* it could lead to selecting the value of the wrong element by accident (for example
50+
* when an annotation type is extended in the future). Prefer the predicate `getValue(string)`
51+
* and explicitly specify the element name. Use `getValue(_)` if it is really desired to
52+
* get the value of any element.
53+
*
4854
* Gets a value of an annotation element. This includes default values in case
4955
* no explicit value is specified. For elements with an array value type this
5056
* might have an `ArrayInit` as result. To properly handle array values, prefer
5157
* the predicate `getAnArrayValue`.
5258
*/
53-
Expr getAValue() { filteredAnnotValue(this, _, result) }
59+
deprecated Expr getAValue() { filteredAnnotValue(this, _, result) }
5460

5561
/**
5662
* Gets the value of the annotation element with the specified `name`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ predicate depends(RefType t, RefType dep) {
7171
a.getAnnotatedElement().(Member).getDeclaringType() = t
7272
|
7373
usesType(a.getType(), dep) or
74-
usesType(a.getAValue().getType(), dep) or
74+
usesType(a.getValue(_).getType(), dep) or
7575
usesType(a.getAnArrayValue(_).getType(), dep)
7676
)
7777
or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ predicate numDepends(RefType t, RefType dep, int value) {
9090
|
9191
elem = a and usesType(a.getType(), dep)
9292
or
93-
elem = [a.getAValue(), a.getAnArrayValue(_)] and
93+
elem = [a.getValue(_), a.getAnArrayValue(_)] and
9494
elem.getFile().getExtension() = "java" and
9595
usesType(elem.(Expr).getType(), dep)
9696
)

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,31 @@ class SuppressWarningsAnnotation extends Annotation {
1919
SuppressWarningsAnnotation() { this.getType().hasQualifiedName("java.lang", "SuppressWarnings") }
2020

2121
/**
22-
* Gets the `StringLiteral` of a warning suppressed by this annotation. To get the name of a suppressed
23-
* warning, prefer `getASuppressedWarning()`. That predicate considers more cases because it does not
24-
* restrict results to `StringLiteral`.
22+
* DEPRECATED: This predicate restricts the results to `StringLiteral`; prefer `getASuppressedWarning()`
23+
* to get the name of a suppressed warning.
24+
*
25+
* Gets the `StringLiteral` of a warning suppressed by this annotation.
2526
*/
26-
StringLiteral getASuppressedWarningLiteral() { result = this.getAnArrayValue("value") }
27+
deprecated StringLiteral getASuppressedWarningLiteral() { result = this.getAnArrayValue("value") }
2728

2829
/** Gets the name of a warning suppressed by this annotation. */
29-
string getASuppressedWarning() {
30-
// Don't use getASuppressedWarningLiteral() because that restricts results to StringLiteral
31-
result = this.getAStringArrayValue("value")
32-
}
30+
string getASuppressedWarning() { result = this.getAStringArrayValue("value") }
3331
}
3432

3533
/** A `@Target` annotation. */
3634
class TargetAnnotation extends Annotation {
3735
TargetAnnotation() { this.getType().hasQualifiedName("java.lang.annotation", "Target") }
3836

3937
/**
38+
* DEPRECATED: Getting the field access expression is rarely useful. Use `getATargetElementType()`
39+
* to get the name of the target element.
40+
*
4041
* Gets a target expression within this annotation.
4142
*
4243
* For example, the field access `ElementType.FIELD` is a target expression in
4344
* `@Target({ElementType.FIELD, ElementType.METHOD})`.
4445
*/
45-
Expr getATargetExpression() { result = this.getAnArrayValue("value") }
46+
deprecated Expr getATargetExpression() { result = this.getAnArrayValue("value") }
4647

4748
/**
4849
* Gets the name of a target element type.

0 commit comments

Comments
 (0)