Java源码示例:org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring

示例1
@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);
}
 
示例2
protected boolean isApplicable(Shell parent, ProcessorBasedRefactoring refactoring) {
	try {
		if (refactoring.isApplicable()) {
			return true;
		}
		showFatalErrorMessage(parent, "Refactoring is not applicable");
	} catch (CoreException e) {
		LOG.error("Error detecting applicability of refactoring", e);
		showFatalErrorMessage(parent, "Cannot apply refactoring. See log for details.");
	}
	return false;
}
 
示例3
@Override
public ProcessorBasedRefactoring getRenameRefactoring(IRenameElementContext renameElementContext) {
	RenameProcessor processor = getRenameProcessor(renameElementContext);
	if (processor != null) {
		return new RenameRefactoring(processor);
	}
	return null;
}
 
示例4
@Override
public ProcessorBasedRefactoring getRenameRefactoring(IRenameElementContext renameElementContext) {
	if (renameElementContext instanceof CombinedJvmJdtRenameContext) {
		RenameProcessor renameProcessor = getRenameProcessor(renameElementContext);
		if (renameProcessor != null) {
			return createChangeCombiningRefactoring(renameProcessor);
		}
		return null;
	}
	return super.getRenameRefactoring(renameElementContext);
}
 
示例5
protected RefactoringStatus renameXtendElementWithError(final XtextEditor editor, final int offset, String newName, IProgressMonitor monitor) throws Exception {
	syncUtil.totalSync(false);
	ProcessorBasedRefactoring renameRefactoring = createXtendRenameRefactoring(editor, offset, newName);
	RefactoringStatus status = renameRefactoring.checkAllConditions(monitor);
	assertFalse("Expected an error", status.isOK());
	return status;
}
 
示例6
protected ProcessorBasedRefactoring createXtendRenameRefactoring(final XtextEditor editor, final int offset,
		String newName) {
	IRenameElementContext renameElementContext = createRenameElementContext(editor, offset);
	ProcessorBasedRefactoring renameRefactoring = renameRefactoringProvider
			.getRenameRefactoring(renameElementContext);
	RefactoringProcessor processor = renameRefactoring.getProcessor();
	if (processor instanceof AbstractRenameProcessor)
		((AbstractRenameProcessor) processor).setNewName(newName);
	else if (processor instanceof JavaRenameProcessor)
		((JavaRenameProcessor) processor).setNewElementName(newName);
	return renameRefactoring;
}
 
示例7
@Fix(IssueCodes.WRONG_FILE)
public void fixWrongFileRenameClass(final Issue issue, final IssueResolutionAcceptor acceptor) {
	URI uri = issue.getUriToProblem();
	String className = uri.trimFileExtension().lastSegment();
	String label = String.format("Rename class to '%s'", className);
	acceptor.accept(issue, label, label, null, (element, context) -> {
		context.getXtextDocument().modify(resource -> {
			IRenameElementContext renameContext = renameContextFactory.createRenameElementContext(element, null,
					new TextSelection(context.getXtextDocument(), issue.getOffset(), issue.getLength()), resource);
			final ProcessorBasedRefactoring refactoring = renameRefactoringProvider.getRenameRefactoring(renameContext);
			((RenameElementProcessor) refactoring.getProcessor()).setNewName(className);
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, monitor -> {
				try {
					if (!refactoring.checkFinalConditions(monitor).isOK())
						return;
					Change change = refactoring.createChange(monitor);
					change.initializeValidationData(monitor);
					PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
					performChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), refactoring.getName());
					performChangeOperation.setSchedulingRule(ResourcesPlugin.getWorkspace().getRoot());
					performChangeOperation.run(monitor);
				} catch (CoreException e) {
					logger.error(e);
				}
			});
			return null;
		});
	});
}
 
