ServiceLoader.Iterator Method

Definition

Lazily loads the available providers of this loader's service.

[Android.Runtime.Register("iterator", "()Ljava/util/Iterator;", "")]
public Java.Util.IIterator? Iterator ();
[<Android.Runtime.Register("iterator", "()Ljava/util/Iterator;", "")>]
abstract member Iterator : unit -> Java.Util.IIterator
override this.Iterator : unit -> Java.Util.IIterator

Returns

An iterator that lazily loads providers for this loader's service

Implements

Attributes

Remarks

Lazily loads the available providers of this loader's service.

The iterator returned by this method first yields all of the elements of the provider cache, in instantiation order. It then lazily loads and instantiates any remaining providers, adding each one to the cache in turn.

To achieve laziness the actual work of parsing the available provider-configuration files and instantiating providers must be done by the iterator itself. Its java.util.Iterator#hasNext hasNext and java.util.Iterator#next next methods can therefore throw a ServiceConfigurationError if a provider-configuration file violates the specified format, or if it names a provider class that cannot be found and instantiated, or if the result of instantiating the class is not assignable to the service type, or if any other kind of exception or error is thrown as the next provider is located and instantiated. To write robust code it is only necessary to catch ServiceConfigurationError when using a service iterator.

If such an error is thrown then subsequent invocations of the iterator will make a best effort to locate and instantiate the next available provider, but in general such recovery cannot be guaranteed.

<blockquote style="font-size: smaller; line-height: 1.2"><span style="padding-right: 1em; font-weight: bold">Design Note</span> Throwing an error in these cases may seem extreme. The rationale for this behavior is that a malformed provider-configuration file, like a malformed class file, indicates a serious problem with the way the Java virtual machine is configured or is being used. As such it is preferable to throw an error rather than try to recover or, even worse, fail silently.</blockquote>

The iterator returned by this method does not support removal. Invoking its java.util.Iterator#remove() remove method will cause an UnsupportedOperationException to be thrown.

Java documentation for java.util.ServiceLoader.iterator().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to