From d5cf83427571efcded3338ae1fb1ad96be870b1b Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 27 Jun 2017 15:22:41 +0200 Subject: [PATCH] extract interface for finemodelchangelistener --- .../ActionBasedChangeListenerImpl.java | 17 ++- .../modelobs/EmptyModelChangeListener.java | 121 ++++++++++++++++++ .../modelobs/FineModelChangeListener.java | 63 +++------ .../spoon/support/StandardEnvironment.java | 3 +- 4 files changed, 156 insertions(+), 48 deletions(-) create mode 100644 src/main/java/spoon/experimental/modelobs/EmptyModelChangeListener.java diff --git a/src/main/java/spoon/experimental/modelobs/ActionBasedChangeListenerImpl.java b/src/main/java/spoon/experimental/modelobs/ActionBasedChangeListenerImpl.java index 70ccbc8bcf5..1aab485138d 100644 --- a/src/main/java/spoon/experimental/modelobs/ActionBasedChangeListenerImpl.java +++ b/src/main/java/spoon/experimental/modelobs/ActionBasedChangeListenerImpl.java @@ -29,6 +29,7 @@ import spoon.reflect.declaration.ModifierKind; import spoon.reflect.path.CtRole; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -36,7 +37,7 @@ /** * This listener will propagate the change to the listener */ -public abstract class ActionBasedChangeListenerImpl extends FineModelChangeListener implements ActionBasedChangeListener { +public abstract class ActionBasedChangeListenerImpl implements ActionBasedChangeListener, FineModelChangeListener { private void propagateModelChange(final Action action) { this.onAction(action); @@ -76,6 +77,13 @@ public void onListAdd(CtElement currentElement, CtRole role, List field, int ind propagateModelChange(new AddAction<>(new ListContext(currentElement, role, field, index), newValue)); } + @Override + public void onListDelete(CtElement currentElement, CtRole role, List field, Collection oldValue) { + for (CtElement ctElement : oldValue) { + onListDelete(currentElement, role, field, field.indexOf(ctElement), ctElement); + } + } + @Override public void onListDelete(CtElement currentElement, CtRole role, List field, int index, CtElement oldValue) { propagateModelChange(new DeleteAction<>(new ListContext(currentElement, role, field, index), oldValue)); @@ -111,6 +119,13 @@ public void onSetDelete(CtElement currentElement, CtRole role, Set field, CtElem propagateModelChange(new DeleteAction<>(new SetContext(currentElement, role, field), oldValue)); } + @Override + public void onSetDelete(CtElement currentElement, CtRole role, Set field, Collection oldValue) { + for (ModifierKind modifierKind : oldValue) { + onSetDelete(currentElement, role, field, modifierKind); + } + } + @Override public void onSetDelete(CtElement currentElement, CtRole role, Set field, ModifierKind oldValue) { propagateModelChange(new DeleteAction<>(new SetContext(currentElement, role, field), oldValue)); diff --git a/src/main/java/spoon/experimental/modelobs/EmptyModelChangeListener.java b/src/main/java/spoon/experimental/modelobs/EmptyModelChangeListener.java new file mode 100644 index 00000000000..ca6dbc942bf --- /dev/null +++ b/src/main/java/spoon/experimental/modelobs/EmptyModelChangeListener.java @@ -0,0 +1,121 @@ +/** + * Copyright (C) 2006-2017 INRIA and contributors + * Spoon - http://spoon.gforge.inria.fr/ + * + * This software is governed by the CeCILL-C License under French law and + * abiding by the rules of distribution of free software. You can use, modify + * and/or redistribute the software under the terms of the CeCILL-C license as + * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-C license and that you accept its terms. + */ +package spoon.experimental.modelobs; + +import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.path.CtRole; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * is the listener that creates the action on the model. This default listener does nothing. + */ +public class EmptyModelChangeListener implements FineModelChangeListener { + + @Override + public void onObjectUpdate(CtElement currentElement, CtRole role, + CtElement newValue, CtElement oldValue) { + } + + @Override + public void onObjectUpdate(CtElement currentElement, CtRole role, + Object newValue, Object oldValue) { + } + + @Override + public void onObjectDelete(CtElement currentElement, CtRole role, + CtElement oldValue) { + } + + @Override + public void onListAdd(CtElement currentElement, CtRole role, List field, + CtElement newValue) { + } + + @Override + public void onListAdd(CtElement currentElement, CtRole role, List field, + int index, CtElement newValue) { + } + + + @Override + public void onListDelete(CtElement currentElement, CtRole role, List field, + Collection oldValue) { + for (CtElement ctElement : oldValue) { + onListDelete(currentElement, role, field, field.indexOf(ctElement), ctElement); + } + } + + @Override + public void onListDelete(CtElement currentElement, CtRole role, List field, + int index, CtElement oldValue) { + } + + + @Override + public void onListDeleteAll(CtElement currentElement, CtRole role, + List field, List oldValue) { + } + + + @Override + public void onMapAdd(CtElement currentElement, CtRole role, + Map field, K key, CtElement newValue) { + } + + @Override + public void onMapDeleteAll(CtElement currentElement, CtRole role, + Map field, Map oldValue) { + } + + @Override + public void onSetAdd(CtElement currentElement, CtRole role, Set field, + CtElement newValue) { + } + + @Override + public void onSetAdd(CtElement currentElement, CtRole role, Set field, + ModifierKind newValue) { + } + + + @Override + public void onSetDelete(CtElement currentElement, CtRole role, Set field, + CtElement oldValue) { + } + + @Override + public void onSetDelete(CtElement currentElement, CtRole role, Set field, Collection oldValue) { + for (ModifierKind modifierKind : oldValue) { + onSetDelete(currentElement, role, field, modifierKind); + } + } + + @Override + public void onSetDelete(CtElement currentElement, CtRole role, Set field, + ModifierKind oldValue) { + } + + @Override + public void onSetDeleteAll(CtElement currentElement, CtRole role, Set field, + Set oldValue) { + } +} diff --git a/src/main/java/spoon/experimental/modelobs/FineModelChangeListener.java b/src/main/java/spoon/experimental/modelobs/FineModelChangeListener.java index 248f0f6b9c6..3504477261b 100644 --- a/src/main/java/spoon/experimental/modelobs/FineModelChangeListener.java +++ b/src/main/java/spoon/experimental/modelobs/FineModelChangeListener.java @@ -25,66 +25,37 @@ import java.util.Map; import java.util.Set; -/** - * is the listener that creates the action on the model. This default listener does nothing. - */ -public class FineModelChangeListener { - - public void onObjectUpdate(CtElement currentElement, CtRole role, CtElement newValue, CtElement oldValue) { - } - - public void onObjectUpdate(CtElement currentElement, CtRole role, Object newValue, Object oldValue) { - } - - public void onObjectDelete(CtElement currentElement, CtRole role, CtElement oldValue) { - } - - public void onListAdd(CtElement currentElement, CtRole role, List field, CtElement newValue) { - } - public void onListAdd(CtElement currentElement, CtRole role, List field, int index, CtElement newValue) { - } +public interface FineModelChangeListener { + void onObjectUpdate(CtElement currentElement, CtRole role, CtElement newValue, CtElement oldValue); + void onObjectUpdate(CtElement currentElement, CtRole role, Object newValue, Object oldValue); - public void onListDelete(CtElement currentElement, CtRole role, List field, Collection oldValue) { - for (CtElement ctElement : oldValue) { - onListDelete(currentElement, role, field, field.indexOf(ctElement), ctElement); - } - } + void onObjectDelete(CtElement currentElement, CtRole role, CtElement oldValue); - public void onListDelete(CtElement currentElement, CtRole role, List field, int index, CtElement oldValue) { - } + void onListAdd(CtElement currentElement, CtRole role, List field, CtElement newValue); + void onListAdd(CtElement currentElement, CtRole role, List field, int index, CtElement newValue); - public void onListDeleteAll(CtElement currentElement, CtRole role, List field, List oldValue) { - } + void onListDelete(CtElement currentElement, CtRole role, List field, Collection oldValue); + void onListDelete(CtElement currentElement, CtRole role, List field, int index, CtElement oldValue); - public void onMapAdd(CtElement currentElement, CtRole role, Map field, K key, CtElement newValue) { - } + void onListDeleteAll(CtElement currentElement, CtRole role, List field, List oldValue); - public void onMapDeleteAll(CtElement currentElement, CtRole role, Map field, Map oldValue) { - } + void onMapAdd(CtElement currentElement, CtRole role, Map field, K key, CtElement newValue); - public void onSetAdd(CtElement currentElement, CtRole role, Set field, CtElement newValue) { - } + void onMapDeleteAll(CtElement currentElement, CtRole role, Map field, Map oldValue); - public void onSetAdd(CtElement currentElement, CtRole role, Set field, ModifierKind newValue) { - } + void onSetAdd(CtElement currentElement, CtRole role, Set field, CtElement newValue); + void onSetAdd(CtElement currentElement, CtRole role, Set field, ModifierKind newValue); - public void onSetDelete(CtElement currentElement, CtRole role, Set field, CtElement oldValue) { - } + void onSetDelete(CtElement currentElement, CtRole role, Set field, CtElement oldValue); - public void onSetDelete(CtElement currentElement, CtRole role, Set field, Collection oldValue) { - for (ModifierKind modifierKind : oldValue) { - onSetDelete(currentElement, role, field, modifierKind); - } - } + void onSetDelete(CtElement currentElement, CtRole role, Set field, Collection oldValue); - public void onSetDelete(CtElement currentElement, CtRole role, Set field, ModifierKind oldValue) { - } + void onSetDelete(CtElement currentElement, CtRole role, Set field, ModifierKind oldValue); - public void onSetDeleteAll(CtElement currentElement, CtRole role, Set field, Set oldValue) { - } + void onSetDeleteAll(CtElement currentElement, CtRole role, Set field, Set oldValue); } diff --git a/src/main/java/spoon/support/StandardEnvironment.java b/src/main/java/spoon/support/StandardEnvironment.java index d31aa8c559e..35e9833e6ff 100644 --- a/src/main/java/spoon/support/StandardEnvironment.java +++ b/src/main/java/spoon/support/StandardEnvironment.java @@ -24,6 +24,7 @@ import spoon.compiler.InvalidClassPathException; import spoon.compiler.SpoonFile; import spoon.compiler.SpoonFolder; +import spoon.experimental.modelobs.EmptyModelChangeListener; import spoon.processing.FileGenerator; import spoon.processing.ProblemFixer; import spoon.processing.ProcessingManager; @@ -86,7 +87,7 @@ public class StandardEnvironment implements Serializable, Environment { private boolean skipSelfChecks; - private FineModelChangeListener modelChangeListener = new FineModelChangeListener(); + private FineModelChangeListener modelChangeListener = new EmptyModelChangeListener(); /** * Creates a new environment with a null default file