Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 356469

Summary: Command handlers should be more robust to avoid exceptions during part de-activation.
Product: [Modeling] Papyrus Reporter: Alain Le Guennec <alain.leguennec>
Component: CoreAssignee: Project Inbox <mdt-papyrus-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipse-bugzilla, vincent.lorenzo, yann.tanguy
Version: 0.8.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed patch to (partially) fix the issue.
none
New patch to solve the NullPointerException with LinkItem none

Description Alain Le Guennec CLA 2011-09-01 09:00:17 EDT
Many Papyrus command handlers trigger NullPointerExceptions during part deactivation (CopyHandler, RenameNamedElementHandler, etc...) if the new part being activated is not a papyrus-related part.

The cause is the way that the isEnabled() function is commonly implemented in Papyrus' command handlers.

Normally, the enablement state is supposed to be computed during setEnabled(evaluationContext), and then simply returned in isEnabled() (in such case, isEnabled() has no side-effect).
It turns out that Papyrus does not usually do this pre-computation of the enablement conditions in setEnabled(), and instead do all the hard work in isEnabled() on-the-fly.

It turns out that during part-deactivation, Eclipse retrieves the enablement state of the "old" handler before it gets replaced (not sure if it is that usefull, but it's done anyway), causing Papyrus to do the isEnabled() work again but in the unintended new context (see Command.setHandler()).
If the new part is not related to Papyrus, Papyrus has trouble getting to its service registry, among possible pbs. And the code in isEnabled() is often not prepare to cope with such situations, missing checks for null pointers for instance.

There are two ways to fix the problems:
-Make the code for isEnabled() more robust
-Preferably, redesign all Command Handlers to override setEnabled(evaluationContext), which would then build and cache the associated Papyrus command and check its executability.
All subclasses of org.eclipse.papyrus.modelexplorer.handler.AbstractCommandHandler and org.eclipse.papyrus.uml.menu.handler.AbstractEMFCommandHandler are potentially impacted.
Comment 1 Alain Le Guennec CLA 2011-09-02 09:18:34 EDT
Created attachment 202669 [details]
Proposed patch to (partially) fix the issue.

(1) I, Alain LE GUENNEC, wrote 100% of the code I've provided. 
(2) This code contains no cryptography 
(3) I have the right to contribute the code to Eclipse. 
(4) I contribute the content under the EPL.

Note that this patch only makes the code more robust against null pointer exceptions. I did not refactor the code to use setEnabled() to prepare the new status of the command, which would probably be more elegant...
Comment 2 Yann Tanguy CLA 2011-09-05 02:50:14 EDT
Patch applied in 0.8.1 (r5368) and trunk (r5369).
Comment 3 Alain Le Guennec CLA 2011-09-22 09:07:50 EDT
There is still one case for which a NullPointerException is raised:
If you have a model explorer customized with a Facet that use folders for FacetReferences, and you change the selection to such a folder,
you'll get a NullPointerException.
This is due to the function org.eclipse.papyrus.modelexplorer.handler.AbstractCommandHandler.getSelectedElements() not being robust enough:
When it does adaptation to EObject, it does not check that the adaptation did not return null (which can happen for LinkItem from EMF facet).
The new proposed patch solves that issue.
Comment 4 Alain Le Guennec CLA 2011-09-22 09:11:03 EDT
Created attachment 203838 [details]
New patch to solve the NullPointerException with LinkItem

(1) I, Alain LE GUENNEC, wrote 100% of the code I've provided. 
(2) This code contains no cryptography 
(3) I have the right to contribute the code to Eclipse. 
(4) I contribute the content under the EPL.
Comment 5 Camille Letavernier CLA 2013-07-05 12:48:53 EDT
> Created attachment 203838 [details]
> New patch to solve the NullPointerException with LinkItem

I think this patch has not been applied, but the handler has been fixed anyway.

I close this task