Skip to content

Commit 44b9773

Browse files
authored
Merge pull request #2866 from hubwriter/alistairs-docs-preparation-1
CodeQL migration: Java topics - change titles & add intros (2164)
2 parents a567dba + daf5a96 commit 44b9773

10 files changed

Lines changed: 164 additions & 144 deletions

docs/language/learn-ql/java/annotations.rst

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
Tutorial: Annotations
2-
=====================
3-
4-
Overview
5-
--------
1+
Annotations in Java
2+
===================
63

74
CodeQL databases of Java projects contain information about all annotations attached to program elements.
85

9-
Annotations are represented by the following CodeQL classes:
6+
About working with annotations
7+
------------------------------
8+
9+
Annotations are represented by these CodeQL classes:
1010

1111
- The class ``Annotatable`` represents all entities that may have an annotation attached to them (that is, packages, reference types, fields, methods, and local variables).
1212
- The class ``AnnotationType`` represents a Java annotation type, such as ``java.lang.Override``; annotation types are interfaces.
1313
- The class ``AnnotationElement`` represents an annotation element, that is, a member of an annotation type.
1414
- The class ``Annotation`` represents an annotation such as ``@Override``; annotation values can be accessed through member predicate ``getValue``.
1515

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:
1717

1818
.. code-block:: java
1919
@@ -25,7 +25,7 @@ As an example, recall that the Java standard library defines an annotation ``Sup
2525
2626
``SuppressWarnings`` is represented as an ``AnnotationType``, with ``value`` as its only ``AnnotationElement``.
2727

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:
2929

3030
.. code-block:: java
3131
@@ -37,7 +37,7 @@ A typical usage of ``SuppressWarnings`` would be the following annotation to pre
3737
3838
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.
3939

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:
4141

4242
.. code-block:: ql
4343
@@ -69,9 +69,9 @@ As another example, this query finds all annotation types that only have a singl
6969
Example: Finding missing ``@Override`` annotations
7070
--------------------------------------------------
7171

72-
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.
7373

74-
For example, consider the following example program:
74+
For example, consider this example program:
7575

7676
.. code-block:: java
7777
@@ -89,9 +89,9 @@ For example, consider the following example program:
8989
9090
Here, both ``Sub1.m`` and ``Sub2.m`` override ``Super.m``, but only ``Sub1.m`` is annotated with ``@Override``.
9191

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.
9393

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:
9595

9696
.. code-block:: ql
9797
@@ -111,7 +111,7 @@ As always, it is a good idea to try this query on a CodeQL database for a Java p
111111
}
112112
}
113113

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.
115115

116116
.. code-block:: ql
117117
@@ -122,14 +122,14 @@ This makes it very easy to write our query for finding methods that override ano
122122
not overriding.getAnAnnotation() instanceof OverrideAnnotation
123123
select overriding, "Method overrides another method, but does not have an @Override annotation."
124124
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.
126126

127127
Example: Finding calls to deprecated methods
128128
--------------------------------------------
129129

130130
As another example, we can write a query that finds calls to methods marked with a ``@Deprecated`` annotation.
131131

132-
For example, consider the following example program:
132+
For example, consider this example program:
133133

134134
.. code-block:: java
135135
@@ -147,7 +147,7 @@ For example, consider the following example program:
147147
148148
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.
149149

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:
151151

152152
.. code-block:: ql
153153
@@ -167,7 +167,7 @@ Now we can define a class for representing deprecated methods:
167167
}
168168
}
169169
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:
171171

172172
.. code-block:: ql
173173
@@ -178,7 +178,9 @@ Finally, we use these classes to find calls to deprecated methods, excluding cal
178178
and not call.getCaller() instanceof DeprecatedMethod
179179
select call, "This call invokes a deprecated method."
180180
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>`.
182184

183185
Improvements
184186
~~~~~~~~~~~~
@@ -235,9 +237,9 @@ Now we can extend our query to filter out calls in methods carrying a ``Suppress
235237
236238
➤ `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.
237239

238-
What next?
239-
----------
240+
Further reading
241+
---------------
240242

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>`.
243245
- 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>`__.

docs/language/learn-ql/java/ast-class-reference.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
AST class reference
2-
===================
1+
Classes for working with Java code
2+
==================================
3+
4+
CodeQL has a large selection of classes for working with Java statements and expressions.
35

46
.. _Expr: https://help.semmle.com/qldoc/java/semmle/code/java/Expr.qll/type.Expr$Expr.html
57
.. _Stmt: https://help.semmle.com/qldoc/java/semmle/code/java/Statement.qll/type.Statement$Stmt.html

docs/language/learn-ql/java/call-graph.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
Tutorial: Navigating the call graph
2-
===================================
1+
Navigating the call graph
2+
=========================
33

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+
------------------
68

79
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``.
810

@@ -56,7 +58,7 @@ Class ``Call`` provides two call graph navigation predicates:
5658

5759
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``.
5860

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:
6062

6163
- ``calls(Callable target)`` succeeds if this callable contains a call whose callee is ``target``.
6264
- ``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
6668
Example: Finding unused methods
6769
-------------------------------
6870

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:
7072

7173
.. code-block:: ql
7274
@@ -84,7 +86,7 @@ Given this API, we can easily write a query that finds methods that are not call
8486

8587
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.
8688

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:
8890

8991
.. code-block:: ql
9092
@@ -142,7 +144,7 @@ A further special case is non-public default constructors: in the singleton patt
142144
143145
➤ `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.
144146

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:
146148

147149
.. code-block:: ql
148150
@@ -159,9 +161,9 @@ Finally, on many Java projects there are methods that are invoked indirectly by
159161
160162
➤ `See this in the query console <https://lgtm.com/query/665760002/>`__. This should give a further reduction in the number of results returned.
161163

162-
What next?
163-
----------
164+
Further reading
165+
---------------
164166

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>`.
167169
- 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

Comments
 (0)