Search in sources :

Example 1 with ProcessorBasedRefactoring

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;
}
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 ProcessorBasedRefactoring

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);
}
Also used : KnownConfigurationsAdapter(org.hibernate.console.KnownConfigurationsAdapter) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ConsoleConfigurationRenameProcessor(org.hibernate.eclipse.launch.core.refactoring.ConsoleConfigurationRenameProcessor) ILaunchManager(org.eclipse.debug.core.ILaunchManager) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) ILaunchConfigurationListener(org.eclipse.debug.core.ILaunchConfigurationListener) Shell(org.eclipse.swt.widgets.Shell) CoreException(org.eclipse.core.runtime.CoreException) ConsoleConfigurationPreferences(org.hibernate.console.preferences.ConsoleConfigurationPreferences) KnownConfigurations(org.hibernate.console.KnownConfigurations)

Example 3 with ProcessorBasedRefactoring

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;
        });
    });
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) CoreException(org.eclipse.core.runtime.CoreException) TextSelection(org.eclipse.jface.text.TextSelection) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) Change(org.eclipse.ltk.core.refactoring.Change) URI(org.eclipse.emf.common.util.URI) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) RenameElementProcessor(org.eclipse.xtext.ui.refactoring.impl.RenameElementProcessor) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 4 with ProcessorBasedRefactoring

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IType(org.eclipse.jdt.core.IType) ChangeSignatureProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)

Example 5 with ProcessorBasedRefactoring

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);
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IType(org.eclipse.jdt.core.IType) ChangeSignatureProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)

Aggregations

ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)11 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)7 CoreException (org.eclipse.core.runtime.CoreException)4 Change (org.eclipse.ltk.core.refactoring.Change)3 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)3 RefactoringProcessor (org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor)3 IRenameElementContext (org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IMethod (org.eclipse.jdt.core.IMethod)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 IType (org.eclipse.jdt.core.IType)2 JavaRenameProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)2 ChangeSignatureProcessor (org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)2 WorkspaceModifyOperation (org.eclipse.ui.actions.WorkspaceModifyOperation)2 CompositeRefactoringProcessor (org.eclipse.xtext.common.types.ui.refactoring.participant.CompositeRefactoringProcessor)2 AbstractRenameProcessor (org.eclipse.xtext.ui.refactoring.impl.AbstractRenameProcessor)2 ArrayList (java.util.ArrayList)1