Java Code Examples for org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor#createRefactoring()

The following examples show how to use org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor#createRefactoring() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GhidraModuleUtils.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Renames the given Java element to the given new name.  Currently only supports renaming 
 * packages and compilation units.
 * 
 * @param element The Java element to rename.
 * @param newName The desired new name of the element.
 * @param monitor The progress monitor.
 * @throws CoreException If there is an Eclipse-related problem with the rename.
 * @throws IllegalArgumentException If the given Java element is not a package or compilation unit.
 */
private static void renameJavaElement(IJavaElement element, String newName,
		IProgressMonitor monitor) throws CoreException, IllegalArgumentException {
	String id;
	if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
		id = IJavaRefactorings.RENAME_PACKAGE;
	}
	else if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
		id = IJavaRefactorings.RENAME_COMPILATION_UNIT;
	}
	else {
		throw new IllegalArgumentException("Can only rename packages and compilation units!");
	}
	RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(id);
	RenameJavaElementDescriptor descriptor =
		(RenameJavaElementDescriptor) contribution.createDescriptor();
	descriptor.setProject(element.getResource().getProject().getName());
	descriptor.setNewName(newName);
	descriptor.setJavaElement(element);
	RefactoringStatus status = new RefactoringStatus();
	Refactoring refactoring = descriptor.createRefactoring(status);
	refactoring.checkInitialConditions(monitor);
	refactoring.checkFinalConditions(monitor);
	Change change = refactoring.createChange(monitor);
	change.perform(monitor);
}
 
Example 2
Source File: JvmRenameRefactoringProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ProcessorBasedRefactoring getRenameRefactoring(IRenameElementContext renameElementContext) {
	if (renameElementContext instanceof JdtRefactoringContext) {
		IJavaElement javaElement = ((JdtRefactoringContext) renameElementContext).getJavaElement();
		if (isJavaSource(javaElement)) {
			try {
				RenameJavaElementDescriptor renameDescriptor = createRenameDescriptor(javaElement,
						javaElement.getElementName());
				return (ProcessorBasedRefactoring) renameDescriptor.createRefactoring(new RefactoringStatus());
			} catch (Exception exc) {
				throw new WrappedException(exc);
			}
		}
	}
	return super.getRenameRefactoring(renameElementContext);
}
 
Example 3
Source File: RenameSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RenameSupport(RenameJavaElementDescriptor descriptor) throws CoreException {
	RefactoringStatus refactoringStatus= new RefactoringStatus();
	fRefactoring= (RenameRefactoring) descriptor.createRefactoring(refactoringStatus);
	if (refactoringStatus.hasFatalError()) {
		fPreCheckStatus= refactoringStatus;
	} else {
		preCheck();
		refactoringStatus.merge(fPreCheckStatus);
		fPreCheckStatus= refactoringStatus;
	}
}
 
Example 4
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RenameSupport(RenameJavaElementDescriptor descriptor) throws CoreException {
	RefactoringStatus refactoringStatus= new RefactoringStatus();
	fRefactoring= (RenameRefactoring) descriptor.createRefactoring(refactoringStatus);
	if (refactoringStatus.hasFatalError()) {
		fPreCheckStatus= refactoringStatus;
	} else {
		preCheck();
		refactoringStatus.merge(fPreCheckStatus);
		fPreCheckStatus= refactoringStatus;
	}
}