Search in sources :

Example 1 with RefactoringDescriptor

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

the class RefactoringTest method performRefactoring.

protected final RefactoringStatus performRefactoring(Refactoring ref, boolean providesUndo) throws Exception {
    performDummySearch();
    IUndoManager undoManager = getUndoManager();
    if (DESCRIPTOR_TEST) {
        final CreateChangeOperation create = new CreateChangeOperation(new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL);
        create.run(new NullProgressMonitor());
        RefactoringStatus checkingStatus = create.getConditionCheckingStatus();
        if (!checkingStatus.isOK())
            return checkingStatus;
        Change change = create.getChange();
        ChangeDescriptor descriptor = change.getDescriptor();
        if (descriptor instanceof RefactoringChangeDescriptor) {
            RefactoringChangeDescriptor rcd = (RefactoringChangeDescriptor) descriptor;
            RefactoringDescriptor refactoringDescriptor = rcd.getRefactoringDescriptor();
            if (refactoringDescriptor instanceof JavaRefactoringDescriptor) {
                JavaRefactoringDescriptor jrd = (JavaRefactoringDescriptor) refactoringDescriptor;
                RefactoringStatus validation = jrd.validateDescriptor();
                if (!validation.isOK())
                    return validation;
                RefactoringStatus refactoringStatus = new RefactoringStatus();
                Class expected = jrd.getClass();
                RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(jrd.getID());
                jrd = (JavaRefactoringDescriptor) contribution.createDescriptor(jrd.getID(), jrd.getProject(), jrd.getDescription(), jrd.getComment(), contribution.retrieveArgumentMap(jrd), jrd.getFlags());
                assertEquals(expected, jrd.getClass());
                ref = jrd.createRefactoring(refactoringStatus);
                if (!refactoringStatus.isOK())
                    return refactoringStatus;
                TestRenameParticipantSingle.reset();
                TestCreateParticipantSingle.reset();
                TestMoveParticipantSingle.reset();
                TestDeleteParticipantSingle.reset();
            }
        }
    }
    final CreateChangeOperation create = new CreateChangeOperation(new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL);
    final PerformChangeOperation perform = new PerformChangeOperation(create);
    perform.setUndoManager(undoManager, ref.getName());
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    if (fIsPreDeltaTest) {
        IResourceChangeListener listener = new IResourceChangeListener() {

            public void resourceChanged(IResourceChangeEvent event) {
                if (create.getConditionCheckingStatus().isOK() && perform.changeExecuted()) {
                    TestModelProvider.assertTrue(event.getDelta());
                }
            }
        };
        try {
            TestModelProvider.clearDelta();
            workspace.checkpoint(false);
            workspace.addResourceChangeListener(listener);
            executePerformOperation(perform, workspace);
        } finally {
            workspace.removeResourceChangeListener(listener);
        }
    } else {
        executePerformOperation(perform, workspace);
    }
    RefactoringStatus status = create.getConditionCheckingStatus();
    if (!status.isOK())
        return status;
    assertTrue("Change wasn't executed", perform.changeExecuted());
    Change undo = perform.getUndoChange();
    if (providesUndo) {
        assertNotNull("Undo doesn't exist", undo);
        assertTrue("Undo manager is empty", undoManager.anythingToUndo());
    } else {
        assertNull("Undo manager contains undo but shouldn't", undo);
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IUndoManager(org.eclipse.ltk.core.refactoring.IUndoManager) RefactoringContribution(org.eclipse.ltk.core.refactoring.RefactoringContribution) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Change(org.eclipse.ltk.core.refactoring.Change) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) ChangeDescriptor(org.eclipse.ltk.core.refactoring.ChangeDescriptor) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor) JavaRefactoringDescriptor(org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor) RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IWorkspace(org.eclipse.core.resources.IWorkspace) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) JavaRefactoringDescriptor(org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor)

Example 2 with RefactoringDescriptor

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

the class RefactoringSessionReader method readSession.

/**
	 * Reads a refactoring history descriptor from the specified input object.
	 *
	 * @param source
	 *            the input source
	 * @return a corresponding refactoring history descriptor, or
	 *         <code>null</code>
	 * @throws CoreException
	 *             if an error occurs while reading form the input source
	 */
