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

Bug 348433

Summary: NPE if a static profile is applied (applyProfile method)
Product: [Modeling] MDT.UML2 Reporter: Tristan Faure <faure.tristan>
Component: CoreAssignee: Kenn Hussey <Kenn.Hussey>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: P3 CC: ed
Version: unspecifiedFlags: Kenn.Hussey: indigo+
Target Milestone: SR1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Tristan Faure CLA 2011-06-06 12:50:50 EDT
Hi,

I consider a model which has a static profile applied on it (named P)
In MDT Papyrus for Control operation we reapply the profile P on a subpackage using the method Package.applyProfile(p);

In org.eclipse.uml2.uml.internal.operations.PackageOperations the method getTarget calls the getNamedElement methods to get the corresponding definition to the UML element. 
In our case as the profile is static, the definition of the profile is not linked to an eannotation child of the eclassifier.
This implies a NPE, the stack strace is available bellow. 

We suggest to add an if expression to return the eclass in parameter if getNamedElement returns null do you agree with this behavior ?

@Override
protected EClass getTarget(EClass eClass) {
		// OLD VERSION
		// return (EClass) profile.getDefinition(getNamedElement(eClass));
		// NEW VERSION
		NamedElement namedElement = getNamedElement(eClass) ;
		if (namedElement != null)
		{
			return (EClass) profile.getDefinition(namedElement);
		}
		else
		{
			return eClass ;
		}
}

This bug is blocking Control Feature of MDT Papyrus so I evaluate this bug as "Major"

The stack trace :
Caused by: java.lang.NullPointerException
    at org.eclipse.emf.ecore.util.EcoreUtil.create(EcoreUtil.java:3270)
    at
org.eclipse.uml2.uml.util.UMLUtil$StereotypeApplicationHelper.applyStereotype(UMLUtil.java:196)
    at org.eclipse.uml2.uml.util.UMLUtil.applyStereotype(UMLUtil.java:9045)
    at
org.eclipse.uml2.uml.internal.operations.PackageOperations.access$0(PackageOperations.java:1)
    at
org.eclipse.uml2.uml.internal.operations.PackageOperations$StereotypeApplicationCopier.createCopy(PackageOperations.java:120)
    at org.eclipse.emf.ecore.util.EcoreUtil$Copier.copy(EcoreUtil.java:472)
    at
org.eclipse.uml2.uml.internal.operations.PackageOperations.applyProfile(PackageOperations.java:581)
    at
org.eclipse.uml2.uml.internal.impl.PackageImpl.applyProfile(PackageImpl.java:1059)
    at
org.eclipse.papyrus.controlmode.umlprofiles.helpers.ProfileApplicationHelper.duplicateProfileApplication(ProfileApplicationHelper.java:48)
    at
org.eclipse.papyrus.controlmode.umlprofiles.commands.UMLProfileControlCommand.duplicateAppliedProfiles(UMLProfileControlCommand.java:71)
    at
org.eclipse.papyrus.controlmode.umlprofiles.commands.UMLProfileControlCommand.access$0(UMLProfileControlCommand.java:68)
    at
org.eclipse.papyrus.controlmode.umlprofiles.commands.UMLProfileControlCommand$1.run(UMLProfileControlCommand.java:47)
    at
org.eclipse.uml2.common.edit.command.ChangeCommand.doExecute(ChangeCommand.java:65)
    at
org.eclipse.emf.edit.command.ChangeCommand.execute(ChangeCommand.java:136)
    at
org.eclipse.emf.common.command.CompoundCommand.execute(CompoundCommand.java:267)
    at
org.eclipse.papyrus.controlmode.commands.ControlCommand.doRedo(ControlCommand.java:171)
    at
org.eclipse.papyrus.controlmode.commands.ControlCommand.doExecuteWithResult(ControlCommand.java:120)
Comment 1 Kenn Hussey CLA 2011-06-07 10:29:55 EDT
In the case where there is no annotation linking the Ecore class to its defining UML class (i.e., for static profiles), the getNamedElement(ENamedElement) is supposed to look up the definition by name after retrieving the profile from the (UML2) generated package registry:

			org.eclipse.uml2.uml.Package package_ = (org.eclipse.uml2.uml.Package) getNamedElement(
				((EClassifier) definition).getEPackage(), context);

			if (package_ != null) {
				String name = definition.getName();

				for (Type ownedType : package_.getOwnedTypes()) {

					if (safeEquals(getValidJavaIdentifier(ownedType.getName()),
						name)) {

						return ownedType;
					}
				}
			}

Is this not happening in your case?
Comment 2 Tristan Faure CLA 2011-06-07 11:24:10 EDT
it is not the case it has been a surprised for me too, 
org.eclipse.uml2.uml.Package package_ = (org.eclipse.uml2.uml.Package) getNamedElement(
				((EClassifier) definition).getEPackage(), context); 
This method returns null as the call to getProfile is made with context equals to null 
the execution order : 
org.eclipse.uml2.uml.internal.operations.createCopy(EObject eObject)
	--> org.eclipse.uml2.uml.internal.operations.getTarget(EClass eClass)
		--> org.eclipse.uml2.uml.util.getNamedElement(ENamedElement definition)
			--> org.eclipse.uml2.uml.util.getNamedElement(ENamedElement definition,	EObject context) (context == null) 
				--> org.eclipse.uml2.uml.util.getProfile(EPackage definition, EObject context)
				
Regards
Comment 3 Kenn Hussey CLA 2011-06-09 12:26:10 EDT
I see. Unfortunately, it's too late to fix this for Indigo, but we can do it in a maintenance release shortly thereafter.

I don't think the proposed fix is correct; instead, I think a proper context needs to be passed to the getNamedElement(ENamedElement, EObject) method from the copier.
Comment 4 Kenn Hussey CLA 2011-08-11 01:03:46 EDT
The fix has been committed to CVS.
Comment 5 Kenn Hussey CLA 2011-08-11 10:48:35 EDT
The fix is available in 3.2.1 maintenance build M201108110857.
Comment 6 Ed Willink CLA 2012-02-23 01:33:46 EST
This fix appears to have been applied during the CVS to GIT transition.

It appears in the read-only CVS but not in GIT for which the 3.2 maintenance stream is missing.

It is not clear whether this fix has been applied to the master stream.
Comment 7 Kenn Hussey CLA 2012-02-23 08:20:03 EST
Did you notice behavior in UML2 4.0 that suggested this problem still exists? All of the changes that were made on the maintenance branch (in CVS) were made on the master branch (in git). I'll pursue the issue with the missing git branch in response to bug 372315.
Comment 8 Ed Willink CLA 2012-02-23 11:34:12 EST
I haven't noticed anything other than a newsgroup message that deserved an answer. It would be good if you could monitor at least the UML group for which the traffic is quite light (perhaps Papyrus too). I try to be helpful but my MDT/UML2 knowledge is fairly limited.
Comment 9 Kenn Hussey CLA 2012-02-23 14:35:50 EST
Thanks for the feedback, I'll try to do a better job of monitoring the newsgroup.