NotNull annotation with generated code

I'm using the NotNull annotations in a lot of my code. I am also using some code generated by XMLBeans, which of course contains no Nullable/NotNull annotations.

I'm having some problems where these two pieces of code meet:

String label = bean.getLabel(); // This is a generated method with no annotations
model.setLabel( label ); // This is my method, declared as setLabel( @NotNull String inLabel );


The problem is that my bean may return a null value for label. When this happens, I get a runtime exception because the @NotNull contract is violated. I see no compiler warning on the call to setLabel(). It's as if Idea is assuming that getLabel() is marked with a @NotNull for the return value.

I don't think Idea is being paranoid enough in this case. I think Idea should automatically assume that any unannotated method (such as the getLabel() above) is @Nullable .Then I would see a compiler warning on the next line.

I understand that this could be really annoying, but if you're going to rely on these annotations, it's better to be overly paranoid than too lax.

Perhaps we could have a project setting like "Assume unannotated mehtods return @Nullable"?


Willis Morse

3 comments
Comment actions Permalink

Hi!
IDEA 7 supports external annotations. Check here:
http://blogs.jetbrains.com/idea/2008/02/external-annotations/

0
Comment actions Permalink

IMHO that would generate too many false positives and would make people switch
the inspection off completely.
+1, though, for an option which is off by default.

Note however that you also can annotate code externally, see for example
http://blogs.jetbrains.com/idea/2008/02/external-annotations/

Also there used to be a commercial plugin, that was able to run in-depth code analysis to
figure out "nullness" and add annotations, but the company recently ceased to be...
http://www.sixthandredriver.com/inspectorgeneral.html


Willis Morse wrote:

I'm using the NotNull annotations in a lot of my code. I am also using some code generated by XMLBeans, which of course contains no Nullable/NotNull annotations.

I'm having some problems where these two pieces of code meet:

String label = bean.getLabel(); // This is a generated method with no annotations
model.setLabel( label ); // This is my method, declared as setLabel( @NotNull String inLabel );


The problem is that my bean may return a null value for label. When this happens, I get a runtime exception because the @NotNull contract is violated. I see no compiler warning on the call to setLabel(). It's as if Idea is assuming that getLabel() is marked with a @NotNull for the return value.

I don't think Idea is being paranoid enough in this case. I think Idea should automatically assume that any unannotated method (such as the getLabel() above) is @Nullable .Then I would see a compiler warning on the next line.

I understand that this could be really annoying, but if you're going to rely on these annotations, it's better to be overly paranoid than too lax.

Perhaps we could have a project setting like "Assume unannotated mehtods return @Nullable"?


Willis Morse

0
Comment actions Permalink

Good call on the external annotations; that seems to be the only way to do it for now. Of course, I have to do this manually for each method, which is a little tedious. But still better than nothing.

And thanks for reminding me about Sixth and River's plugin that I own :) It's great, but Infer Nullity doesn't seem to do anything when run against .class files. I guess there's not much to go on when trying to infer nullity and code flow.

Thanks for the help,
Willis Morse

0

Please sign in to leave a comment.