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
Open the various java sources and read the comments to have an understanding of various checks that can be done (or not)
9
+
by these tools.
10
+
11
+
:warning: It is NORMAL that your IDE shows errors in the source files if it supports some checks for null reference analysis.:warning:
12
+
13
+
The default javac compiler does not report any error however.
14
+
15
+
## Eclipse
16
+
Last version tested: eclipse 4.6.3.
5
17
6
18
The best analysis is performed using [external annotations](http://help.eclipse.org/neon/topic/org.eclipse.jdt.doc.user/tasks/task-using_external_null_annotations.htm?cp=1_3_9_2).
7
19
8
20
[This repository/project](https://github.com/sylvainlaurent/eclipse-external-annotations) contains a (non-exhaustive but growing) number of external annotations for usual classes (e.g. Map, List, some guava classes...).
9
21
10
-
## Inside eclipse IDE
22
+
### Inside eclipse IDE
23
+
24
+
To activate the null reference analysis in this example project, copy ``ide-settings/eclipse-no-npe-analysis/org.eclipse.jdt.core.prefs`` to the ``.settings/`` directory.
25
+
11
26
To automatically associate an "annotation path" with the "Maven Dependencies" and "JRE" libraries in eclipse build path:
12
27
- install eclipse-external-annotations-m2e-plugin from [this p2 repository](http://sylvainlaurent.github.io/eclipse-external-annotations/p2/).
13
-
- add a maven property `m2e.jdt.annotationpath` in your pom, as demonstrated in [with-external-annotations/pom.xml](with-external-annotations/pom.xml).
28
+
- add a maven property `m2e.jdt.annotationpath` in your pom, as demonstrated in [pom.xml](pom.xml#93).
14
29
- perform a full `Maven/Update project...` in eclipse.
15
30
16
31
Tip: place the property in a `m2e` profile activated only inside eclipse, not in the command-line (see below for command-line usage).
@@ -31,19 +46,14 @@ Using a source project for external annotations in the same eclipse workspace al
31
46
</profile>
32
47
```
33
48
34
-
##When running maven from the command-line
35
-
To perform null-analysis during a maven build, the jdt compiler must be used in place of the default javac, as demonstrated in the `not-m2e` maven profile of [with-external-annotations/pom.xml](with-external-annotations/pom.xml).
49
+
### Using eclipse jdt compiler with maven from the command-line
50
+
To perform the same null reference analysis as the eclipse IDE during a maven build, the jdt compiler must be used in place of the default javac, as demonstrated in the `jdt` maven profile of [pom.xml](pom.xml).
36
51
37
52
```xml
38
53
<profile>
39
-
<id>not-m2e</id>
40
-
<activation>
41
-
<property>
42
-
<name>!m2e.version</name>
43
-
</property>
44
-
</activation>
54
+
<id>jdt</id>
45
55
<properties>
46
-
<tycho-version>0.25.0</tycho-version>
56
+
<tycho-version>0.26.0</tycho-version>
47
57
</properties>
48
58
<repositories>
49
59
<repository>
@@ -82,7 +92,7 @@ To perform null-analysis during a maven build, the jdt compiler must be used in
Though the Checker Framework proposes many checks, we only consider its [Nullness checker](https://checkerframework.org/manual/#nullness-checker) for the purpose of this example project.
141
+
142
+
In this example project, the ``checker-framework`` maven profile allows to use this nullness checker:
143
+
144
+
```xml
145
+
<profile>
146
+
<id>checker-framework</id>
147
+
<dependencies>
148
+
<dependency>
149
+
<groupId>org.checkerframework</groupId>
150
+
<artifactId>checker</artifactId>
151
+
<version>${checker-framework.version}</version>
152
+
</dependency>
153
+
<dependency>
154
+
<groupId>org.checkerframework</groupId>
155
+
<artifactId>jdk8</artifactId>
156
+
<version>${checker-framework.version}</version>
157
+
</dependency>
158
+
</dependencies>
159
+
<build>
160
+
<plugins>
161
+
<plugin>
162
+
<artifactId>maven-compiler-plugin</artifactId>
163
+
<configuration>
164
+
<fork>true</fork>
165
+
<annotationProcessors>
166
+
<!-- Add all the checkers you want to enable here -->
[ERROR] /Users/slaurent/Developer/repos/null-pointer-analysis-examples/src/main/java/packageNotAnnotated/ClassInNotAnnotatedPackage.java:[10,13] error: [argument.type.incompatible] incompatible types in argument.
201
+
[ERROR] found : null
202
+
required: @Initialized @NonNull String
203
+
/Users/slaurent/Developer/repos/null-pointer-analysis-examples/src/main/java/packageNotAnnotated/ClassInNotAnnotatedPackage.java:[16,19] error: [return.type.incompatible] incompatible types in return.
204
+
[ERROR] found : null
205
+
required: @Initialized @NonNull String
206
+
/Users/slaurent/Developer/repos/null-pointer-analysis-examples/src/main/java/packageNonNull/ClassInAnnotatedPackage.java:[13,7] error: [argument.type.incompatible] incompatible types in argument.
207
+
[ERROR] found : null
208
+
required: @Initialized @NonNull String
209
+
/Users/slaurent/Developer/repos/null-pointer-analysis-examples/src/main/java/packageNonNull/ClassInAnnotatedPackage.java:[19,10] error: [return.type.incompatible] incompatible types in return.
210
+
[ERROR] found : null
211
+
required: @Initialized @NonNull String
212
+
/Users/slaurent/Developer/repos/null-pointer-analysis-examples/src/main/java/test/EverythingNonNullByDefault.java:[25,11] error: [initialization.fields.uninitialized] the constructor does not initialize fields: address
213
+
...
214
+
```
215
+
## Comparison
216
+
217
+
Feature | Eclipse IDE or jdt | Checker Framework | IntelliJ
IDE support | :white_check_mark: | :white_check_mark: using plugin | :white_check_mark:
220
+
Command line support | :white_check_mark: | :white_check_mark: | :red_circle:
221
+
Java 8 type annotations support | :white_check_mark: | :white_check_mark: | :red_circle:``List<@Nonnull String>`` is the same as ``List<String>``, no error for ``list.add(null)``.
External annotations support | :white_check_mark: using .eea files | :white_check_mark: using stubs files | :white_check_mark: using xml files
224
+
External annotations provided for common libraries | :red_circle: community effort with [lastnpe.org](http://lastnpe.org)| :white_check_mark: JDK, Guava | :white_check_mark: JDK
225
+
IDE support to create external annotations | :white_check_mark: | :warning: using command line tools | :white_check_mark:
226
+
Treat all types as @Nonnull by default, unless annotated wih @Nullable | :white_check_mark: using @NonNullByDefault for each package, allows to define the scope: field, parameters, return, generic types, etc... | :white_check_mark: by default, customizable with @DefaultQualifier | :white_check_mark: IDE setting :question: global?
227
+
@Polynull support | :red_circle: | :white_check_mark: using [@PolyNull](https://checkerframework.org/manual/#qualifier-polymorphism) | :white_check_mark: using [@Contract](https://www.jetbrains.com/help/idea/2017.1/contract-annotations.html), for instance @Contract("!null->!null;null->null")
0 commit comments