示例8
private Change createChangeForMethodRename(IMethod methodToRename)
    throws RefactoringException {
  MethodRenameChangeBuilder builder = new MethodRenameChangeBuilder(
      methodToRename, newMethodName, processor,
      methodToRename.getJavaProject().getProject().getWorkspace());

  ProcessorBasedRefactoring refactoring = (ProcessorBasedRefactoring) builder.createRefactoring();
  RefactoringProcessor nestedProcessor = refactoring.getProcessor();
  NestedRefactoringContext.storeForProcessor(
      nestedProcessor,
      MethodRenameRefactoringContext.newNestedRefactoringContext(refactoringContext));

  return builder.createChange();
}
 
示例9
private Change createChangeForInterfaceRename() throws RefactoringException {
  InterfaceRenameChangeBuilder builder = new InterfaceRenameChangeBuilder(
      typeContainer.getPairedType(),
      pairedNewName,
      processor,
      typeContainer.getPairedType().getJavaProject().getProject().getWorkspace());

  ProcessorBasedRefactoring refactoring = (ProcessorBasedRefactoring) builder.createRefactoring();
  RefactoringProcessor nestedProcessor = refactoring.getProcessor();
  NestedRefactoringContext.storeForProcessor(
      nestedProcessor,
      InterfaceRenameRefactoringContext.newNestedRefactoringContext(typeContainer.getBaseType()));

  return builder.createChange();
}
 
示例10
/**
 * {@inheritDoc}
 */
@Override
public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	PushDownRefactoringProcessor processor= new PushDownRefactoringProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例11
/**
 * {@inheritDoc}
 */
@Override
public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	UseSuperTypeProcessor processor= new UseSuperTypeProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例12
/**
 * {@inheritDoc}
 */
@Override
public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	PullUpRefactoringProcessor processor= new PullUpRefactoringProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例13
/**
 * {@inheritDoc}
 */
@Override
public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	ExtractSupertypeProcessor processor= new ExtractSupertypeProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例14
@Override
public Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	if (descriptor instanceof IntroduceParameterObjectDescriptor) {
		IntroduceParameterObjectProcessor processor= new IntroduceParameterObjectProcessor((IntroduceParameterObjectDescriptor) descriptor);
		return new ProcessorBasedRefactoring(processor);
	}
	return null;
}
 
示例15
/**
 * {@inheritDoc}
 */
@Override
public Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	ChangeSignatureProcessor processor= new ChangeSignatureProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例16
/**
 * {@inheritDoc}
 */
@Override
public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
	JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
	ExtractInterfaceProcessor processor= new ExtractInterfaceProcessor(arguments, status);
	return new ProcessorBasedRefactoring(processor);
}
 
示例17
public static void startChangeSignatureRefactoring(final IMethod method, final SelectionDispatchAction action, final Shell shell) throws JavaModelException {
	if (!RefactoringAvailabilityTester.isChangeSignatureAvailable(method))
		return;
	try {
		ChangeSignatureProcessor processor= new ChangeSignatureProcessor(method);
		RefactoringStatus status= processor.checkInitialConditions(new NullProgressMonitor());
		if (status.hasFatalError()) {
			final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
			if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
				Object element= entry.getData();
				if (element != null) {
					String message= Messages.format(RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion, entry.getMessage());
					if (MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
						IStructuredSelection selection= new StructuredSelection(element);
						// TODO: should not hijack this
						// ModifiyParametersAction.
						// The action is set up on an editor, but we use it
						// as if it were set up on a ViewPart.
						boolean wasEnabled= action.isEnabled();
						action.selectionChanged(selection);
						if (action.isEnabled()) {
							action.run(selection);
						} else {
							MessageDialog.openInformation(shell, ActionMessages.ModifyParameterAction_problem_title, ActionMessages.ModifyParameterAction_problem_message);
						}
						action.setEnabled(wasEnabled);
					}
				}
				return;
			}
		}

		Refactoring refactoring= new ProcessorBasedRefactoring(processor);
		ChangeSignatureWizard wizard= new ChangeSignatureWizard(processor, refactoring);
		new RefactoringStarter().activate(wizard, shell, wizard.getDefaultPageTitle(), RefactoringSaveHelper.SAVE_REFACTORING);
	} catch (CoreException e) {
		ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.RefactoringStarter_unexpected_exception);
	}
}
 
