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
Copy file name to clipboardExpand all lines: docs/language/learn-ql/java/annotations.rst
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
-
Tutorial: Annotations
2
-
=====================
3
-
4
-
Overview
5
-
--------
1
+
Annotations in Java
2
+
===================
6
3
7
4
CodeQL databases of Java projects contain information about all annotations attached to program elements.
8
5
9
-
Annotations are represented by the following CodeQL classes:
6
+
About working with annotations
7
+
------------------------------
8
+
9
+
Annotations are represented by these CodeQL classes:
10
10
11
11
- The class ``Annotatable`` represents all entities that may have an annotation attached to them (that is, packages, reference types, fields, methods, and local variables).
12
12
- The class ``AnnotationType`` represents a Java annotation type, such as ``java.lang.Override``; annotation types are interfaces.
13
13
- The class ``AnnotationElement`` represents an annotation element, that is, a member of an annotation type.
14
14
- The class ``Annotation`` represents an annotation such as ``@Override``; annotation values can be accessed through member predicate ``getValue``.
15
15
16
-
As an example, recall that the Java standard library defines an annotation ``SuppressWarnings`` that instructs the compiler not to emit certain kinds of warnings. It is defined as follows:
16
+
For example, the Java standard library defines an annotation ``SuppressWarnings`` that instructs the compiler not to emit certain kinds of warnings:
17
17
18
18
.. code-block:: java
19
19
@@ -25,7 +25,7 @@ As an example, recall that the Java standard library defines an annotation ``Sup
25
25
26
26
``SuppressWarnings`` is represented as an ``AnnotationType``, with ``value`` as its only ``AnnotationElement``.
27
27
28
-
A typical usage of ``SuppressWarnings`` would be the following annotation to prevent a warning about using raw types:
28
+
A typical usage of ``SuppressWarnings`` would be this annotation for preventing a warning about using raw types:
29
29
30
30
.. code-block:: java
31
31
@@ -37,7 +37,7 @@ A typical usage of ``SuppressWarnings`` would be the following annotation to pre
37
37
38
38
The expression ``@SuppressWarnings("rawtypes")`` is represented as an ``Annotation``. The string literal ``"rawtypes"`` is used to initialize the annotation element ``value``, and its value can be extracted from the annotation by means of the ``getValue`` predicate.
39
39
40
-
We could then write the following query to find all ``@SuppressWarnings`` annotations attached to constructors, and return both the annotation itself and the value of its ``value`` element:
40
+
We could then write this query to find all ``@SuppressWarnings`` annotations attached to constructors, and return both the annotation itself and the value of its ``value`` element:
41
41
42
42
.. code-block:: ql
43
43
@@ -69,9 +69,9 @@ As another example, this query finds all annotation types that only have a singl
In newer versions of Java, it is recommended (though not required) to annotate methods that override another method with an ``@Override`` annotation. These annotations, which are checked by the compiler, serve as documentation, and also help you avoid accidental overloading where overriding was intended.
72
+
In newer versions of Java, it's recommended (though not required) that you annotate methods that override another method with an ``@Override`` annotation. These annotations, which are checked by the compiler, serve as documentation, and also help you avoid accidental overloading where overriding was intended.
73
73
74
-
For example, consider the following example program:
74
+
For example, consider this example program:
75
75
76
76
.. code-block:: java
77
77
@@ -89,9 +89,9 @@ For example, consider the following example program:
89
89
90
90
Here, both ``Sub1.m`` and ``Sub2.m`` override ``Super.m``, but only ``Sub1.m`` is annotated with ``@Override``.
91
91
92
-
We will now develop a query for finding methods like ``Sub2.m`` that should be annotated with ``@Override``, but are not.
92
+
We'll now develop a query for finding methods like ``Sub2.m`` that should be annotated with ``@Override``, but are not.
93
93
94
-
As a first step, let us write a query that finds all ``@Override`` annotations. Annotations are expressions, so their type can be accessed using ``getType``. Annotation types, on the other hand, are interfaces, so their qualified name can be queried using ``hasQualifiedName``. Therefore we can implement the query as follows:
94
+
As a first step, let's write a query that finds all ``@Override`` annotations. Annotations are expressions, so their type can be accessed using ``getType``. Annotation types, on the other hand, are interfaces, so their qualified name can be queried using ``hasQualifiedName``. Therefore we can implement the query like this:
95
95
96
96
.. code-block:: ql
97
97
@@ -111,7 +111,7 @@ As always, it is a good idea to try this query on a CodeQL database for a Java p
111
111
}
112
112
}
113
113
114
-
This makes it very easy to write our query for finding methods that override another method, but do not have an ``@Override`` annotation: we use predicate ``overrides`` to find out whether one method overrides another, and predicate ``getAnAnnotation`` (available on any ``Annotatable``) to retrieve some annotation.
114
+
This makes it very easy to write our query for finding methods that override another method, but don't have an ``@Override`` annotation: we use predicate ``overrides`` to find out whether one method overrides another, and predicate ``getAnAnnotation`` (available on any ``Annotatable``) to retrieve some annotation.
115
115
116
116
.. code-block:: ql
117
117
@@ -122,14 +122,14 @@ 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 <https://lgtm.com/query/1505752756202/>`__. In practice, this query may yield many results from compiled library code, which are not very interesting. Therefore, it is 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 <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.
126
126
127
127
Example: Finding calls to deprecated methods
128
128
--------------------------------------------
129
129
130
130
As another example, we can write a query that finds calls to methods marked with a ``@Deprecated`` annotation.
131
131
132
-
For example, consider the following example program:
132
+
For example, consider this example program:
133
133
134
134
.. code-block:: java
135
135
@@ -147,7 +147,7 @@ For example, consider the following example program:
147
147
148
148
Here, both ``A.m`` and ``A.n`` are marked as deprecated. Methods ``n`` and ``r`` both call ``m``, but note that ``n`` itself is deprecated, so we probably should not warn about this call.
149
149
150
-
Like in the previous example, we start by defining a class for representing ``@Deprecated`` annotations:
150
+
As in the previous example, we'll start by defining a class for representing ``@Deprecated`` annotations:
151
151
152
152
.. code-block:: ql
153
153
@@ -167,7 +167,7 @@ Now we can define a class for representing deprecated methods:
167
167
}
168
168
}
169
169
170
-
Finally, we use these classes to find calls to deprecated methods, excluding calls that themselves appear in deprecated methods (see :doc:`Tutorial: Navigating the call graph <call-graph>` for more information on class ``Call``):
170
+
Finally, we use these classes to find calls to deprecated methods, excluding calls that themselves appear in deprecated methods:
171
171
172
172
.. code-block:: ql
173
173
@@ -178,7 +178,9 @@ Finally, we use these classes to find calls to deprecated methods, excluding cal
178
178
and not call.getCaller() instanceof DeprecatedMethod
179
179
select call, "This call invokes a deprecated method."
180
180
181
-
On our example, this query flags the call to ``A.m`` in ``A.r``, but not the one in ``A.n``.
181
+
In our example, this query flags the call to ``A.m`` in ``A.r``, but not the one in ``A.n``.
182
+
183
+
For more information about the class ``Call``, see :doc:`Navigating the call graph <call-graph>`.
182
184
183
185
Improvements
184
186
~~~~~~~~~~~~
@@ -235,9 +237,9 @@ Now we can extend our query to filter out calls in methods carrying a ``Suppress
235
237
236
238
➤ `See this in the query console <https://lgtm.com/query/665760001>`__. It's fairly common for projects to contain calls to methods that appear to be deprecated.
237
239
238
-
What next?
239
-
----------
240
+
Further reading
241
+
---------------
240
242
241
-
- Take a look at some of the other tutorials: :doc:`Tutorial: Javadoc <javadoc>` and :doc:`Tutorial: Working with source locations <source-locations>`.
242
-
- Find out how specific classes in the AST are represented in the standard library for Java: :doc:`AST class reference<ast-class-reference>`.
243
+
- Take a look at some of the other articles in this section: :doc:`Javadoc <javadoc>` and :doc:`Working with source locations <source-locations>`.
244
+
- Find out how specific classes in the AST are represented in the standard library for Java: :doc:`Classes for working with Java code<ast-class-reference>`.
243
245
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/java/call-graph.rst
+14-12Lines changed: 14 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,10 @@
1
-
Tutorial: Navigating the call graph
2
-
===================================
1
+
Navigating the call graph
2
+
=========================
3
3
4
-
Call graph API
5
-
--------------
4
+
CodeQL has classes for identifying code that calls other code, and code that can be called from elsewhere. This allows you to find, for example, methods that are never used.
5
+
6
+
Call graph classes
7
+
------------------
6
8
7
9
The CodeQL library for Java provides two abstract classes for representing a program's call graph: ``Callable`` and ``Call``. The former is simply the common superclass of ``Method`` and ``Constructor``, the latter is a common superclass of ``MethodAccess``, ``ClassInstanceExpression``, ``ThisConstructorInvocationStmt`` and ``SuperConstructorInvocationStmt``. Simply put, a ``Callable`` is something that can be invoked, and a ``Call`` is something that invokes a ``Callable``.
8
10
@@ -56,7 +58,7 @@ Class ``Call`` provides two call graph navigation predicates:
56
58
57
59
For instance, in our example ``getCallee`` of the second call in ``Client.main`` would return ``Super.getX``. At runtime, though, this call would actually invoke ``Sub.getX``.
58
60
59
-
Class ``Callable`` defines a large number of member predicates; for our purposes, the two most important ones are as follows:
61
+
Class ``Callable`` defines a large number of member predicates; for our purposes, the two most important ones are:
60
62
61
63
- ``calls(Callable target)`` succeeds if this callable contains a call whose callee is ``target``.
62
64
- ``polyCalls(Callable target)`` succeeds if this callable may call ``target`` at runtime; this is the case if it contains a call whose callee is either ``target`` or a method that ``target`` overrides.
@@ -66,7 +68,7 @@ In our example, ``Client.main`` calls the constructor ``Sub(int)`` and the metho
66
68
Example: Finding unused methods
67
69
-------------------------------
68
70
69
-
Given this API, we can easily write a query that finds methods that are not called by any other method:
71
+
We can use the ``Callable`` class to write a query that finds methods that are not called by any other method:
70
72
71
73
.. code-block:: ql
72
74
@@ -84,7 +86,7 @@ Given this API, we can easily write a query that finds methods that are not call
84
86
85
87
We have to use ``polyCalls`` instead of ``calls`` here: we want to be reasonably sure that ``callee`` is not called, either directly or via overriding.
86
88
87
-
Running this query on a typical Java project results in lots of hits in the Java standard library. This makes sense, since no single client program uses every method of the standard library. More generally, we may want to exclude methods and constructors from compiled libraries. We can use the predicate ``fromSource`` to check whether a compilation unit is a source file, and refine our query as follows:
89
+
Running this query on a typical Java project results in lots of hits in the Java standard library. This makes sense, since no single client program uses every method of the standard library. More generally, we may want to exclude methods and constructors from compiled libraries. We can use the predicate ``fromSource`` to check whether a compilation unit is a source file, and refine our query:
88
90
89
91
.. code-block:: ql
90
92
@@ -142,7 +144,7 @@ A further special case is non-public default constructors: in the singleton patt
142
144
143
145
➤ `See this in the query console <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.
144
146
145
-
Finally, on many Java projects there are methods that are invoked indirectly by reflection. Thus, 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 QL Java library has support for recognizing test classes of JUnit and other testing frameworks, which we can employ to filter out methods defined in such classes:
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 QL Java library has support for recognizing test classes of JUnit and other testing frameworks, which we can employ to filter out methods defined in such classes:
146
148
147
149
.. code-block:: ql
148
150
@@ -159,9 +161,9 @@ Finally, on many Java projects there are methods that are invoked indirectly by
159
161
160
162
➤ `See this in the query console <https://lgtm.com/query/665760002/>`__. This should give a further reduction in the number of results returned.
161
163
162
-
What next?
163
-
----------
164
+
Further reading
165
+
---------------
164
166
165
-
- Find out how to query metadata and white space: :doc:`Tutorial: Annotations <annotations>`, :doc:`Tutorial: Javadoc <javadoc>`, and :doc:`Tutorial: Working with source locations <source-locations>`.
166
-
- Find out how specific classes in the AST are represented in the standard library for Java: :doc:`AST class reference<ast-class-reference>`.
167
+
- Find out how to query metadata and white space: :doc:`Annotations in Java <annotations>`, :doc:`Javadoc <javadoc>`, and :doc:`Working with source locations <source-locations>`.
168
+
- Find out how specific classes in the AST are represented in the standard library for Java: :doc:`Classes for working with Java code<ast-class-reference>`.
167
169
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
0 commit comments