|
| 1 | +# null-pointer-analysis-examples |
| 2 | + |
| 3 | +Demonstrates capabilities of eclipse 4.6 for analysis of (potential) null references. |
| 4 | +Just open the java source files in eclipse and look at the comments. |
| 5 | + |
| 6 | +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 | + |
| 8 | +[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 | + |
| 10 | +To automatically associate an "annotation path" with the "Maven Dependencies" and "JRE" libraries in eclipse build path, install eclipse-external-annotations-m2e-plugin from [this p2 repository](http://sylvainlaurent.github.io/eclipse-external-annotations/p2/). |
| 11 | +Then add a maven property `m2e.jdt.annotationpath` in your pom, as demonstrated in [with-external-annotations/pom.xml](with-external-annotations/pom.xml). |
| 12 | +And finally perform a full `Maven/Update project...` in eclipse. |
| 13 | +Tip: place the property in a `m2e` profile activated only inside eclipse, not in the command-line (see below for command-line usage). |
| 14 | +Using a source project for external annotations in the same eclipse workspace allows to quickly add missing annotations directly from eclipse (by pressing Cmd-1 or Ctr-1 on the type of a method signature). |
| 15 | + |
| 16 | +```xml |
| 17 | + <profile> |
| 18 | + <id>m2e</id> |
| 19 | + <activation> |
| 20 | + <property> |
| 21 | + <name>m2e.version</name> |
| 22 | + </property> |
| 23 | + </activation> |
| 24 | + <properties> |
| 25 | + <!-- the following is effective if the eclipse-external-annotations-m2e-plugin is installed and the eclipse-external-annotations project is open in the same workspace --> |
| 26 | + <m2e.jdt.annotationpath>/eclipse-external-annotations/src/main/resources</m2e.jdt.annotationpath> |
| 27 | + </properties> |
| 28 | + </profile> |
| 29 | +``` |
| 30 | + |
| 31 | +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). |
| 32 | + |
| 33 | +```xml |
| 34 | + <profile> |
| 35 | + <id>not-m2e</id> |
| 36 | + <activation> |
| 37 | + <property> |
| 38 | + <name>!m2e.version</name> |
| 39 | + </property> |
| 40 | + </activation> |
| 41 | + <properties> |
| 42 | + <tycho-version>0.25.0</tycho-version> |
| 43 | + </properties> |
| 44 | + <repositories> |
| 45 | + <repository> |
| 46 | + <!-- just to retrieve snapshots of com.github.sylvainlaurent:null-pointer-analysis-examples. Useless if using versions released to maven central --> |
| 47 | + <id>ossrh-snapshots</id> |
| 48 | + <url>https://oss.sonatype.org/content/repositories/snapshots/</url> |
| 49 | + <snapshots> |
| 50 | + <enabled>true</enabled> |
| 51 | + <checksumPolicy>warn</checksumPolicy> |
| 52 | + </snapshots> |
| 53 | + </repository> |
| 54 | + </repositories> |
| 55 | + <dependencies> |
| 56 | + <dependency> |
| 57 | + <groupId>com.github.sylvainlaurent</groupId> |
| 58 | + <artifactId>eclipse-external-annotations</artifactId> |
| 59 | + <version>0.0.1-SNAPSHOT</version> |
| 60 | + <scope>provided</scope> |
| 61 | + </dependency> |
| 62 | + </dependencies> |
| 63 | + <build> |
| 64 | + <pluginManagement> |
| 65 | + <plugins> |
| 66 | + <plugin> |
| 67 | + <groupId>org.apache.maven.plugins</groupId> |
| 68 | + <artifactId>maven-compiler-plugin</artifactId> |
| 69 | + <executions> |
| 70 | + <execution> |
| 71 | + <!-- check nullability using jdt compiler, only for compile, not for testCompile --> |
| 72 | + <id>compile</id> |
| 73 | + <phase>compile</phase> |
| 74 | + <goals> |
| 75 | + <goal>compile</goal> |
| 76 | + </goals> |
| 77 | + <configuration> |
| 78 | + <compilerId>jdt</compilerId> |
| 79 | + <compilerArgs> |
| 80 | + <arg>-properties</arg> |
| 81 | + <arg>${project.basedir}/.settings/org.eclipse.jdt.core.prefs</arg> |
| 82 | + <arg>-annotationpath</arg> |
| 83 | + <arg>CLASSPATH</arg> |
| 84 | + </compilerArgs> |
| 85 | + </configuration> |
| 86 | + </execution> |
| 87 | + </executions> |
| 88 | + <dependencies> |
| 89 | + <dependency> |
| 90 | + <groupId>org.eclipse.tycho</groupId> |
| 91 | + <artifactId>tycho-compiler-jdt</artifactId> |
| 92 | + <version>${tycho-version}</version> |
| 93 | + </dependency> |
| 94 | + </dependencies> |
| 95 | + </plugin> |
| 96 | + </plugins> |
| 97 | + </pluginManagement> |
| 98 | + </build> |
| 99 | + </profile> |
| 100 | +``` |
0 commit comments