|
Re: deleting eobject [message #687029 is a reply to message #673397] |
Mon, 23 May 2011 16:48 |
Eclipse User |
|
|
|
Originally posted by: Rich Kulp
On 5/23/2011 10:11 AM, chuck wrote:
> Hi everybody,
> i'm trying to delete some classes in my model with this code:
>
> private static void deleteObj(EObject preR, Resource resource) {
> // TODO Auto-generated method stub
>
>
>
> TreeIterator< EObject> iterator=preR.eAllContents();
>
> while (iterator.hasNext() ) {
> EObject elementToDelete=iterator.next();
> System.out.println("-->deleting "+elementToDelete);
>
> Collection found =
> EcoreUtil.UsageCrossReferencer.find(elementToDelete,preRefactoring.eResource()
> );
> //EcoreUtil.remove(elementToDelete);
> System.out.println(found.size()+":"+found);
> }
>
>
>
>
> //EcoreUtil.remove(preR);
> System.out.println("----->deleting "+preR);
>
> }
>
> i receive an arror concerning the crossreferences and in that code, i
You didn't say what the error was, but I'm betting it is because doing a
ECoreUtil.remove() causes the iterator to become invalid and so the next
call to iterator.hasNext() throws a ConcurrentModificationException.
ECoreUtil.remove() is safe when:
1) Working in a single threaded environment
2) Not within an iterator on anything that may somehow reference that
object or its children, either in single thread or multithread env.
3) In a multi thread then no one else is referencing this object or any
of its children or containers.
> commented delete command to understand which resources have references....
> but is there a method to safely delete an eobject and all its contained
> resources?
> thx a lot
--
Thanks,
Rich Kulp
|
|
|
Re: deleting eobject [message #687030 is a reply to message #673397] |
Mon, 23 May 2011 16:49 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Chuck,
Comments below.
chuck wrote:
> Hi everybody,
> i'm trying to delete some classes in my model with this code:
>
> private static void deleteObj(EObject preR, Resource resource) {
> // TODO Auto-generated method stub
>
>
>
> TreeIterator< EObject> iterator=preR.eAllContents();
>
> while (iterator.hasNext() ) {
> EObject elementToDelete=iterator.next();
> System.out.println("-->deleting "+elementToDelete);
>
> Collection found =
> EcoreUtil.UsageCrossReferencer.find(elementToDelete,preRefactoring.eResource()
> );
> //EcoreUtil.remove(elementToDelete);
Deleting things while you iterate over them is bound to cause problems.
> System.out.println(found.size()+":"+found);
> }
>
>
>
>
> //EcoreUtil.remove(preR);
> System.out.println("----->deleting "+preR);
>
> }
>
> i receive an arror concerning the crossreferences
Should I guess at the error?
> and in that code, i commented delete command to understand which
> resources have references....
> but is there a method to safely delete an eobject and all its
> contained resources?
Doesn't delete in the editor work? Deleting resources is a little
trickier. The editor doesn't do that.
In general you should find all the objects on which to operate and then
do the changes, not do them while iterating.
> thx a lot
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.04707 seconds