public RefactoringSessionDescriptor readSession(final InputSource source) throws CoreException {
    fSessionFound = false;
    try {
        //$NON-NLS-1$
        source.setSystemId("/");
        createParser(SAXParserFactory.newInstance()).parse(source, this);
        if (!fSessionFound)
            throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, RefactoringCoreMessages.RefactoringSessionReader_no_session, null));
        if (fRefactoringDescriptors != null) {
            if (//$NON-NLS-1$
            fVersion == null || "".equals(fVersion))
                throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.MISSING_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_missing_version_information, null));
            if (!IRefactoringSerializationConstants.CURRENT_VERSION.equals(fVersion))
                throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.UNSUPPORTED_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_unsupported_version_information, null));
            return new RefactoringSessionDescriptor((RefactoringDescriptor[]) fRefactoringDescriptors.toArray(new RefactoringDescriptor[fRefactoringDescriptors.size()]), fVersion, fComment);
        }
    } catch (IOException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } catch (ParserConfigurationException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } catch (SAXParseException exception) {
        String message = Messages.format(RefactoringCoreMessages.RefactoringSessionReader_invalid_contents_at, new Object[] { Integer.toString(exception.getLineNumber()), Integer.toString(exception.getColumnNumber()) });
        throwCoreException(exception, message);
    } catch (SAXException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } finally {
        fRefactoringDescriptors = null;
        fVersion = null;
        fComment = null;
        fLocator = null;
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DefaultRefactoringDescriptor(org.eclipse.ltk.internal.core.refactoring.history.DefaultRefactoringDescriptor) RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) CoreException(org.eclipse.core.runtime.CoreException) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RefactoringSessionDescriptor(org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor) SAXException(org.xml.sax.SAXException)

Example 3 with RefactoringDescriptor

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

the class RefactoringSessionReader method startElement.

/**
	 * {@inheritDoc}
	 */
public void startElement(final String uri, final String localName, final String qualifiedName, final Attributes attributes) throws SAXException {
    if (IRefactoringSerializationConstants.ELEMENT_REFACTORING.equals(qualifiedName)) {
        final int length = attributes.getLength();
        final Map map = new HashMap(length);
        //$NON-NLS-1$
        String id = "";
        //$NON-NLS-1$
        String stamp = "";
        //$NON-NLS-1$
        String description = "";
        String comment = null;
        //$NON-NLS-1$
        String flags = "0";
        String project = null;
        for (int index = 0; index < length; index++) {
            final String name = attributes.getQName(index);
            final String value = attributes.getValue(index);
            if (IRefactoringSerializationConstants.ATTRIBUTE_ID.equals(name)) {
                id = value;
            } else if (IRefactoringSerializationConstants.ATTRIBUTE_STAMP.equals(name)) {
                stamp = value;
            } else if (IRefactoringSerializationConstants.ATTRIBUTE_DESCRIPTION.equals(name)) {
                description = value;
            } else if (IRefactoringSerializationConstants.ATTRIBUTE_FLAGS.equals(name)) {
                flags = value;
            } else if (IRefactoringSerializationConstants.ATTRIBUTE_COMMENT.equals(name)) {
                if (//$NON-NLS-1$
                !"".equals(value))
                    comment = value;
            } else if (IRefactoringSerializationConstants.ATTRIBUTE_PROJECT.equals(name)) {
                project = value;
            } else if (!"".equals(name)) {
                //$NON-NLS-1$
                map.put(name, value);
            }
        }
        int flag = 0;
        try {
            flag = Integer.parseInt(flags);
        } catch (NumberFormatException exception) {
        // Do nothing
        }
        RefactoringDescriptor descriptor = null;
        if (fCreateDefaultDescriptors) {
            descriptor = new DefaultRefactoringDescriptor(id, project, description, comment, map, flag);
        } else {
            if (fProject != null && project == null) {
                // override project from file if fProject != null
                project = fProject;
            }
            try {
                descriptor = RefactoringContributionManager.getInstance().createDescriptor(id, project, description, comment, map, flag);
            } catch (RuntimeException e) {
                throw new SAXParseException(RefactoringCoreMessages.RefactoringSessionReader_invalid_values_in_xml, fLocator, e) {

                    private static final long serialVersionUID = 1L;

                    public Throwable getCause() {
                        // support proper 1.4-style exception chaining
                        return getException();
                    }
                };
            }
        }
        try {
            descriptor.setTimeStamp(Long.valueOf(stamp).longValue());
        } catch (NumberFormatException exception) {
        // Do nothing
        }
        if (fRefactoringDescriptors == null)
            fRefactoringDescriptors = new ArrayList();
        fRefactoringDescriptors.add(descriptor);
    } else if (IRefactoringSerializationConstants.ELEMENT_SESSION.equals(qualifiedName)) {
        fSessionFound = true;
        final String version = attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_VERSION);
        if (//$NON-NLS-1$
        version != null && !"".equals(version))
            fVersion = version;
        fComment = attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT);
    }
}
Also used : DefaultRefactoringDescriptor(org.eclipse.ltk.internal.core.refactoring.history.DefaultRefactoringDescriptor) RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) HashMap(java.util.HashMap) SAXParseException(org.xml.sax.SAXParseException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) DefaultRefactoringDescriptor(org.eclipse.ltk.internal.core.refactoring.history.DefaultRefactoringDescriptor)

