< prev index next >

src/java.base/share/classes/java/util/ServiceLoader.java

Print this page




 813 
 814         @Override
 815         S nextService() {
 816             if (!hasNextService())
 817                 throw new NoSuchElementException();
 818             String cn = nextName;
 819             nextName = null;
 820             Class<?> c = null;
 821             try {
 822                 c = Class.forName(cn, false, loader);
 823             } catch (ClassNotFoundException x) {
 824                 fail(service,
 825                      "Provider " + cn + " not found");
 826             }
 827             if (!service.isAssignableFrom(c)) {
 828                 fail(service,
 829                      "Provider " + cn  + " not a subtype");
 830             }
 831             S p = null;
 832             try {
 833                 p = service.cast(c.newInstance());


 834             } catch (Throwable x) {
 835                 fail(service,
 836                      "Provider " + cn + " could not be instantiated",
 837                      x);
 838             }
 839             providers.add(p);
 840             providerNames.add(cn);
 841             return p;
 842         }
 843     }
 844 
 845     /**
 846      * Lazily loads the available providers of this loader's service.
 847      *
 848      * <p> The iterator returned by this method first yields all of the
 849      * elements of the provider cache, in instantiation order.  It then lazily
 850      * loads and instantiates any remaining providers, adding each one to the
 851      * cache in turn.
 852      *
 853      * <p> To achieve laziness the actual work of locating and instantiating




 813 
 814         @Override
 815         S nextService() {
 816             if (!hasNextService())
 817                 throw new NoSuchElementException();
 818             String cn = nextName;
 819             nextName = null;
 820             Class<?> c = null;
 821             try {
 822                 c = Class.forName(cn, false, loader);
 823             } catch (ClassNotFoundException x) {
 824                 fail(service,
 825                      "Provider " + cn + " not found");
 826             }
 827             if (!service.isAssignableFrom(c)) {
 828                 fail(service,
 829                      "Provider " + cn  + " not a subtype");
 830             }
 831             S p = null;
 832             try {
 833                 @SuppressWarnings("deprecation")
 834                 Object tmp = c.newInstance();
 835                 p = service.cast(tmp);
 836             } catch (Throwable x) {
 837                 fail(service,
 838                      "Provider " + cn + " could not be instantiated",
 839                      x);
 840             }
 841             providers.add(p);
 842             providerNames.add(cn);
 843             return p;
 844         }
 845     }
 846 
 847     /**
 848      * Lazily loads the available providers of this loader's service.
 849      *
 850      * <p> The iterator returned by this method first yields all of the
 851      * elements of the provider cache, in instantiation order.  It then lazily
 852      * loads and instantiates any remaining providers, adding each one to the
 853      * cache in turn.
 854      *
 855      * <p> To achieve laziness the actual work of locating and instantiating


< prev index next >