Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): Model change listener #1417

Merged
merged 40 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4796655
feat: Adds actions to stack.
GerardPaligot Jul 6, 2016
7755be1
feat: Push to stack in all setters.
GerardPaligot Jul 8, 2016
ea58080
fix javadoc constructor
tdurieux Jun 4, 2017
99e9525
create change factory
tdurieux Jun 5, 2017
02a7a28
improve action
tdurieux Jun 23, 2017
89a8ec6
new version of the stack collector
tdurieux Jun 5, 2017
0b14e24
remove stack
tdurieux Jun 23, 2017
2fd79e3
use spoon method to change modifiers
tdurieux Jun 23, 2017
b350422
add getter and setter annotation for new roles
tdurieux Jun 23, 2017
f2b7b06
use spoon api instead of collection api
tdurieux Jun 23, 2017
fe2c16d
return unmodifiableList is list getter
tdurieux Jun 23, 2017
f657f60
fix change factory
tdurieux Jun 23, 2017
c07fc0f
fix import test
tdurieux Jun 23, 2017
8a5f87f
fix test model change listener
tdurieux Jun 23, 2017
900ddcc
remove useless tests
tdurieux Jun 23, 2017
5866f95
fix checkstyle
tdurieux Jun 23, 2017
446bc82
move Actions
tdurieux Jun 23, 2017
14ad3d0
create ListeningChangeFactory
tdurieux Jun 23, 2017
c4a286a
move ChangeFactory
tdurieux Jun 23, 2017
1db2c75
add documentation
tdurieux Jun 23, 2017
a257749
update license
tdurieux Jun 23, 2017
0181053
fix handling policy
tdurieux Jun 23, 2017
507d036
remove type parameter warning
tdurieux Jun 23, 2017
f48f492
refactor: rename
tdurieux Jun 23, 2017
032da4f
move actions to package
tdurieux Jun 23, 2017
069ba33
refactor: rename
tdurieux Jun 23, 2017
c95ba0f
refactor: rename
tdurieux Jun 23, 2017
01e3a65
fix test
tdurieux Jun 23, 2017
9300bd1
add missing change collector
tdurieux Jun 24, 2017
4ed1bce
improve FineModelChangeListener
tdurieux Jun 24, 2017
18b5f1b
fix chackstyle
tdurieux Jun 24, 2017
f3d2325
move the actionbasedchangelistener to ListeningChangeFactory
tdurieux Jun 24, 2017
61253dc
refactor ListeningChangeFactory
tdurieux Jun 26, 2017
6c93854
refactor ModelChangePropagator
tdurieux Jun 26, 2017
37df2eb
change the name to ActionBasedChangeListenerImpl
tdurieux Jun 26, 2017
bd6ddd8
remove addArgument(int i, ...)
tdurieux Jun 26, 2017
7f98f19
change boolean to void with removeXXX method
tdurieux Jun 26, 2017
014ef05
fix compilation issue
tdurieux Jun 26, 2017
86c80bb
change the entry point for the model change listener
tdurieux Jun 27, 2017
d5cf834
extract interface for finemodelchangelistener
tdurieux Jun 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/spoon/compiler/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import spoon.processing.ProcessorProperties;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.experimental.modelobs.FineModelChangeListener;
import spoon.reflect.factory.Factory;

/**
Expand Down Expand Up @@ -330,4 +331,14 @@ void report(Processor<?> processor, Level level,

/** Set the directory where binary .class files are created */
String getBinaryOutputDirectory();

/**
* get the model change listener that is used to follow the change of the AST.
*/
FineModelChangeListener getModelChangeListener();