示例18
public static void startExtractSupertypeRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
	if (!RefactoringAvailabilityTester.isExtractSupertypeAvailable(members))
		return;
	IJavaProject project= null;
	if (members != null && members.length > 0)
		project= members[0].getJavaProject();
	ExtractSupertypeProcessor processor= new ExtractSupertypeProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project));
	Refactoring refactoring= new ProcessorBasedRefactoring(processor);
	ExtractSupertypeWizard wizard= new ExtractSupertypeWizard(processor, refactoring);
	new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
}
 
示例19
public static void startPullUpRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
	if (!RefactoringAvailabilityTester.isPullUpAvailable(members))
		return;
	IJavaProject project= null;
	if (members != null && members.length > 0)
		project= members[0].getJavaProject();
	PullUpRefactoringProcessor processor= new PullUpRefactoringProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project));
	Refactoring refactoring= new ProcessorBasedRefactoring(processor);
	new RefactoringStarter().activate(new PullUpWizard(processor, refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
}
 
示例20
public static void startPushDownRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
	if (!RefactoringAvailabilityTester.isPushDownAvailable(members))
		return;
	PushDownRefactoringProcessor processor= new PushDownRefactoringProcessor(members);
	Refactoring refactoring= new ProcessorBasedRefactoring(processor);
	PushDownWizard wizard= new PushDownWizard(processor, refactoring);
	new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
}
 
示例21
public static void startIntroduceParameterObject(IMethod method, Shell shell) throws CoreException {
	RefactoringStatus availability= Checks.checkAvailability(method);
	if (availability.hasError()){
		MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
		return;
	}
	IntroduceParameterObjectDescriptor ipod= RefactoringSignatureDescriptorFactory.createIntroduceParameterObjectDescriptor();
	ipod.setMethod(method);

	IntroduceParameterObjectProcessor processor= new IntroduceParameterObjectProcessor(ipod);

	final RefactoringStatus status= processor.checkInitialConditions(new NullProgressMonitor());
	if (status.hasFatalError()) {
		final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
		if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
			final Object element= entry.getData();
			IMethod superMethod= (IMethod) element;
			availability= Checks.checkAvailability(superMethod);
			if (availability.hasError()){
				MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
				return;
			}
			String message= Messages.format(RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion, entry.getMessage());
			if (element != null && MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
				ipod= RefactoringSignatureDescriptorFactory.createIntroduceParameterObjectDescriptor();
				ipod.setMethod(superMethod);
				processor= new IntroduceParameterObjectProcessor(ipod);
			}
			else processor=null;
		}
	}
	if (processor != null) {
		Refactoring refactoring= new ProcessorBasedRefactoring(processor);
		IntroduceParameterObjectWizard wizard= new IntroduceParameterObjectWizard(processor, refactoring);
		new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
	}
}
 
示例22
/**
 * Rename refactoring Xpect method
 */
