Search in sources :

Example 6 with RenameRefactoring

use of org.eclipse.ltk.core.refactoring.participants.RenameRefactoring in project che by eclipse.

the class RenameResourceDescriptor method createRefactoring.

/* (non-Javadoc)
	 * @see org.eclipse.ltk.core.refactoring.RefactoringDescriptor#createRefactoring(org.eclipse.ltk.core.refactoring.RefactoringStatus)
	 */
public Refactoring createRefactoring(RefactoringStatus status) throws CoreException {
    IPath resourcePath = getResourcePath();
    if (resourcePath == null) {
        status.addFatalError(RefactoringCoreMessages.RenameResourceDescriptor_error_path_not_set);
        return null;
    }
    IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(resourcePath);
    if (resource == null || !resource.exists()) {
        status.addFatalError(Messages.format(RefactoringCoreMessages.RenameResourceDescriptor_error_resource_not_existing, BasicElementLabels.getPathLabel(resourcePath, false)));
        return null;
    }
    String newName = getNewName();
    if (newName == null || newName.length() == 0) {
        status.addFatalError(RefactoringCoreMessages.RenameResourceDescriptor_error_name_not_defined);
        return null;
    }
    RenameResourceProcessor processor = new RenameResourceProcessor(resource);
    processor.setNewResourceName(newName);
    processor.setUpdateReferences(isUpdateReferences());
    return new RenameRefactoring(processor);
}
Also used : RenameResourceProcessor(org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor) IPath(org.eclipse.core.runtime.IPath) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) IResource(org.eclipse.core.resources.IResource)

Example 7 with RenameRefactoring

use of org.eclipse.ltk.core.refactoring.participants.RenameRefactoring in project che by eclipse.

the class RenameNonPrivateFieldTest method helper2.

private void helper2(String fieldName, String newFieldName, boolean createDelegates) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType classA = getType(cu, "A");
    IField field = classA.getField(fieldName);
    boolean isEnum = JdtFlags.isEnum(field);
    String id = isEnum ? IJavaRefactorings.RENAME_ENUM_CONSTANT : IJavaRefactorings.RENAME_FIELD;
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(id);
    descriptor.setJavaElement(field);
    descriptor.setNewName(newFieldName);
    descriptor.setUpdateReferences(fUpdateReferences);
    descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
    if (!isEnum) {
        descriptor.setRenameGetters(fRenameGetter);
        descriptor.setRenameSetters(fRenameSetter);
        descriptor.setKeepOriginal(createDelegates);
        descriptor.setDeprecateDelegate(true);
    }
    RenameRefactoring refactoring = (RenameRefactoring) createRefactoring(descriptor);
    RenameFieldProcessor processor = (RenameFieldProcessor) refactoring.getProcessor();
    List elements = new ArrayList();
    elements.add(field);
    List args = new ArrayList();
    args.add(new RenameArguments(newFieldName, fUpdateReferences));
    if (fRenameGetter) {
        elements.add(processor.getGetter());
        args.add(new RenameArguments(processor.getNewGetterName(), fUpdateReferences));
    }
    if (fRenameSetter) {
        elements.add(processor.getSetter());
        args.add(new RenameArguments(processor.getNewSetterName(), fUpdateReferences));
    }
    String[] renameHandles = ParticipantTesting.createHandles(elements.toArray());
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    ParticipantTesting.testRename(renameHandles, (RenameArguments[]) args.toArray(new RenameArguments[args.size()]));
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RenameFieldProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType) ArrayList(java.util.ArrayList) List(java.util.List) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 8 with RenameRefactoring

use of org.eclipse.ltk.core.refactoring.participants.RenameRefactoring in project che by eclipse.

the class RenamePackageTest method testPackageRenameWithResource1.

