Silver: unable to generate @Nullable properties in Android

I have a situation where I need to extend a RecyclerView. In order to do this and still be able to construct my new view in a layout file, I need to re-implement the three constructors. Here is what it would look like in java:

public class RecyclerTest extends RecyclerView {

public RecyclerTest(Context context) {
    super(context);
}

public RecyclerTest(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public RecyclerTest(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
}

However, in swift specifying a property as nullable using ‘!’ does not add the @Nullable attribute.

Thanks, logged as bugs://75978

You mean, that you want ! to automatically emit @Nullable ?

Not necessarily, but it would be nice to have some way to emit @Nullable. Otherwise there is no way to extend any Android UI elements in Silver and still be able to use them in a layout xml file.

Or is there already a way to specify in Silver to add @Nullable?

Sure:

import java.util
import android.app
import android.content
import android.os
import android.util
import android.support.annotation
import android.view
import android.widget

public class RecyclerTest: ViewGroup {

public init(_ context: Context ) {
    super.init(context);
}

public init(_ context: Context , @Nullable  _ attrs: AttributeSet!) {
    super.init(context, attrs);
}

public init(_ context: Context , @Nullable _ attrs: AttributeSet!, _ defStyle: Integer) {
    super.init(context, attrs, defStyle);
}
}

bugs://75978 got closed with status nochangereq.

That worked beautifully! I was thinking you couldn’t add @Nullable because I hadn’t added the android.support.annotation import. Thank you!

Just looking at @Nullable annotations in the context of using https://github.com/uber/NullAway
We have a mixed code base of Silver compiled swift, and Java at the UI layer. I would really like to tighten up the null checking in Java as we are constantly getting NPEs. To do that effectively, I need to get NullAway or a similar tool to know about the nullable / nonnull ness of the swift method params and return types.

  1. I’m assuming that there’s no current way for the Silver compiler to automatically emit @Nullable annotations for optional parameters / return types ? Ever consider adding this?

  2. Is there any way to read the metadata file that’s spit out in the jar that comes out of Silver to get this metadata? Nullaway has a way of providing nullable metadata outside of annotations.

Cheers

Jon

I’'m not aware of NullAway, but we should have a look. Currently;y, the compiler generates internal metadata for nullability, that regular “Oracle Java Compiler” compiled code might/will not see, I’m afraid. One option for now is to also use Elem ents to build the Java code, our Java (language) compiler will honor its nullability info from, Swift (and vice-versa).

—marc

Thanks.
I don’t think moving Java compilers is an option for us in the short term - just too big a regression risk for an app thats in production already.
Is there any way to get the nullability data out of the compiler metadata file? Then I could write some code which makes this available to Nullaway.
Alternatively is it a possibility for the compiler to emit @Nullable annotations on return types and params? If it used the android.support.annotation.Nullable version, it should also be visible to Android Lint as well as Nullaway: https://developer.android.com/studio/write/annotations.html#res-annotations

bugs://i63913 was closed as fixed.

Logged as bugs://i63913.