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

Correct parameters instrumentation for delegating constructors #57

Closed
denis-zhdanov opened this Issue Nov 18, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@denis-zhdanov
Owner

denis-zhdanov commented Nov 18, 2017

There is a possible case that a constructor delegates to another constructor through the this() or super() calls:

public My(@NotNull Object arg) {
  super(arg);
}

We can't insert argument(s) null-check as the first statement then because of this() and super() are required to be the very first constructor statements (if any). However, the plugin tries to do that currently.

@denis-zhdanov

This comment has been minimized.

Owner

denis-zhdanov commented Nov 18, 2017

There are at least two possible solutions:

  1. Add checking static methods:
public My(@NotNull Object arg) {
  super(arg);
}

becomes

public My(@NotNull Object arg) {
  super(checkNotNull(arg));
}

private static <T> T checkNotNull(T arg) {
  if (arg == null) {
    throw new NullPointerException("<message>");
  }
}
  1. Put the checks after this() / super():
public My(@NotNull Object arg) {
  super(arg);
}

becomes

public My(@NotNull Object arg) {
  super(checkNotNull(arg));
  if (arg == null) {
    throw new NullPointerException("<message>");
  }
}

I decided to go the second way because:

  1. Additional methods are not welcome by Android
  2. There is no chance for a program to continue processing in case a null is given as a @NotNull argument - a NullPointerException occurs during this()/super() processing or after this()/super() processing in the plugin-introduced check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment