False positives?
I have the following code:
public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
EntityProvider adapter = (EntityProvider]]>) part.getAdapter(
EntityProvider.class );
if ( adapter == null ) return;
//noinspection ObjectEquality
if ( entityComposite.getSelectedEntity() ==
adapter.getSelectedEntity() ) return;
if ( adapter.getSelectedEntity()!=null &&
adapter.getSelectedEntity().equals( entityComposite.getSelectedEntity()
) ) return; //<-- This line has warnings: may produce NPE and is always
false
entityComposite.checkCurrentState();
entityComposite.setSelectedEntity( adapter.getSelectedEntity() );
entityComposite.update();
}
I get those two warnings. But I think they are not correct. Any comments?
Johannes Schneider
Please sign in to leave a comment.
is getSelectedEntity() method somehow annotated with @Nullable or @NotNull?
-
Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"
Maxim Shafirov (JetBrains) wrote:
No annotations...
Johannes Schneider
>> I have the following code:
>>
>> public void selectionChanged( IWorkbenchPart part, ISelection
>> selection ) {
>> EntityProvider adapter = (EntityProvider) part.getAdapter( >> EntityProvider.class ); >> if ( adapter == null ) return; >> //noinspection ObjectEquality >> if ( entityComposite.getSelectedEntity() == >> adapter.getSelectedEntity() ) return; >> if ( adapter.getSelectedEntity()!=null && >> adapter.getSelectedEntity().equals( >> entityComposite.getSelectedEntity() >> ) ) return; //<-- This line has warnings: may produce NPE and is >> always >> false >> entityComposite.checkCurrentState(); >> entityComposite.setSelectedEntity( adapter.getSelectedEntity() ); >> entityComposite.update(); >> } >> I get those two warnings. But I think they are not correct. Any >> comments? >> >> Johannes Schneider >>]]>
Johannes Schneider wrote:
>>is getSelectedEntity() method somehow annotated with @Nullable or @NotNull?
>>----
I'm stupid...
Yes of course: I have added annotations to some methods. I will take a
closer look tomorrow, but I think "getSelectedEntity" is annotated with
@Nullable.
Johannes Schneider
>>Maxim Shafirov
>>http://www.jetbrains.com
>>"Develop with pleasure!"
>>
>>
>>>I have the following code:
>>>
>>>public void selectionChanged( IWorkbenchPart part, ISelection
>>>selection ) {
>>>EntityProvider adapter = (EntityProvider) part.getAdapter( >>>EntityProvider.class ); >>>if ( adapter == null ) return; >>>//noinspection ObjectEquality >>>if ( entityComposite.getSelectedEntity() == >>>adapter.getSelectedEntity() ) return; >>>if ( adapter.getSelectedEntity()!=null && >>>adapter.getSelectedEntity().equals( >>>entityComposite.getSelectedEntity() >>>) ) return; //<-- This line has warnings: may produce NPE and is >>>always >>>false >>>entityComposite.checkCurrentState(); >>>entityComposite.setSelectedEntity( adapter.getSelectedEntity() ); >>>entityComposite.update(); >>>} >>>I get those two warnings. But I think they are not correct. Any >>>comments? >>> >>>Johannes Schneider >>> >> >>]]>
Maybe invoking adapter.getSelectedEntity() can return a non-null reference
the first time and null the second time. How should IDEA know (beside taking
a deeper look into the method call)?
Tom
Thomas Singer (MoTJ) wrote:
Well, you're right... Thanks for opening my eyes ;)
Johannes Schneider