You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-spec@incubator.apache.org by Daniel Julin <dp...@us.ibm.com> on 2009/04/11 01:29:29 UTC

Re: Kato API javadoc - using Iterable

Just to make matters a little bit more complicated for the use of Iterable,
please note that for some current Kato/DTFJ objects, there is more than one
potential iterator __of the same type__.

For example, JavaClassLoader has both a getCachedClasses() and a
getDefinedClasses() method, both of which return an iterator of JavaClass.
There may be others....


-- Daniel --


Nicholas.Sterling@Sun.COM wrote on 2009-04-09 06:08:08 PM:
> Steve Poole wrote:
> > On Wed, Apr 8, 2009 at 7:37 AM, Nicholas Sterling
<Nicholas.Sterling@sun.
> >
> > Rather than returning an Iterator for the objects on the heap, why not
make
> > JavaHeap extend Iterable?  That way we could write
> >
> >   for ( JavaObject object : heap ) { ... }
> >
> > without having to call getObjects().  If we are using generics, I
believe
> > this would work for both objects and sections, because we would extend
both
> > Iterable<JavaObject> and Iterable<ImageSection>,  i.e.
> >
> >   for ( JavaObject   object  : heap ) { ... }
> >   for ( ImageSection section : heap ) { ... }
> >
> >
> >
> > Two questions
> >
> > 1) Isn't an Iterable just an iterator under the covers?
> >
> An Iterable is a thing which can *return* an Iterator via the iterator()
> method.  In a for-statement like those above, the thing after the colon
> has to be Iterable, because the compiler is going to generate code to
> ask for its Iterator.
> > 2) I thought you could only have one Iterable implemented by a class?
> >
> >
> Unfortunately, you are right.  The following does not work:
>
>     import java.util.ArrayList;
>
>     public class GenericInterfaceTest
>         _implements Iterable<String>, Iterable<Integer>_ {
>
>         ArrayList<String>  strings  = new ArrayList<String >();
>         ArrayList<Integer> integers = new ArrayList<Integer>();
>
>         public GenericInterfaceTest() {
>             strings.add("foo");
>             strings.add("bar");
>             integers.add(1);
>             integers.add(2);
>         }
>
>         _public Iterator<String> iterator()_ {
>             return strings.iterator();
>         }
>
>         _public Iterator<Integer> iterator()_ {
>             return integers.iterator();
>         }
>
>         public static void main( String[] args ) {
>             GenericInterfaceTest git = new GenericInterfaceTest();
>             for ( String  string  : git ) { System.out.println
( string  ); }
>             for ( Integer integer : git ) { System.out.println
( integer ); }
>         }
>     }
>
> Since the method being interited from the interface, iterator(), only
> differs in its return type, it can't work.  Actually, I think
> implementing the same interface twice with different types is disallowed
> in any case because of type erasure
> <http://forums-beta.sun.com/thread.jspa?messageID=1418132>.
>
> Presumably the list of sections is of manageable size, though, so we
> could have a method that returns a List of them, such that we could say
>
>   for ( JavaObject   object  : heap            ) { ... }
>   for ( ImageSection section : heap.sections() ) { ... }