A drop-in replacement for the "javac" ANT task, which automatically injects assertion code for @NotNull / @NonNull annotations
Java
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.idea
lib
src/org/parboiled
.gitignore
LICENSE
NOTICE
README.textile
build.xml
javacn.iml

README.textile

javacn

javacn is a stripped down and slightly modified version of com.jetbrains.ant.Javac2.
It provides an ANT task that serves as a drop-in replacement for the standard javac task.

When you use javacn instead of javac your java source files will not only be compiled to .class files,
but will also contain assertion code for all NotNull/NonNnull annotations on reference type method parameters
as well as methods returning a reference type.

The package name of the annotations does not matter, you can use any annotation implementation as long as
its simple name is either NotNull, NonNull, Notnull or Nonnull (this includes org.jetbrains.annotations.NotNull,
which is the implementation that will even yield similar assertion code when used from within the IntelliJ IDEA IDE,
as well as javax.annotation.Nonnull from JSR-305).

Consider the following code snippet:

public void raiseSalary(@NotNull Employee employee, int percent) {
    Money currentSalary = employee.getCurrentSalary();
    ...
}

Normally this method would throw an NPE when called with null for the first parameter.
When compiled with javacn however, you would get an exception like the following and the actual method code would not even be executed.

java.lang.IllegalArgumentException: 1st argument of method com.company.Project.raiseSalary(...)
        corresponds to @NotNull parameter and must not be null
        at com.company.Project.raiseSalary.parse(Project.java)
        at ....

A similar exception will be raised when a method carrying a @NotNull / @NonNull annotation returns a null value.

Usage

javacn has a dependency on ASM version 3.
Therefore your ANT build file should include a taskdef like the following:

<taskdef name="javacn" classname="org.parboiled.ant.Javacn">
    <classpath>
        <pathelement location="lib/javacn/asm-3.2.jar"/>
        <pathelement location="lib/javacn/javacn.jar"/>
    </classpath>
</taskdef>

where the path to the jars has to be adapted to your project structure.

You can then use javacn just as you would use javac:

<target name="compile" description="Compile java sources">
    <javacn debug="on" destdir="build/classes" source="1.5" target="1.5" encoding="utf8">
        <src location="src"/>
        ...
    </javacn>
</target>

Please let me know if you find any issues with javacn.

Cheers,
Mathias