Search in sources :

Example 6 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor 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 7 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor 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 8 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor 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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) IPath(org.eclipse.core.runtime.IPath) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) MoveArguments(org.eclipse.ltk.core.refactoring.participants.MoveArguments) List(java.util.List) ArrayList(java.util.ArrayList) IContainer(org.eclipse.core.resources.IContainer) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 9 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenamePackageTest method createRefactoringDescriptor.

// -------------
private RenameJavaElementDescriptor createRefactoringDescriptor(IPackageFragment pack, String newName) {
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
    descriptor.setJavaElement(pack);
    descriptor.setNewName(newName);
    descriptor.setUpdateReferences(true);
    return descriptor;
}
Also used : RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 10 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor 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)

Aggregations

RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)40 IType (org.eclipse.jdt.core.IType)24 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)21 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)18 IMethod (org.eclipse.jdt.core.IMethod)12 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)11 Test (org.junit.Test)10 IField (org.eclipse.jdt.core.IField)9 ArrayList (java.util.ArrayList)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)8 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)8 List (java.util.List)7 IJavaProject (org.eclipse.jdt.core.IJavaProject)7 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)6 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3