/**
* set the model change listener
*/
void setModelChangeListener(FineModelChangeListener modelChangeListener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.experimental.modelobs.action.Action;
import spoon.experimental.modelobs.action.AddAction;
import spoon.experimental.modelobs.action.DeleteAction;
import spoon.experimental.modelobs.action.DeleteAllAction;
import spoon.experimental.modelobs.action.UpdateAction;

/**
* notifies all change on the AST
*/
public interface ActionBasedChangeListener {
/**
* when an element is removed
* @param action contains information of the change
*/
void onDelete(DeleteAction action);

/**
* when all element are removed
* @param action contains information of the change
*/
void onDeleteAll(DeleteAllAction action);

/**
* when an element is added
* @param action contains information of the change
*/
void onAdd(AddAction action);

/**
* when an element is modified
* @param action contains information of the change
*/
void onUpdate(UpdateAction action);

/**
* when an element is changed
* @param action contains information of the change
*/
void onAction(Action action);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* 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.experimental.modelobs.action.Action;
import spoon.experimental.modelobs.action.AddAction;
import spoon.experimental.modelobs.action.DeleteAction;
import spoon.experimental.modelobs.action.DeleteAllAction;
import spoon.experimental.modelobs.action.UpdateAction;
import spoon.experimental.modelobs.context.ListContext;
import spoon.experimental.modelobs.context.MapContext;
import spoon.experimental.modelobs.context.ObjectContext;
import spoon.experimental.modelobs.context.SetContext;
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;

/**
* This listener will propagate the change to the listener
*/
public abstract class ActionBasedChangeListenerImpl implements ActionBasedChangeListener, FineModelChangeListener {

private void propagateModelChange(final Action action) {
this.onAction(action);
if (action instanceof DeleteAllAction) {
this.onDeleteAll((DeleteAllAction) action);
} else if (action instanceof DeleteAction) {
this.onDelete((DeleteAction) action);
} else if (action instanceof AddAction) {
this.onAdd((AddAction) action);
} else if (action instanceof UpdateAction) {
this.onUpdate((UpdateAction) action);
}
}

@Override
public void onObjectUpdate(CtElement currentElement, CtRole role, CtElement newValue, CtElement oldValue) {
propagateModelChange(new UpdateAction<>(new ObjectContext(currentElement, role), newValue, oldValue));
}

@Override
public void onObjectUpdate(CtElement currentElement, CtRole role, Object newValue, Object oldValue) {
propagateModelChange(new UpdateAction<>(new ObjectContext(currentElement, role), newValue, oldValue));
}

@Override
public void onObjectDelete(CtElement currentElement, CtRole role, CtElement oldValue) {
propagateModelChange(new DeleteAction<>(new ObjectContext(currentElement, role), oldValue));
}

@Override
public void onListAdd(CtElement currentElement, CtRole role, List field, CtElement newValue) {
propagateModelChange(new AddAction<>(new ListContext(currentElement, role, field), newValue));
}

@Override
public void onListAdd(CtElement currentElement, CtRole role, List field, int index, CtElement newValue) {
propagateModelChange(new AddAction<>(new ListContext(currentElement, role, field, index), newValue));
}

@Override
public void onListDelete(CtElement currentElement, CtRole role, List field, Collection<? extends CtElement> 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));
}

@Override
public void onListDeleteAll(CtElement currentElement, CtRole role, List field, List oldValue) {
propagateModelChange(new DeleteAllAction(new ListContext(currentElement, role, field), oldValue));
}

@Override
public <K, V> void onMapAdd(CtElement currentElement, CtRole role, Map<K, V> field, K key, CtElement newValue) {
propagateModelChange(new AddAction<>(new MapContext<>(currentElement, role, field, key), newValue));
}

@Override
public <K, V> void onMapDeleteAll(CtElement currentElement, CtRole role, Map<K, V> field, Map<K, V> oldValue) {
propagateModelChange(new DeleteAllAction(new MapContext<>(currentElement, role, field), oldValue));
}

@Override
public void onSetAdd(CtElement currentElement, CtRole role, Set field, CtElement newValue) {
propagateModelChange(new AddAction<>(new SetContext(currentElement, role, field), newValue));
}

@Override
public void onSetAdd(CtElement currentElement, CtRole role, Set field, ModifierKind newValue) {
propagateModelChange(new AddAction<>(new SetContext(currentElement, role, field), newValue));
}

@Override
public void onSetDelete(CtElement currentElement, CtRole role, Set field, CtElement oldValue) {
propagateModelChange(new DeleteAction<>(new SetContext(currentElement, role, field), oldValue));
}

@Override
public void onSetDelete(CtElement currentElement, CtRole role, Set field, Collection<ModifierKind> 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));
}

@Override
public void onSetDeleteAll(CtElement currentElement, CtRole role, Set field, Set oldValue) {
propagateModelChange(new DeleteAllAction(new SetContext(currentElement, role, field), oldValue));
}

@Override
public void onDelete(DeleteAction action) {
}

@Override
public void onDeleteAll(DeleteAllAction action) {
}

@Override
public void onAdd(AddAction action) {
}

@Override
public void onUpdate(UpdateAction action) {
}

@Override
public void onAction(Action action) {
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends CtElement> 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 <K, V> void onMapAdd(CtElement currentElement, CtRole role,
Map<K, V> field, K key, CtElement newValue) {
}

@Override
public <K, V> void onMapDeleteAll(CtElement currentElement, CtRole role,
Map<K, V> field, Map<K, V> 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<ModifierKind> 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) {
}
}
Loading