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

5 comments
Comment actions Permalink

is getSelectedEntity() method somehow annotated with @Nullable or @NotNull?
-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

I have the following code:

public void selectionChanged( IWorkbenchPart part, ISelection
selection ) {
EntityProvider<t> adapter = (EntityProvider<t>) 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



0
Comment actions Permalink

Maxim Shafirov (JetBrains) wrote:

is getSelectedEntity() method somehow annotated with @Nullable or @NotNull?
-------------------


No annotations...


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; //> 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 >>]]>


0
Comment actions Permalink

Johannes Schneider wrote:

Maxim Shafirov (JetBrains) wrote:

>>is getSelectedEntity() method somehow annotated with @Nullable or @NotNull?
>>----



No annotations...


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

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; //>>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 >>> >> >>]]>

0
Comment actions Permalink

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

0
Comment actions Permalink

Thomas Singer (MoTJ) wrote:

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


Well, you're right... Thanks for opening my eyes ;)


Johannes Schneider

0

Please sign in to leave a comment.