Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » deleting eobject(problems)
deleting eobject [message #673397] Mon, 23 May 2011 14:11 Go to next message
chuck  is currently offline chuck Friend
Messages: 160
Registered: July 2010
Senior Member
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 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
Re: deleting eobject [message #687029 is a reply to message #673397] Mon, 23 May 2011 16:48 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous message
Ed Merks is currently offline Ed MerksFriend
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/
Previous Topic:Ecore to Schema -> how to get IDREF?
Next Topic:Question about constraints vs. invariants
Goto Forum:
  


Current Time: Thu Mar 28 22:28:04 GMT 2024

Powered by FUDForum. Page generated in 0.04707 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top