From af71f09add80b79690b3cd9d954a7e656c5ebec1 Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Wed, 14 Mar 2007 17:49:41 +0300 Subject: [PATCH] save and events in predictable order to prevent ipr file changes (IDEADEV-15242) --- .../lang/ant/config/impl/AntConfigurationImpl.java | 61 ++++++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/plugins/ant/src/com/intellij/lang/ant/config/impl/AntConfigurationImpl.java b/plugins/ant/src/com/intellij/lang/ant/config/impl/AntConfigurationImpl.java index 57a515ad89..2b3bf8fe24 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/impl/AntConfigurationImpl.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/impl/AntConfigurationImpl.java @@ -47,10 +47,7 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class AntConfigurationImpl extends AntConfigurationBase implements JDOMExternalizable, ModificationTracker { @@ -330,17 +327,7 @@ public class AntConfigurationImpl extends AntConfigurationBase implements JDOMEx Element element = new Element(BUILD_FILE); element.setAttribute(URL, buildFile.getVirtualFile().getUrl()); ((AntBuildFileBase)buildFile).writeProperties(element); - for (final ExecutionEvent event : myEventToTargetMap.keySet()) { - Pair pair = myEventToTargetMap.get(event); - if (buildFile.equals(pair.first)) { - Element eventElement = new Element(EXECUTE_ON_ELEMENT); - eventElement.setAttribute(EVENT_ELEMENT, event.getTypeId()); - eventElement.setAttribute(TARGET_ELEMENT, (String)pair.second); - event.writeExternal(eventElement); - element.addContent(eventElement); - } - } - + saveEvents(element, buildFile); parentNode.addContent(element); } } @@ -348,13 +335,44 @@ public class AntConfigurationImpl extends AntConfigurationBase implements JDOMEx try { ActionRunner.runInsideReadAction(action); } + catch (WriteExternalException e) { + LOG.error(e); + throw e; + } + catch (RuntimeException e) { + LOG.error(e); + throw e; + } catch (Exception e) { - if (e instanceof WriteExternalException) throw(WriteExternalException)e; - if (e instanceof RuntimeException) throw(RuntimeException)e; LOG.error(e); } } + private void saveEvents(final Element element, final AntBuildFile buildFile) { + List events = null; + for (final ExecutionEvent event : myEventToTargetMap.keySet()) { + final Pair pair = myEventToTargetMap.get(event); + if (!buildFile.equals(pair.first)) { + continue; + } + Element eventElement = new Element(EXECUTE_ON_ELEMENT); + eventElement.setAttribute(EVENT_ELEMENT, event.getTypeId()); + eventElement.setAttribute(TARGET_ELEMENT, pair.second); + event.writeExternal(eventElement); + if (events == null) { + events = new ArrayList(); + } + events.add(eventElement); + } + + if (events != null) { + Collections.sort(events, EventElementComparator.INSTANCE); + for (Element eventElement : events) { + element.addContent(eventElement); + } + } + } + public AntBuildModel getModel(final AntBuildFile buildFile) { AntBuildModelBase model = myModelToBuildFileMap.get(buildFile); if (model == null) { @@ -603,4 +621,13 @@ public class AntConfigurationImpl extends AntConfigurationBase implements JDOMEx } return null; } + + private static class EventElementComparator implements Comparator { + static final Comparator INSTANCE = new EventElementComparator(); + + public int compare(final Element o1, final Element o2) { + final int typesEqual = o1.getAttributeValue(EVENT_ELEMENT).compareTo(o2.getAttributeValue(EVENT_ELEMENT)); + return typesEqual == 0? o1.getAttributeValue(TARGET_ELEMENT).compareTo(o2.getAttributeValue(TARGET_ELEMENT)) : typesEqual; + } + } } -- 2.11.4.GIT