Bug 11285

Summary: Potential NPE in CopyResourceElementsOperation.processPackageFragmentResource
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 2.0   
Target Milestone: 2.0 M4   
Hardware: All   
OS: All   
Whiteboard:

Description Olivier Thomann CLA 2002-03-13 13:22:46 EST
In latest code (0313), if a package fragment contains an empty file with the extension .java, we 
might have a NPE when trying to move this package fragment. The guilty code is in 
org.eclipse.jdt.internal.core.CopyResourceElementsOperation.processPackageFragmentResource(org.eclipse.jdt.core.IPackageFragment, 
org.eclipse.jdt.core.IPackageFragmentRoot, java.lang.String)

On the line:
String 
cuContents = org.eclipse.jdt.internal.core.Util.normalizeCRs(domCU.getContents(), 
bufferContents);

there is no check to find out that domCU.getContents() can return null. If 
the source file used to create the DOMCompilation unit is empty, getContents() will return null. 
We simply need a null check in this case.
Comment 1 Philipe Mulet CLA 2002-03-14 05:44:20 EST
Please produce a test case, and a fix.
Comment 2 Olivier Thomann CLA 2002-03-15 19:40:13 EST
I added a new empty compilation unit creation in CreateCompilationUnitAndPackageTests. Then 
without the fix, I got this stack trace running the 
JavaModelTests.
ava.lang.NullPointerException
	at 
org.eclipse.jdt.internal.core.Util.normalizeCRs(Util.java:614)
	at 
org.eclipse.jdt.internal.core.CopyResourceElementsOperation.processPackageFragmentResource(CopyResourceElementsOperation.java:370)
	at 
org.eclipse.jdt.internal.core.CopyResourceElementsOperation.processElement(CopyResourceElementsOperation.java:285)
	at 
org.eclipse.jdt.internal.core.MultiOperation.processElements(MultiOperation.java:182)
	at 
org.eclipse.jdt.internal.core.CopyResourceElementsOperation.processElements(CopyResourceElementsOperation.java:299)
	at 
org.eclipse.jdt.internal.core.MultiOperation.executeOperation(MultiOperation.java:86)
	at 
org.eclipse.jdt.internal.core.JavaModelOperation.execute(JavaModelOperation.java:268)
	at 
org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:476)
	at 
org.eclipse.core.internal.resources.Workspace.run(Workspace.java:1292)
	at 
org.eclipse.jdt.internal.core.JavaModelManager.runOperation(JavaModelManager.java:1074)
	at 
org.eclipse.jdt.internal.core.JavaElement.runOperation(JavaElement.java:533)
	at 
org.eclipse.jdt.internal.core.JavaModel.rename(JavaModel.java:422)
	at 
org.eclipse.jdt.internal.core.PackageFragment.rename(PackageFragment.java:289)
	at 
org.eclipse.jdt.tests.core.RenameTests.testRenamePFWithSubPackages(RenameTests.java:661)
	at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at 
junit.extensions.TestDecorator.basicRun(TestDecorator.java:22)
	at 
junit.extensions.TestSetup$1.protect(TestSetup.java:19)
	at 
junit.extensions.TestSetup.run(TestSetup.java:23)
	at 
junit.extensions.TestDecorator.basicRun(TestDecorator.java:22)
	at 
junit.extensions.TestSetup$1.protect(TestSetup.java:19)
	at 
junit.extensions.TestSetup.run(TestSetup.java:23)

The fix simply consists in 
checking the value of the domCU.getContents(). With the fix the NPE is gone.
Comment 3 Olivier Thomann CLA 2002-03-15 19:48:12 EST
Fixed and released in HEAD.
Comment 4 Olivier Thomann CLA 2002-03-15 19:48:21 EST
Fixed.