public static final <T extends EObject>List<T> collectNodesWithinSameThisEnvironment(EObject root,Class<T> cls){
  final List<T> result=new ArrayList<>();
  final TreeIterator<EObject> iter=root.eAllContents();
  while (iter.hasNext()) {
    final EObject obj=iter.next();
    if (cls.isAssignableFrom(obj.getClass())) {
      @SuppressWarnings("unchecked") final T objCasted=(T)obj;
      result.add(objCasted);
    }
    if (obj instanceof ThisArgProvider) {
      iter.prune();
    }
  }
  return result;
}

public static final < T extends EObject > List < T > collectNodesWithinSameThisEnvironment ( EObject root , Class < T > cls ) { final List < T > result = new ArrayList < > ( ) ; final TreeIterator < EObject > iter = root . eAllContents ( ) ; while ( iter . hasNext ( ) ) { final EObject obj = iter . next ( ) ; if ( cls . isAssignableFrom ( obj . getClass ( ) ) ) { @ SuppressWarnings ( "unchecked" ) final T objCasted = ( T ) obj ; result . add ( objCasted ) ; } if ( obj instanceof ThisArgProvider ) { iter . prune ( ) ; } } return result ; }
root usually a function or other thisproviding environment .
