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

Bug 275476

Summary: EPackage.Registry.INSTANCE.getEPackage(String) returns null after package is initiated.
Product: [Modeling] EMF Reporter: Paul Slauenwhite <paulslau>
Component: CoreAssignee: Dave Steinberg <davidms>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Ed.Merks, jgwest
Version: 2.5.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 270865    
Bug Blocks: 275576, 276128    
Attachments:
Description Flags
Updated template and regenerated EMF models none

Description Paul Slauenwhite CLA 2009-05-08 11:43:27 EDT
EPackage.Registry.INSTANCE.getEPackage(String) returns null after package is initiated.

TPTP recently regenerated our EMF model code using EMF 2.5 M7.  As a result, the call to EPackage.Registry.INSTANCE.getEPackage(String) returns null after package is initiated.  For example (see org.eclipse.hyades.models.hierarchy.impl.HierarchyPackageImpl.init()),

if (isInited) return (HierarchyPackage)EPackage.Registry.INSTANCE.getEPackage(HierarchyPackage.eNS_URI);

I cannot reproduce the symptom using a self-hosted instance of Eclipse and the
latest code in CVS (HEAD).  However, using the http://www.eclipse.org/tptp/home/downloads/?buildId=TPTP-4.6.0-200905080100, I am
able to reproduce the symptom and attach a remote debugger.  The problem appears to be in org.eclipse.emf.ecore.impl.EPackageImpl.EPackageImpl(...).new Descriptor() {...}.getEPackage(), which was recently changed under defect 270865.

Note, this is blocking TPTP (see 275354 and 275344).
Comment 1 Ed Merks CLA 2009-05-08 15:04:47 EDT
Please provide a reproducible test case that I can run myself and then reopen it. I'd also suggest that your debugger works as well as mine and you might even have more time than me to do that.
Comment 2 Paul Slauenwhite CLA 2009-05-10 19:09:03 EDT
(In reply to comment #1)
> Please provide a reproducible test case that I can run myself and then reopen
> it. 

Sure.

Steps to reproduce:

1) Import the following project set in an Eclipse 3.5 M7/EMF 2.5 M7/XSD 2.5 M7 installation:

<?xml version="1.0" encoding="UTF-8"?>
<psf version="2.0">
<provider id="org.eclipse.team.cvs.core.cvsnature">
<project reference="1.0,:pserver:dev.eclipse.org:/cvsroot/tptp,platform/org.eclipse.tptp.platform.models.hierarchy,org.eclipse.tptp.platform.models.hierarchy,HEAD"/>
</provider>
</psf>

2) Create a plug-in project with a dependency on the org.eclipse.tptp.platform.models.hierarchy plug-in.

3) Create and run the following JUnit plug-in test:

import junit.framework.TestCase;

import org.eclipse.hyades.models.hierarchy.HierarchyPackage;
import org.eclipse.hyades.models.hierarchy.impl.HierarchyPackageImpl;

public class ddd extends TestCase {

	public ddd(String name) {
		super(name);
	}

	public void test1() throws Exception {
		
		assertNotNull(HierarchyPackageImpl.init());
		
		SecondThread thread = new SecondThread();
		
		thread.start();
		
		try {
			Thread.sleep(2000);
		} 
		catch (InterruptedException i) {
			//Ignore.
		}
		
		assertNotNull(thread.getHierarchyPackage());
	}
	
	class SecondThread extends Thread{
		
		public HierarchyPackage hierarchyPackage = null;
		
		public void run() {
			hierarchyPackage = HierarchyPackageImpl.init();
		}
		
		public HierarchyPackage getHierarchyPackage(){
			return hierarchyPackage;
		}
	};
}

> I'd also suggest that your debugger works as well as mine and you might
> even have more time than me to do that.

We have already debugged this into the EMF code and we (TPTP) are also resource and time contained.  

Dave Steinberg provided the following reference and work-around:

Reference: http://www.eclipse.org/newsportal/article.php?id=41548&group=eclipse.tools.emf#41548

Work-around: Add the package to registry at the end of the generated org.eclipse.hyades.models.hierarchy.impl.HierarchyPackageImpl.init() method

[Thanks Dave for your help in providing this reference.]

The question that remains is will this work-around be added to code template for this generated method, assuming it is the correct fix? 
Comment 3 Dave Steinberg CLA 2009-05-10 19:22:35 EDT
Note that you should not be calling init() on the package implementation directly.  You should simply access the interface's eINSTANCE field.

That said, we will make this change in the template.
Comment 4 Dave Steinberg CLA 2009-05-10 20:59:22 EDT
Created attachment 135087 [details]
Updated template and regenerated EMF models

Here's a patch with the template enhancement and all the changes when everything in EMF is regen'ed. In addition to putting the package into the registry at the end of init(), the comment for that method is improved to specify that it shouldn't be called directly by clients.

Ed, does this look good and complete?
Comment 5 Paul Slauenwhite CLA 2009-05-11 07:30:04 EDT
(In reply to comment #3)
> Note that you should not be calling init() on the package implementation
> directly.  You should simply access the interface's eINSTANCE field.
> 
> That said, we will make this change in the template.
> 

Correct.  This was a shortcut in the JUnit Plug-in test.
Comment 6 Dave Steinberg CLA 2009-05-12 11:55:10 EDT
The fix is in CVS for EMF 2.5.
Comment 7 Paul Slauenwhite CLA 2009-05-13 09:14:08 EDT
Verified in the EMF 2.5 I200905121800 build.

Closing.