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

Bug 451786

Summary: refactor rename package doesn't update references of the renamed package
Product: [Eclipse Project] JDT Reporter: susheel PM <susheel_pm>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: noopur_gupta, susheel_pm, Vikas.Chandra
Version: 4.3.2   
Target Milestone: 4.5   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description susheel PM CLA 2014-11-17 00:53:42 EST
If you are programmatically updating a package without using UI then the references inside the renamed packages are getting updated; but it doesn't update the files which are being references outside the renamed package.


Below:
**************************************************************************
private void renameRefactorPackageName(String oldName, String newName)
			throws CoreException {
		// Getting the Project
		IProject project = PamConnectorPluginUtil.getCurrentProject();
		project.open(null /* IProgressMonitor */);
		IJavaProject javaProject = JavaCore.create(project);
		IPackageFragment[] ipackageFragments = javaProject.getPackageFragments();
		for (IPackageFragment iPackageFragment : ipackageFragments) {
			if (iPackageFragment.getElementName().contains(oldName)) {
				iPackageFragment.rename(newName, true, null);
			}
		}
}
**************************************************************************

The above code only refactors the name of the package and its child elements; i couldn't find anything which could give me update references flag.



After that i have tried with this code also:
**************************************************************************
 project.open(null);
		    //Getting Class and Compilation Unit
		    IJavaProject javaProject = JavaCore.create(project);
		    IPackageFragmentRoot ipackageFragmentsRoot = javaProject.getPackageFragmentRoot(oldName);
		    IPackageFragment oldPackageFragment = ipackageFragmentsRoot.getPackageFragment(oldName);
		    //RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_PACKAGE);
		    
		    

		    //Refactoring refactoring = PerformRefactoringOperation
		    
		    String id = IJavaRefactorings.RENAME_PACKAGE;
		    RefactoringContribution contrib = RefactoringCore.getRefactoringContribution(id);
		    RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contrib.createDescriptor();
		    descriptor.setProject(project.toString());
		    descriptor.setNewName(newName); 
		    descriptor.setJavaElement(oldPackageFragment);
		    //Executing the refactoring
		    RefactoringStatus status = new RefactoringStatus();
		    Refactoring refactoring = descriptor.createRefactoring(status);
	    
		    IProgressMonitor monitor = new NullProgressMonitor();
		    refactoring.checkInitialConditions(monitor);
		    refactoring.setValidationContext(this);
		    refactoring.checkFinalConditions(monitor);
		    Change change = refactoring.createChange(monitor);
		    change.perform(monitor);


**************************************************************************

In the above code it gives me exception for cannot participate in refactoring.

Is there a way to refactor package with references. Please share a code snippet if possible.

Thanks,
Susheel.
Comment 1 Vikas Chandra CLA 2014-11-17 03:51:37 EST
moving to JDT UI for comments.
Comment 2 Noopur Gupta CLA 2014-11-17 03:59:42 EST
See: org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor for implementation in Eclipse.

Please use the Eclipse JDT forum for such questions:
https://www.eclipse.org/forums/index.php?t=thread&frm_id=13