Search in sources :

Example 1 with RenamePackageProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor 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 2 with RenamePackageProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor in project che by eclipse.

the class RenamePackageTest method test1.

//-------
@Test
public void test1() throws Exception {
    fIsPreDeltaTest = true;
    RenamePackageProcessor proc = helper2(new String[] { "r" }, new String[][] { { "A" } }, "p1");
    IJavaElementMapper jm = (IJavaElementMapper) proc.getAdapter(IJavaElementMapper.class);
    IResourceMapper rm = (IResourceMapper) proc.getAdapter(IResourceMapper.class);
    IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    IJavaProject project = getRoot().getJavaProject();
    IFile _project = project.getProject().getFile(".project");
    checkMappingUnchanged(jm, rm, new Object[] { project, _project, getRoot(), getPackageP(), getRoot().getPackageFragment("inexistent"), getRoot().getPackageFragment("r.inexistent"), getRoot().getPackageFragment("p1.inexistent") });
    IPackageFragment r = getRoot().getPackageFragment("p1");
    ICompilationUnit r_A = r.getCompilationUnit("A.java");
    IType r_A_A = r_A.getType("A");
    IField r_A_A_a = r_A_A.getField("a");
    IPackageFragment p1 = getRoot().getPackageFragment("p1");
    ICompilationUnit p1_A = p1.getCompilationUnit("A.java");
    IType p1_A_A = p1_A.getType("A");
    IField p1_A_A_a = p1_A_A.getField("a");
    checkMappingChanged(jm, rm, new Object[][] { { r, p1 }, { r_A, p1_A }, { r_A_A, p1_A_A }, { r_A_A_a, p1_A_A_a } });
}
Also used : IResourceMapper(org.eclipse.ltk.core.refactoring.IResourceMapper) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper) IField(org.eclipse.jdt.core.IField) IJavaModel(org.eclipse.jdt.core.IJavaModel) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Example 3 with RenamePackageProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor in project che by eclipse.

the class RenamePackageRefactoringContribution method createRefactoring.

/**
	 * {@inheritDoc}
	 */
@Override
public Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) {
    JavaRefactoringArguments arguments = new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
    RenamePackageProcessor processor = new RenamePackageProcessor(arguments, status);
    return new RenameRefactoring(processor);
}
Also used : JavaRefactoringArguments(org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)

Example 4 with RenamePackageProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor in project che by eclipse.

the class RenamePackageTest method test2.

@Test
public void test2() throws Exception {
    fIsPreDeltaTest = true;
    RenamePackageProcessor processor = helper2(new String[] { "r", "fred" }, new String[][] { { "A" }, { "A" } }, "p1");
    // test that participants are correctly informed after '< Back': https://bugs.eclipse.org/bugs/show_bug.cgi?id=280068
    performUndo();
    ParticipantTesting.reset();
    String secondName = "pipapo";
    processor.setNewElementName(secondName);
    String[] renameHandles = ParticipantTesting.createHandles(new Object[] { processor.getPackage(), processor.getPackage().getResource() });
    RenameRefactoring refactoring = (RenameRefactoring) processor.getRefactoring();
    refactoring.checkFinalConditions(new NullProgressMonitor());
    refactoring.createChange(new NullProgressMonitor());
    ParticipantTesting.testRename(renameHandles, new RenameArguments[] { new RenameArguments(secondName, true), new RenameArguments(secondName, true) });
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) Test(org.junit.Test)

Example 5 with RenamePackageProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor in project bndtools by bndtools.

the class PkgRenameParticipant method initialize.

@Override
protected boolean initialize(Object element) {
    IPackageFragment pkgFragment = (IPackageFragment) element;
    RenameArguments args = getArguments();
    pkgFragments.put(pkgFragment, args);
    StringBuilder sb = new StringBuilder(256);
    sb.append("Bndtools: rename package '");
    sb.append(pkgFragment.getElementName());
    sb.append("' ");
    if (((RenamePackageProcessor) this.getProcessor()).getRenameSubpackages())
        sb.append("and subpackages ");
    sb.append("to '");
    sb.append(args.getNewName());
    sb.append("'");
    changeTitle = sb.toString();
    return true;
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments)

Aggregations

RenamePackageProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)3 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IField (org.eclipse.jdt.core.IField)1 IJavaModel (org.eclipse.jdt.core.IJavaModel)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IType (org.eclipse.jdt.core.IType)1 IJavaElementMapper (org.eclipse.jdt.core.refactoring.IJavaElementMapper)1 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)1