File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
codeql-custom-queries-java/queries/recommendations Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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"
You can’t perform that action at this time.
0 commit comments