Skip to content

Commit 98cdeed

Browse files
committed
Add Class-getEnumConstants-instead-of-Enum-values.ql
1 parent b7ec207 commit 98cdeed

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Finds usage of `Class.getEnumConstants()` called on a type literal, for example
3+
* `MyEnum.class.getEnumConstants()`. Such code should be replaced with a call to
4+
* the synthetic `values()` method of the enum type, which behaves the same but is
5+
* more concise and possibly also slightly faster. For example `MyEnum.values()`.
6+
*
7+
* @kind problem
8+
*/
9+
10+
// Inspired by https://bugs.openjdk.org/browse/JDK-8273140
11+
12+
import java
13+
14+
class GetEnumConstantsMethod extends Method {
15+
GetEnumConstantsMethod() {
16+
getDeclaringType() instanceof TypeClass
17+
and hasName("getEnumConstants")
18+
}
19+
}
20+
21+
from MethodAccess getEnumConstantsCall, EnumType enumType
22+
where
23+
getEnumConstantsCall.getMethod() instanceof GetEnumConstantsMethod
24+
and getEnumConstantsCall.getQualifier().(TypeLiteral).getReferencedType() = enumType
25+
select getEnumConstantsCall, "Should use `" + enumType.getName() + ".values()` instead"

0 commit comments

Comments
 (0)