use of org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring in project che by eclipse.
the class RenamePackageTest method helper2.
private RenamePackageProcessor helper2(String[] packageNames, String[][] packageFileNames, String newPackageName) throws Exception {
ParticipantTesting.reset();
IPackageFragment[] packages = new IPackageFragment[packageNames.length];
ICompilationUnit[][] cus = new ICompilationUnit[packageFileNames.length][packageFileNames[0].length];
for (int i = 0; i < packageNames.length; i++) {
packages[i] = getRoot().createPackageFragment(packageNames[i], true, null);
for (int j = 0; j < packageFileNames[i].length; j++) {
cus[i][j] = createCUfromTestFile(packages[i], packageFileNames[i][j], packageNames[i].replace('.', '/') + "/");
}
}
IPackageFragment thisPackage = packages[0];
boolean hasSubpackages = thisPackage.hasSubpackages();
IPath path = thisPackage.getParent().getPath();
path = path.append(newPackageName.replace('.', '/'));
IFolder target = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
boolean targetExists = target.exists();
boolean isRename = !targetExists && !thisPackage.hasSubpackages() && thisPackage.getResource().getParent().equals(target.getParent());
String[] createHandles = null;
String[] moveHandles = null;
String[] deleteHandles = null;
boolean doDelete = true;
String[] renameHandles = null;
if (isRename) {
renameHandles = ParticipantTesting.createHandles(thisPackage, thisPackage.getResource());
} else {
renameHandles = ParticipantTesting.createHandles(thisPackage);
IContainer loop = target;
List handles = new ArrayList();
while (loop != null && !loop.exists()) {
handles.add(ParticipantTesting.createHandles(loop)[0]);
loop = loop.getParent();
}
createHandles = (String[]) handles.toArray(new String[handles.size()]);
IFolder source = (IFolder) thisPackage.getResource();
deleteHandles = ParticipantTesting.createHandles(source);
IResource[] members = source.members();
List movedObjects = new ArrayList();
for (int i = 0; i < members.length; i++) {
if (members[i] instanceof IFolder) {
doDelete = false;
} else {
movedObjects.add(members[i]);
}
}
moveHandles = ParticipantTesting.createHandles(movedObjects.toArray());
}
RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(thisPackage, newPackageName);
descriptor.setUpdateReferences(fUpdateReferences);
descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
setFilePatterns(descriptor);
Refactoring refactoring = createRefactoring(descriptor);
RefactoringStatus result = performRefactoring(refactoring);
TestCase.assertEquals("preconditions were supposed to pass", null, result);
if (isRename) {
ParticipantTesting.testRename(renameHandles, new RenameArguments[] { new RenameArguments(newPackageName, fUpdateReferences), new RenameArguments(target.getName(), fUpdateReferences) });
} else {
ParticipantTesting.testRename(renameHandles, new RenameArguments[] { new RenameArguments(newPackageName, fUpdateReferences) });
ParticipantTesting.testCreate(createHandles);
List args = new ArrayList();
for (int i = 0; i < packageFileNames[0].length; i++) {
args.add(new MoveArguments(target, fUpdateReferences));
}
ParticipantTesting.testMove(moveHandles, (MoveArguments[]) args.toArray(new MoveArguments[args.size()]));
if (doDelete) {
ParticipantTesting.testDelete(deleteHandles);
} else {
ParticipantTesting.testDelete(new String[0]);
}
}
if (hasSubpackages) {
assertTrue("old package does not exist anymore", getRoot().getPackageFragment(packageNames[0]).exists());
} else {
assertTrue("package not renamed", !getRoot().getPackageFragment(packageNames[0]).exists());
}
IPackageFragment newPackage = getRoot().getPackageFragment(newPackageName);
assertTrue("new package does not exist", newPackage.exists());
for (int i = 0; i < packageFileNames.length; i++) {
String packageName = (i == 0) ? newPackageName.replace('.', '/') + "/" : packageNames[i].replace('.', '/') + "/";
for (int j = 0; j < packageFileNames[i].length; j++) {
String s1 = getFileContents(getOutputTestFileName(packageFileNames[i][j], packageName));
ICompilationUnit cu = (i == 0) ? newPackage.getCompilationUnit(packageFileNames[i][j] + ".java") : cus[i][j];
//DebugUtils.dump("cu:" + cu.getElementName());
String s2 = cu.getSource();
//DebugUtils.dump("expected:" + s1);
//DebugUtils.dump("was:" + s2);
assertEqualLines("invalid update in file " + cu.getElementName(), s1, s2);
}
}
RefactoringProcessor processor = ((ProcessorBasedRefactoring) refactoring).getProcessor();
return (RenamePackageProcessor) processor;
}
use of org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring in project jbosstools-hibernate by jbosstools.
the class HibernateConsolePlugin method listenForConfigurations.
private void listenForConfigurations() {
final ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
kcl = new KnownConfigurationsAdapter() {
/**
* @param root
* @param forUpdate - shows whether physical removal necessary
*/
public void configurationRemoved(ConsoleConfiguration root, boolean forUpdate) {
if (!forUpdate) {
try {
removeConfiguration(root.getName());
} catch (CoreException e) {
logErrorMessage(HibernateConsoleMessages.HibernateConsolePlugin_could_not_delete_launch_config_for + root.getName(), e);
}
}
}
};
KnownConfigurations.getInstance().addConsoleConfigurationListener(kcl);
icl = new ILaunchConfigurationListener() {
boolean isConsoleConfiguration(ILaunchConfiguration configuration) {
try {
return configuration.getType().getIdentifier().equals(ICodeGenerationLaunchConstants.CONSOLE_CONFIGURATION_LAUNCH_TYPE_ID);
} catch (CoreException e) {
// HibernateConsolePlugin.getDefault().log( e );
// ignore since it occurs on delete
}
return false;
}
public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
ConsoleConfiguration cfg = KnownConfigurations.getInstance().find(configuration.getName());
if (cfg != null) {
// file system removal have been made already.
KnownConfigurations.getInstance().removeConfiguration(cfg, true);
}
}
public void launchConfigurationChanged(ILaunchConfiguration configuration) {
if (configuration.isWorkingCopy() || isTemporary(configuration)) {
return;
}
if (isConsoleConfiguration(configuration)) {
KnownConfigurations instance = KnownConfigurations.getInstance();
ConsoleConfiguration oldcfg = instance.find(configuration.getName());
if (oldcfg != null) {
// reset it no matter what.
oldcfg.reset();
} else {
// A new one!
ConsoleConfigurationPreferences adapter = buildConfigurationPreferences(configuration);
instance.addConfiguration(new ConsoleConfiguration(adapter), true);
}
}
}
private ConsoleConfigurationPreferences buildConfigurationPreferences(ILaunchConfiguration configuration) {
return new EclipseLaunchConsoleConfigurationPreferences(configuration);
}
public void launchConfigurationAdded(ILaunchConfiguration configuration) {
if (isConsoleConfiguration(configuration)) {
ILaunchConfiguration movedFrom = launchManager.getMovedFrom(configuration);
if (movedFrom != null && isConsoleConfiguration(movedFrom)) {
KnownConfigurations instance = KnownConfigurations.getInstance();
ConsoleConfiguration oldcfg = instance.find(movedFrom.getName());
if (oldcfg != null) {
// call this before we remove old configuration
refactor(movedFrom, configuration);
// reset it no matter what.
oldcfg.reset();
instance.removeConfiguration(oldcfg, false);
}
}
KnownConfigurations instance = KnownConfigurations.getInstance();
ConsoleConfigurationPreferences adapter = buildConfigurationPreferences(configuration);
boolean temporary = isTemporary(configuration);
if (!temporary) {
instance.addConfiguration(new ConsoleConfiguration(adapter), true);
}
}
}
private void refactor(ILaunchConfiguration oldConfiguration, ILaunchConfiguration newConfiguration) {
if (!oldConfiguration.getName().equals(newConfiguration.getName())) {
// only rename of console configuration refactoring is supported.
ConsoleConfigurationRenameProcessor proc = new ConsoleConfigurationRenameProcessor(oldConfiguration, newConfiguration.getName());
// Refactor for rename
PerformRefactoringOperation refOperation = new PerformRefactoringOperation(new ProcessorBasedRefactoring(proc), CheckConditionsOperation.ALL_CONDITIONS);
try {
ResourcesPlugin.getWorkspace().run(refOperation, null);
} catch (OperationCanceledException oce) {
throw new OperationCanceledException();
} catch (CoreException ce) {
HibernateConsolePlugin.openError(new Shell(), HibernateConsoleMessages.EditConsoleConfiguration_rename_refactoring_error_totle, ce.getLocalizedMessage(), ce, HibernateConsolePlugin.PERFORM_SYNC_EXEC);
}
}
}
private boolean isTemporary(ILaunchConfiguration configuration) {
boolean temporary = true;
try {
temporary = configuration.getAttribute(AddConfigurationAction.TEMPORARY_CONFIG_FLAG, false);
} catch (CoreException e) {
HibernateConsolePlugin.getDefault().showError(getShell(), HibernateConsoleMessages.HibernateConsolePlugin_problem_to_get_flag, e);
}
return temporary;
}
};
launchManager.addLaunchConfigurationListener(icl);
}
use of org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method fixWrongFileRenameClass.
@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;
});
});
}
use of org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring in project che by eclipse.
the class RenameParametersTest method helper1.
private void helper1(String[] newNames, String[] signature) throws Exception {
ICompilationUnit cu = createCUfromTestFile(getPackageP(), true, true);
IType classA = getType(cu, "A");
IMethod method = classA.getMethod("m", signature);
Assert.assertTrue("refactoring not available", RefactoringAvailabilityTester.isChangeSignatureAvailable(method));
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(method);
Refactoring ref = new ProcessorBasedRefactoring(processor);
//ref.setUpdateReferences(updateReferences);
//ref.setNewParameterNames(newNames);
//ref.setNewNames(createRenamings(method, newNames));
modifyInfos(processor.getParameterInfos(), newNames);
RefactoringStatus result = performRefactoring(ref);
Assert.assertEquals("precondition was supposed to pass", null, result);
IPackageFragment pack = (IPackageFragment) cu.getParent();
String newCuName = getSimpleTestFileName(true, true);
ICompilationUnit newcu = pack.getCompilationUnit(newCuName);
Assert.assertTrue(newCuName + " does not exist", newcu.exists());
Assert.assertEquals("invalid renaming", getFileContents(getTestFileName(true, false)).length(), newcu.getSource().length());
assertEqualLines("invalid renaming", getFileContents(getTestFileName(true, false)), newcu.getSource());
}
use of org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring in project che by eclipse.
the class RenameParametersTest method helper2.
private void helper2(String[] newNames, String[] signature) throws Exception {
IType classA = getType(createCUfromTestFile(getPackageP(), false, false), "A");
//DebugUtils.dump("classA" + classA);
IMethod method = classA.getMethod("m", signature);
Assert.assertTrue("refactoring not available", RefactoringAvailabilityTester.isChangeSignatureAvailable(method));
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(method);
Refactoring ref = new ProcessorBasedRefactoring(processor);
modifyInfos(processor.getParameterInfos(), newNames);
RefactoringStatus result = performRefactoring(ref);
Assert.assertNotNull("precondition was supposed to fail", result);
}
Aggregations