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

When reporting error on an initializer, also give line numbers for uninitialized fields #95

Closed
chengniansun opened this issue Dec 28, 2017 · 15 comments

Comments

@chengniansun
Copy link

In java.util.AbstractMap, the field "values" should be annotated with @MonotonicNonNull. However, there is no way to annotate it. The best way is to use a library model, I guess.

https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/AbstractMap.java#L300

@msridhar
Copy link
Collaborator

msridhar commented Dec 28, 2017 via email

@chengniansun
Copy link
Author

https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/MapMakerInternalMap.java#L160

com/google/common/collect/MapMakerInternalMap.java:160: warning: [NullAway] initializer method does not guarantee @NonNul l fields keySet, values, entrySet are initialized along all control-flow paths (remember to check for exceptions or early returns).

I suppose that the three fields are inherited from AbstractMap.

@msridhar
Copy link
Collaborator

msridhar commented Dec 28, 2017 via email

@msridhar
Copy link
Collaborator

Actually looks like those fields are declared within MapMakerInternalMap:

keySet

values

entrySet

I still think our handling of fields inherited from abstract classes is buggy 😄 But that doesn't seem to be the problem here.

@msridhar
Copy link
Collaborator

I take back the comment about abstract classes. I think they should be treated just like regular classes: if you declare a non-null field in an abstract class, you should also declare a constructor / initializer that sets it. Relying on sub-classes to do this work seems fragile.

@chengniansun I'm going to close this one, pls re-open if I got something wrong

@chengniansun
Copy link
Author

Oh, I am sorry for the false alarm. I found these three fields in this class, but thought they belonged to the internal class WeakKeyDummyValueSegment (I connected to my machine remotely and did not use an IDE).

I think the curry way that NullAway handles inheritance is correct, so nothing needs to be done with this bug report.

But, as an enhancement, it would help if NullAway can print the line numbers of the fields that are not initialized. Then I can easily identify these three fields.

@msridhar
Copy link
Collaborator

But, as an enhancement, it would help if NullAway can print the line numbers of the fields that are not initialized. Then I can easily identify these three fields.

Yeah, it's a bit tricky; should we report the error on the constructor / initializer missing the initialization, or on the fields? I agree that giving line numbers of the fields in the former case could be useful. Hopefully in most cases the IDE will solve the problem.

@chengniansun
Copy link
Author

Currently, the locations of the initializers are reported. It will be helpful if there are locations after each field in the following example.

com/google/common/collect/MapMakerInternalMap.java:160: warning: [NullAway] initializer method does not guarantee @nonnul l fields keySet (LOCATION HERE), values (LOCATION HERE), entrySet (LOCATION HERE) are initialized along all control-flow paths (remember to check for exceptions or early returns).

This is definitely not an urgent or import feature, but worth consideration. :)

@msridhar msridhar changed the title How to handle nullness of the fields in the jdk source. When reporting error on an initializer, also give line numbers for uninitialized fields Dec 28, 2017
@msridhar
Copy link
Collaborator

I've re-titled the issue and marked it as low priority, though we will try to get to it as I don't think it should be a hard fix. I've also marked it as a good first bug to fix for someone looking to learn the NullAway code and contribute.

@chengniansun
Copy link
Author

Thank you, Manu.

@swayamraina
Copy link

Hi! I am interested in this enhancement.
Can you please guide me how to build the code and how should I test my changes.

@lazaroclapp
Copy link
Collaborator

@swayamraina Sure!

For building, .gradlew build on the repository root should build everything.

For testing, feel free to add a few unit tests in nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java (see e.g. tryWithResourcesSupport for a simple example). Note that you can use these special comments // BUG: Diagnostic contains: to check what is on the error message reported by error prone.

I suspect most changes to fix this issue will be in NullAway.java and fairly close to where the original initialization error messages are created.

@swayamraina
Copy link

I am building it against a non-android java project.
It shows the following message with build failure

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

@rahilvora
Copy link

If someone is not looking into this can I contribute to this? Can someone please guide me with the requirement.

@msridhar
Copy link
Collaborator

msridhar commented Aug 3, 2018

@swayamraina sorry for the slow response. I'm not sure if NullAway can be built without the Android SDK installed, as we have a module that is a sample app. Can you open a separate issue on this and we can investigate?

@rahilvora what is unclear about the requirement? Hopefully this comment makes it clear what we are looking for.

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

No branches or pull requests

5 participants