Community
Participate
Working Groups
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.
Please produce a test case, and a fix.
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.
Fixed and released in HEAD.
Fixed.