Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforcing nullability contracts in runtime #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Enforcing nullability contracts in runtime #352

wants to merge 1 commit into from

Conversation

denis-zhdanov
Copy link

This PR applies Traute to the project. The result is that Traute generates runtime null-checks based on NonNull annotations defined in source code.

Consider ATEUtils. applyTaskDescription() for example:

Source code

private static void applyTaskDescription(@NonNull Activity activity, @Nullable String key, int color) {
    ...
}

Resulting bytecode looks like if it's compiled from the source below:

private static void applyTaskDescription(@NonNull Activity activity, @Nullable String key, int color) {
    if (activity == null) {
        throw new NullPointerException("Argument 'activity' of type Activity (#0 out of 3, zero-based) is marked by @android.support.annotation.NonNull but got null for it");
    }
    ...
}

Raw bytecode

javap -c -private ./app/build/intermediates/classes/debug/com/naman14/timber/utils/ATEUtils.class
...
  private static void applyTaskDescription(android.app.Activity, java.lang.String, int);
    Code:
       0: aload_0
       1: ifnonnull     14
       4: new           #28                 // class java/lang/NullPointerException
       7: dup
       8: ldc           #29                 // String Argument 'activity' of type Activity (#0 out of 3, zero-based) is marked by @android.support.annotation.NonNull but got null for it
      10: invokespecial #30                 // Method java/lang/NullPointerException."<init>":(Ljava/lang/String;)V
      13: athrow
       ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant