Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 356097 - NPE and wrong change description in compare editor if an UpdateReference has neither a left nor a right target
Summary: NPE and wrong change description in compare editor if an UpdateReference has ...
Status: CLOSED FIXED
Alias: None
Product: EMFCompare
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: EMF Compare CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-29 11:53 EDT by Andreas Mayer CLA
Modified: 2013-01-17 08:32 EST (History)
2 users (show)

See Also:


Attachments
Models to reproduce the issue (765 bytes, application/octet-stream)
2011-08-29 11:57 EDT, Andreas Mayer CLA
no flags Details
Patch for UpdateReferenceItemProvider r1.22 (4.89 KB, patch)
2011-08-30 05:47 EDT, Andreas Mayer CLA
laurent.goubet: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Mayer CLA 2011-08-29 11:53:55 EDT
Build Identifier: I20110613-1736

If a diff model contains an UpdateReference that has neither a left nor a right target, then the following NPE is thrown once its parent is expanded and the UpdateReferenceItemProvider is asked for an image for the UpdateReference instance. In addition, UpdateReferenceItemProvider.getText(...) also returns a wrong textual description ("Reference ... in ... changed from null to null" instead of "... changed from XYZ to null") here.

java.lang.NullPointerException
        at org.eclipse.emf.edit.provider.ComposedAdapterFactory.adapt(ComposedAdapterFactory.java:340)
        at org.eclipse.emf.edit.provider.ComposedAdapterFactory.adapt(ComposedAdapterFactory.java:277)
        at org.eclipse.emf.compare.util.AdapterUtils.adapt(AdapterUtils.java:55)
        at org.eclipse.emf.compare.util.AdapterUtils.getItemProviderImage(AdapterUtils.java:79)
        at org.eclipse.emf.compare.diff.provider.UpdateReferenceItemProvider.getImage(UpdateReferenceItemProvider.java:58)
        at org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.getImage(AdapterFactoryLabelProvider.java:336)
        at org.eclipse.emf.compare.ui.viewer.structure.ModelStructureMergeViewer$ModelStructureLabelProvider.getImage(ModelStructureMergeViewer.java:432)
        at org.eclipse.jface.viewers.WrappedViewerLabelProvider.getImage(WrappedViewerLabelProvider.java:117)
        at org.eclipse.jface.viewers.WrappedViewerLabelProvider.update(WrappedViewerLabelProvider.java:165)
        at org.eclipse.jface.viewers.ViewerColumn.refresh(ViewerColumn.java:152)
        at org.eclipse.jface.viewers.AbstractTreeViewer.doUpdateItem(AbstractTreeViewer.java:938)
        at org.eclipse.jface.viewers.AbstractTreeViewer$UpdateItemSafeRunnable.run(AbstractTreeViewer.java:106)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
        at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49)
        at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
	at org.eclipse.jface.viewers.AbstractTreeViewer.doUpdateItem(AbstractTreeViewer.java:1018)
        ...
   
You will get a diff model with an UpdateReference where leftTarget = rightTarget = null by comparing two models, one of which sets a reference to an object that was newly added, for example:

* Model 1

  <?xml version="1.0" encoding="UTF-8"?>
  <ecore:EPackage xmi:version="2.0"
      xmlns:xmi="http://www.omg.org/XMI"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"   name="mypackage">
    <eClassifiers xsi:type="ecore:EClass" name="A">
      <eStructuralFeatures xsi:type="ecore:EReference" name="b"   eType="#//B"/>
    </eClassifiers>
    <eClassifiers xsi:type="ecore:EClass" name="B"/>
  </ecore:EPackage>

* Model 2

  <?xml version="1.0" encoding="UTF-8"?>
  <ecore:EPackage xmi:version="2.0"
      xmlns:xmi="http://www.omg.org/XMI"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"   name="mypackage">
    <eClassifiers xsi:type="ecore:EClass" name="A">
      <eStructuralFeatures xsi:type="ecore:EReference" name="b"/>
    </eClassifiers>
  </ecore:EPackage>

This is because ReferenceCheck.createUpdateReferenceOperation(...) uses 

  EObject leftTarget = getMatchedEObject(addedValue);
  EObject rightTarget = getMatchedEObject(deletedValue);

to compute the targets. In this case addedValue has no match and deletedValue is null (or vice versa) and so both targets are null.

To fix this issue, UpdateReferenceItemProvider shouldn't use the UpdateReference's targets to compute the image and text. Instead it should use the reference value from leftElement and rightElement as already done in UpdateReferenceImpl.toString(). 

Reproducible: Always

Steps to Reproduce:
1. Compare two models, one of which sets a reference to an object that was newly added (see examples above).
2. Expand the diff element that corresponds to the change of that reference.
3. An NPE is logged and the viewer shows a wrong textual description of the change.
Comment 1 Andreas Mayer CLA 2011-08-29 11:57:17 EDT
Created attachment 202343 [details]
Models to reproduce the issue
Comment 2 Andreas Mayer CLA 2011-08-30 05:47:38 EDT
Created attachment 202396 [details]
Patch for UpdateReferenceItemProvider r1.22

This patch changes UpdateReferenceItemProvider.getText(Object) and UpdateReferenceItemProvider.getImage(Object) to use the actual reference values of the left and right elements instead of the targets to compute the text or image, respectively. This fixes the issue described in the bug report and shouldn't change anything for all other cases.

It may also be worth considering, to check the argument of AdapterUtils.adapt(EObject, Class) for null and return null in this case instead of letting ComposedAdapterFactory throw its NPE. Adding this check will make AdapterUtils.getItemProviderImage(Object) correctly return a null image for null.
Comment 3 Laurent Goubet CLA 2011-09-02 05:10:15 EDT
Thanks Andreas. This patch has been commited on both master and the 1.2 maintenance branch.

Changing AdapterUtils is something I am reluctant to do as we do not have tests for the UI itself.
Comment 4 Laurent Goubet CLA 2013-01-17 08:32:17 EST
batch-closing a bunch of "RESOLVED" bugs.