You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/632150601>`__. Several of the LGTM.com demo projects use the ``@SuppressWarnings`` annotation. Looking at the ``value``\ s of the annotation element returned by the query, we can see that the *apache/activemq* project uses the ``"rawtypes"`` value described above.
52
+
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/1775658606775222283/>`__. Several of the LGTM.com demo projects use the ``@SuppressWarnings`` annotation. Looking at the ``value``\ s of the annotation element returned by the query, we can see that the *apache/activemq* project uses the ``"rawtypes"`` value described above.
53
53
54
54
As another example, this query finds all annotation types that only have a single annotation element, which has name ``value``:
55
55
@@ -64,7 +64,7 @@ As another example, this query finds all annotation types that only have a singl
64
64
)
65
65
select anntp
66
66
67
-
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/669220001>`__.
67
+
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/2145264152490258283/>`__.
@@ -122,7 +122,7 @@ This makes it very easy to write our query for finding methods that override ano
122
122
not overriding.getAnAnnotation() instanceof OverrideAnnotation
123
123
select overriding, "Method overrides another method, but does not have an @Override annotation."
124
124
125
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1505752756202/>`__. In practice, this query may yield many results from compiled library code, which aren't very interesting. It's therefore a good idea to add another conjunct ``overriding.fromSource()`` to restrict the result to only report methods for which source code is available.
125
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/7419756266089837339/>`__. In practice, this query may yield many results from compiled library code, which aren't very interesting. It's therefore a good idea to add another conjunct ``overriding.fromSource()`` to restrict the result to only report methods for which source code is available.
126
126
127
127
Example: Finding calls to deprecated methods
128
128
--------------------------------------------
@@ -192,13 +192,13 @@ For instance, consider this slightly updated example:
192
192
.. code-block:: java
193
193
194
194
classA {
195
-
@Deprecatedvoidm() {}
195
+
@Deprecatedvoidm() {}
196
196
197
-
@Deprecatedvoidn() {
198
-
m();
199
-
}
197
+
@Deprecatedvoidn() {
198
+
m();
199
+
}
200
200
201
-
@SuppressWarnings("deprecated")
201
+
@SuppressWarnings("deprecated")
202
202
voidr() {
203
203
m();
204
204
}
@@ -235,7 +235,7 @@ Now we can extend our query to filter out calls in methods carrying a ``Suppress
235
235
and not call.getCaller().getAnAnnotation() instanceof SuppressDeprecationWarningAnnotation
236
236
select call, "This call invokes a deprecated method."
237
237
238
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/665760001>`__. It's fairly common for projects to contain calls to methods that appear to be deprecated.
238
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8706367340403790260/>`__. It's fairly common for projects to contain calls to methods that appear to be deprecated.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/java/call-graph.rst
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ We can use the ``Callable`` class to write a query that finds methods that are n
78
78
where not exists(Callable caller | caller.polyCalls(callee))
79
79
select callee
80
80
81
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/665280012/>`__. This simple query typically returns a large number of results.
81
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8376915232270534450/>`__. This simple query typically returns a large number of results.
82
82
83
83
.. pull-quote::
84
84
@@ -97,7 +97,7 @@ Running this query on a typical Java project results in lots of hits in the Java
97
97
callee.getCompilationUnit().fromSource()
98
98
select callee, "Not called."
99
99
100
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/668510015/>`__. This change reduces the number of results returned for most projects.
100
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8711624074465690976/>`__. This change reduces the number of results returned for most projects.
101
101
102
102
We might also notice several unused methods with the somewhat strange name ``<clinit>``: these are class initializers; while they are not explicitly called anywhere in the code, they are called implicitly whenever the surrounding class is loaded. Hence it makes sense to exclude them from our query. While we are at it, we can also exclude finalizers, which are similarly invoked implicitly:
103
103
@@ -111,7 +111,7 @@ We might also notice several unused methods with the somewhat strange name ``<cl
111
111
not callee.hasName("<clinit>") and not callee.hasName("finalize")
112
112
select callee, "Not called."
113
113
114
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/672230002/>`__. This also reduces the number of results returned by most projects.
114
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/925473733866047471/>`__. This also reduces the number of results returned by most projects.
115
115
116
116
We may also want to exclude public methods from our query, since they may be external API entry points:
117
117
@@ -126,7 +126,7 @@ We may also want to exclude public methods from our query, since they may be ext
126
126
not callee.isPublic()
127
127
select callee, "Not called."
128
128
129
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/667290016/>`__. This should have a more noticeable effect on the number of results returned.
129
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6284320987237954610/>`__. This should have a more noticeable effect on the number of results returned.
130
130
131
131
A further special case is non-public default constructors: in the singleton pattern, for example, a class is provided with private empty default constructor to prevent it from being instantiated. Since the very purpose of such constructors is their not being called, they should not be flagged up:
132
132
@@ -142,7 +142,7 @@ A further special case is non-public default constructors: in the singleton patt
142
142
not callee.(Constructor).getNumberOfParameters() = 0
143
143
select callee, "Not called."
144
144
145
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/673060008/>`__. This change has a large effect on the results for some projects but little effect on the results for others. Use of this pattern varies widely between different projects.
145
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2625028545869146918/>`__. This change has a large effect on the results for some projects but little effect on the results for others. Use of this pattern varies widely between different projects.
146
146
147
147
Finally, on many Java projects there are methods that are invoked indirectly by reflection. So, while there are no calls invoking these methods, they are, in fact, used. It is in general very hard to identify such methods. A very common special case, however, is JUnit test methods, which are reflectively invoked by a test runner. The CodeQL library for Java has support for recognizing test classes of JUnit and other testing frameworks, which we can employ to filter out methods defined in such classes:
148
148
@@ -159,7 +159,7 @@ Finally, on many Java projects there are methods that are invoked indirectly by
159
159
not callee.getDeclaringType() instanceof TestClass
160
160
select callee, "Not called."
161
161
162
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/665760002/>`__. This should give a further reduction in the number of results returned.
162
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2055862421970264112/>`__. This should give a further reduction in the number of results returned.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/java/expressions-statements.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ We'll start by writing a query that finds less-than expressions (CodeQL class ``
42
42
expr.getRightOperand().getType().hasName("long")
43
43
select expr
44
44
45
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/672320008/>`__. This query usually finds results on most projects.
45
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/490866529746563234/>`__. This query usually finds results on most projects.
46
46
47
47
Notice that we use the predicate ``getType`` (available on all subclasses of ``Expr``) to determine the type of the operands. Types, in turn, define the ``hasName`` predicate, which allows us to identify the primitive types ``int`` and ``long``. As it stands, this query finds *all* less-than expressions comparing ``int`` and ``long``, but in fact we are only interested in comparisons that are part of a loop condition. Also, we want to filter out comparisons where either operand is constant, since these are less likely to be real bugs. The revised query looks like this:
48
48
@@ -57,7 +57,7 @@ Notice that we use the predicate ``getType`` (available on all subclasses of ``E
57
57
not expr.getAnOperand().isCompileTimeConstant()
58
58
select expr
59
59
60
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/690010001/>`__. Notice that fewer results are found.
60
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/4315986481180063825/>`__. Notice that fewer results are found.
61
61
62
62
The class ``LoopStmt`` is a common superclass of all loops, including, in particular, ``for`` loops as in our example above. While different kinds of loops have different syntax, they all have a loop condition, which can be accessed through predicate ``getCondition``. We use the reflexive transitive closure operator ``*`` applied to the ``getAChildExpr`` predicate to express the requirement that ``expr`` should be nested inside the loop condition. In particular, it can be the loop condition itself.
63
63
@@ -120,7 +120,7 @@ Now we rewrite our query to make use of these new classes:
120
120
not expr.getAnOperand().isCompileTimeConstant()
121
121
select expr
122
122
123
-
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/1951710018/lang:java/>`__.
123
+
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/506868054626167462/>`__.
0 commit comments