@Test
public void testPackageRenameWithResource1() throws Exception {
    IPackageFragment fragment = getRoot().createPackageFragment("org.test", true, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package org.test;\n");
    buf.append("public class MyClass {\n");
    buf.append("	org.test.MyClass me;\n");
    buf.append("}\n");
    fragment.createCompilationUnit("MyClass.java", buf.toString(), true, null);
    IFile file = ((IFolder) getRoot().getResource()).getFile("x.properties");
    byte[] content = "This is about 'org.test' and more".getBytes();
    file.create(new ByteArrayInputStream(content), true, null);
    file.refreshLocal(IResource.DEPTH_ONE, null);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
    descriptor.setJavaElement(fragment);
    descriptor.setNewName("org.test2");
    descriptor.setUpdateReferences(true);
    descriptor.setUpdateQualifiedNames(true);
    descriptor.setFileNamePatterns("*.properties");
    Refactoring refactoring = createRefactoring(descriptor);
    RefactoringStatus status = performRefactoring(refactoring);
    if (status != null)
        assertTrue(status.toString(), status.isOK());
    RefactoringProcessor processor = ((RenameRefactoring) refactoring).getProcessor();
    IResourceMapper rm = (IResourceMapper) processor.getAdapter(IResourceMapper.class);
    IJavaElementMapper jm = (IJavaElementMapper) processor.getAdapter(IJavaElementMapper.class);
    checkMappingUnchanged(jm, rm, new Object[] { getRoot().getJavaProject(), getRoot(), file });
    IFile newFile = ((IContainer) getRoot().getResource()).getFile(new Path("x.properties"));
    assertEquals("This is about 'org.test2' and more", getContents(newFile));
    checkMappingChanged(jm, rm, new Object[][] { { fragment, getRoot().getPackageFragment("org.test2") } });
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IResourceMapper(org.eclipse.ltk.core.refactoring.IResourceMapper) ByteArrayInputStream(java.io.ByteArrayInputStream) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IContainer(org.eclipse.core.resources.IContainer) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 9 with RenameRefactoring

use of org.eclipse.ltk.core.refactoring.participants.RenameRefactoring in project che by eclipse.

the class Rename18Test method renameLocalVariable.

private void renameLocalVariable(String newFieldName, boolean updateReferences) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    ISourceRange selection = getSelection(cu);
    IJavaElement[] elements = cu.codeSelect(selection.getOffset(), selection.getLength());
    assertEquals(1, elements.length);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_LOCAL_VARIABLE);
    descriptor.setJavaElement(elements[0]);
    descriptor.setNewName(newFieldName);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setUpdateTextualOccurrences(false);
    RenameRefactoring refactoring = (RenameRefactoring) createRefactoring(descriptor);
    List list = new ArrayList();
    list.add(elements[0]);
    List args = new ArrayList();
    args.add(new RenameArguments(newFieldName, updateReferences));
    String[] renameHandles = ParticipantTesting.createHandles(list.toArray());
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    ParticipantTesting.testRename(renameHandles, (RenameArguments[]) args.toArray(new RenameArguments[args.size()]));
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayList(java.util.ArrayList) List(java.util.List) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 10 with RenameRefactoring

use of org.eclipse.ltk.core.refactoring.participants.RenameRefactoring in project che by eclipse.

the class RenameMethodInInterfaceTest method helper1_not_available.

private void helper1_not_available(String methodName, String[] signatures) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType interfaceI = getType(cu, "I");
    RenameProcessor processor = new RenameVirtualMethodProcessor(interfaceI.getMethod(methodName, signatures));
    RenameRefactoring ref = new RenameRefactoring(processor);
    assertTrue(!ref.isApplicable());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameProcessor(org.eclipse.ltk.core.refactoring.participants.RenameProcessor) RenameVirtualMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) IType(org.eclipse.jdt.core.IType)

Aggregations

RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)36 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)13 IType (org.eclipse.jdt.core.IType)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 JavaRefactoringArguments (org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments)8 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)5 RenameMethodProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameMethodProcessor)5 RenameTypeParameterProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor)5 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)5 RefactoringWizard (org.eclipse.ltk.ui.refactoring.RefactoringWizard)5 RefactoringWizardOpenOperation (org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation)5 RenameRefactoringWizard (org.eclipse.wst.xsd.ui.internal.refactor.wizard.RenameRefactoringWizard)5 CoreException (org.eclipse.core.runtime.CoreException)4 IMethod (org.eclipse.jdt.core.IMethod)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 RenameVirtualMethodProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IFile (org.eclipse.core.resources.IFile)3