Example 4 with RefactoringDescriptor

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

the class RefactoringHistoryService method readRefactoringHistory.

/**
	 * {@inheritDoc}
	 */
public RefactoringHistory readRefactoringHistory(final InputStream stream, final int flags) throws CoreException {
    Assert.isNotNull(stream);
    Assert.isTrue(flags >= RefactoringDescriptor.NONE);
    final List list = new ArrayList();
    final RefactoringSessionDescriptor descriptor = new RefactoringSessionReader(false, null).readSession(new InputSource(stream));
    if (descriptor != null) {
        final RefactoringDescriptor[] descriptors = descriptor.getRefactorings();
        if (flags > RefactoringDescriptor.NONE) {
            for (int index = 0; index < descriptors.length; index++) {
                final int current = descriptors[index].getFlags();
                if ((current | flags) == current)
                    list.add(descriptors[index]);
            }
        } else
            list.addAll(Arrays.asList(descriptors));
    }
    final RefactoringDescriptorProxy[] proxies = new RefactoringDescriptorProxy[list.size()];
    for (int index = 0; index < list.size(); index++) proxies[index] = new RefactoringDescriptorProxyAdapter((RefactoringDescriptor) list.get(index));
    return new RefactoringHistoryImplementation(proxies);
}
Also used : RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) InputSource(org.xml.sax.InputSource) RefactoringSessionReader(org.eclipse.ltk.internal.core.refactoring.RefactoringSessionReader) RefactoringDescriptorProxy(org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy) ArrayList(java.util.ArrayList) List(java.util.List) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) RefactoringSessionDescriptor(org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor)

Example 5 with RefactoringDescriptor

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

the class RenameTypeTest method helper3_fail.

private void helper3_fail(String oldName, String newName, boolean updateSimilar, boolean updateTextual, boolean updateRef) throws JavaModelException, CoreException, IOException, Exception {
    RefactoringDescriptor descriptor = initWithAllOptions(oldName, oldName, newName, updateRef, updateTextual, updateSimilar, null, RenamingNameSuggestor.STRATEGY_SUFFIX);
    Assert.assertNotNull("was supposed to fail", performRefactoring(descriptor));
}
Also used : RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor)

Aggregations

RefactoringDescriptor (org.eclipse.ltk.core.refactoring.RefactoringDescriptor)13 RefactoringSessionDescriptor (org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor)4 ArrayList (java.util.ArrayList)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CoreException (org.eclipse.core.runtime.CoreException)2 ListenerList (org.eclipse.core.runtime.ListenerList)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)2 RefactoringDescriptorProxy (org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)2 RefactoringSessionReader (org.eclipse.ltk.internal.core.refactoring.RefactoringSessionReader)2 DefaultRefactoringDescriptor (org.eclipse.ltk.internal.core.refactoring.history.DefaultRefactoringDescriptor)2 InputSource (org.xml.sax.InputSource)2 SAXParseException (org.xml.sax.SAXParseException)2