KAnnotator 0.1 is out

We announced KAnnotator back in December: it helps you against NPEs in Java and makes your Kotlin code nicer. Today we are pleased to announce a KAnnotator Plugin for IntelliJ, version 0.1. It is available from the plugin repository.

Why Infer Annotations

An excerpt from an earlier blog post, explaining what KAnnotator is:

Since Kotlin M3, you can use external annotations to tell the system that your methods return/take non-null values. This mechanism is useful even if you don’t use Kotlin: you can turn on nullability inspections for Java too (and I totally recommend you to do so).

One problem about this used to be that, while you can annotate your own code while you write it (and we do it all the time at JetBrains), your favorite library is not annotated, and it is so big, you can’t annotate it manually.

The key thing here is that ‘manually’. A programmer is a lazy creature, and by virtue of our laziness, we want to automate as much as we can. KAnnotator is a tool that annotates your libraries automatically (the tool is written in Kotlin, of course).

How it works, in a nutshell: you have your library as a jar (or a number of jars), you tell KAnnotator to infer annotations for these jars, and get a bunch of .xml files back. They contain annotations for methods and fields, for example:

Now, you can attach these annotations to your project, and both Java IDE and Kotlin compiler will see them.

We used KAnnotator to infer annotations for JDK that ship with Kotlin. Now you can use it as well: to annotate your own libraries.

Install

KAnnotator ships as a separate plugin for IntelliJ IDEA 12 or higher (both 12.0.4 and 12.1 will work). Note that the Kotlin plugin is not required. To install the plugin, follow the instructions from here.

Infer

No you can call Analyze -> Annotate Jar Files… (from either Main menu or context menu)

You get a dialog like this:

There you specify what JAR files you want analyzed and where to put the results, i.e. XML files containing annotations. By default, KAnnotator will attach the annotation to the libraries you call it on.

Enjoy

So, when you see Kotlin complaining about nullable types coming from Java, all you need to do is run KAnnotator once on that Java library, and it will turn your red code green.

If it does not, maybe KAnnotator is not smart enough, but maybe that method actually returns null?

About Andrey Breslav

Andrey is the lead language designer working on Project Kotlin at JetBrains. He also works on making the Java language better, serving as a Java Community Process expert in a group for JSR-335 ("Project Lambda"), and occasionally speak at software conferences such as Devoxx or JavaOne.
This entry was posted in General and tagged . Bookmark the permalink.

13 Responses to KAnnotator 0.1 is out

  1. Maarten says:

    Interesting tool. I’d like to try it on a large maven-based project I have at work, but I’d need an hour just to click all the checkboxes on all the libraries.
    Could you include a ‘select all’ button in the next version?

  2. Ian Clarke says:

    I’ve had some issues with IDEA assigning various nullability annotations to things, that then cause them to break when my colleagues try to use the source code in a different IDE (eg. Eclipse), because the annotations aren’t in the classpath (we use Maven, and try to keep everything cross-IDE).

    Would you mind clarifying how to take advantage of these nullability annotations in a cross-IDE manner (at least so that it doesn’t break the source code in other IDEs)?

    • In what way does it break the code? (IntelliJ only issues warnings upon nullability issues, never errors, so the code should always compile).

      • Ian Clarke says:

        In that it added annotations to the code that weren’t in any Maven dependency (IIRC it was some auto-generated method, sorry – it was a while ago). Worked locally, but not elsewhere.

        I guess the broader question is: If IDEA nullability annotations are IDEA-specific, how does this work when other people working on the source code might be using different IDEs.

  3. Maarten says:

    I’ve found a jar file that causes a JetNullPointerException when KAnnotator processes it: http://mavenhub.com/mvn/thirdparty-releases/gnu-getopt/getopt/1.0.12-brew

    The exception has been sent as error report 480295 to the exception analyzer.

  4. Gri says:

    http://kotlin-demo.jetbrains.com/

    Problems with aS =”123.34″.split(“.”) ( js project)
    aS.size == 7 ???

    fun main(args : Array) {
    println(“Hello, world!”)
    val aS = “123.34″.split(“.”)
    println( aS.size)
    }

    ——–
    Hello, world!
    7

    Generated JavaScript code
    Kotlin.System.flush();
    (function () {
    ‘use strict’;
    var _ = Kotlin.defineRootPackage(null, /** @lends _ */ {
    main: function (args) {
    Kotlin.println(‘Hello, world!’);
    var aS = Kotlin.splitString(’123.34′, ‘.’);
    Kotlin.println(aS.length);
    }
    });
    Kotlin.defineModule(‘JS_TESTS’, _);
    _.main([]);
    }());

    Kotlin.System.output();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>