// Note: arg1=OFFSET makes the 'offset' parameter contain the right offset value
@ParameterParser(syntax = "('at' arg2=OFFSET 'to' arg3=STRING) ('resource' arg4=STRING)?")
@Xpect
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void renameRefactoring(
		@StringDiffExpectation(whitespaceSensitive = false) IStringDiffExpectation expectation, // arg0
		@ThisResource XtextResource resource, // arg1
		IEObjectCoveringRegion offset, // arg2
		String newName, // arg3
		String specifiedResourcePath, // arg4
		@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectedResult)
		throws Exception {
	try {
		EObject context = offset.getEObject();
		EObject selectedElement = offsetHelper.resolveElementAt((XtextResource) context.eResource(),
				offset.getOffset());

		// LiteralOrComputedPropertyName does not have a type model but its container does
		if (selectedElement instanceof LiteralOrComputedPropertyName) {
			selectedElement = selectedElement.eContainer();
		}

		// An IdentifierRef refers to an AST FormalParameter and not TFormalParameter
		if (!(selectedElement instanceof FormalParameter)
				&& (N4JSLanguageUtils.getDefinedTypeModelElement(selectedElement) != null)) {
			selectedElement = N4JSLanguageUtils.getDefinedTypeModelElement(selectedElement);
		}

		// while (selectedElement != null) {
		// while (Display.getCurrent().readAndDispatch())
		// ;
		// Display.getCurrent().sleep();
		// }

		URI targetResourceUri = context.eResource().getURI();
		Optional<XtextEditor> editorOp = EditorsUtil.openXtextEditor(targetResourceUri,
				N4JSActivator.ORG_ECLIPSE_N4JS_N4JS);
		XtextEditor editor = editorOp.get();
		final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();

		IRenameElementContext renameElementContext = renameContextFactory
				.createRenameElementContext(
						selectedElement, editor, selection, resource);

		IRenameSupport renameSupport = renameSupportFactory.create(renameElementContext, newName);

		// HACK, use reflection to obtain the private field 'renameRefactoring' since we need it to verify the
		// conditions
		// Field field = renameSupport.getClass().getDeclaredField("renameRefactoring");
		// field.setAccessible(true);
		ProcessorBasedRefactoring refactoring = (ProcessorBasedRefactoring) ReflectionUtil.getFieldValue(
				renameSupport,
				"renameRefactoring");

		RefactoringStatus status = refactoring.checkAllConditions(new NullProgressMonitor());
		// If rename refactoring's conditions are not satisfied, validate the error message
		if (status.hasError()) {
			RefactoringStatusEntry[] entries = status.getEntries();
			List<String> errorMessages = Arrays.stream(entries).map(statusEntry -> statusEntry.getMessage())
					.collect(Collectors.toList());

			expectedResult.assertEquals(errorMessages);
		} else {
			String beforeRenameContent = getResourceContentWithoutXpectComment(specifiedResourcePath, resource);
			renameSupport.startDirectRefactoring();
			String afterRenameContent = getResourceContentWithoutXpectComment(specifiedResourcePath, resource);

			expectation.assertDiffEquals(beforeRenameContent, afterRenameContent);
		}
	} finally {
		EditorsUtil.forceCloseAllEditors();
	}
}
 
示例23
public RenameElementWizard(ProcessorBasedRefactoring refactoring, IRenameElementContext context) {
	super(refactoring, DIALOG_BASED_USER_INTERFACE);
	setWindowTitle("Rename Element");
	this.context = context;
	renameProcessor = (AbstractRenameProcessor) refactoring.getProcessor();
}
 
示例24
public CheckConditionsAndCreateChangeRunnable(Shell shell, ProcessorBasedRefactoring refactoring) {
	this.shell = shell;
	this.refactoring = refactoring;
}
 
示例25
protected ProcessorBasedRefactoring createChangeCombiningRefactoring(RenameProcessor renameProcessor) {
	return new ChangeCombiningRenameRefactoring(renameProcessor, textChangeCombiner);
}
 
示例26
public static void startExtractInterfaceRefactoring(final IType type, final Shell shell) {
	ExtractInterfaceProcessor processor= new ExtractInterfaceProcessor(type, JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()));
	Refactoring refactoring= new ProcessorBasedRefactoring(processor);
	new RefactoringStarter().activate(new ExtractInterfaceWizard(processor, refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring,
			RefactoringSaveHelper.SAVE_REFACTORING);
}
 
示例27
public static void startUseSupertypeRefactoring(final IType type, final Shell shell) {
	UseSuperTypeProcessor processor= new UseSuperTypeProcessor(type);
	Refactoring refactoring= new ProcessorBasedRefactoring(processor);
	UseSupertypeWizard wizard= new UseSupertypeWizard(processor, refactoring);
	new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
}
 
示例28
ProcessorBasedRefactoring getRenameRefactoring(IRenameElementContext renameElementContext);