Java 如何在不使用ServiceLoader.Provider类的情况下加载服务(用于J1.8兼容性)

Java 如何在不使用ServiceLoader.Provider类的情况下加载服务(用于J1.8兼容性),java,spi,serviceloader,Java,Spi,Serviceloader,我正在尝试将端口返回到J1.8,这是一个为J9()编写的应用程序;它使用ServiceLoader.Provider类及其方法。 原代码为: public static <T extends Service> T loadService(ModuleLayer layer, ClassLoader classLoader, Class<T> type, String classname) { if (classname != nul

我正在尝试将端口返回到J1.8,这是一个为J9()编写的应用程序;它使用ServiceLoader.Provider类及其方法。 原代码为:

public static <T extends Service> T loadService(ModuleLayer layer, ClassLoader classLoader, Class<T> type,
                String classname) {
    if (classname != null && !StringUtils.isClassName(classname)) {
        throw new IllegalArgumentException(classname + " is not a valid Java class name.");
    }

    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }

    ServiceLoader<T> loader;
    List<Provider<T>> providers = new ArrayList<>();

    if (layer != null) {
        loader = ServiceLoader.load(layer, type);
        providers.addAll(loader.stream().collect(Collectors.toList()));
    }

    loader = ServiceLoader.load(type, classLoader);
    providers.addAll(loader.stream().collect(Collectors.toList()));

    if (classname != null) {
        // an explicit class name is used
        // first lets look at providers, to locate in closed modules
        for (Provider<T> p : providers) {
            if (p.type().getName().equals(classname))
                return p.get();
        }

        // nothing found, lets load with reflection
        try {
            Class<?> clazz = classLoader.loadClass(classname);

            if (type.isAssignableFrom(clazz)) {

                // What do you mean?? look 1 line above
                @SuppressWarnings("unchecked")
                T value = (T) clazz.getConstructor().newInstance();
                return value;

            } else {
                // wrong type
                throw new IllegalArgumentException(classname + " is not of type " + type.getCanonicalName());
            }
        } catch (RuntimeException e) {
            throw e; // avoid unnecessary wrapping
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    } else {

        if (providers.isEmpty()) {
            throw new IllegalStateException("No provider found for " + type.getCanonicalName());
        }

        List<T> values = providers.stream().map(Provider::get).collect(Collectors.toList());

        long maxVersion = Long.MIN_VALUE;
        T maxValue = null;
        for (T t : values) {
            long version = t.version();
            if (maxVersion <= version) {
                maxVersion = version;
                maxValue = t;
            }
        }

        return maxValue;
    }
}
publicstatict加载服务(ModuleLayer层、类加载器、类类型、,
字符串(类名){
if(classname!=null&&!StringUtils.isClassName(classname)){
抛出新的IllegalArgumentException(classname+“不是有效的Java类名”);
}
if(classLoader==null){
classLoader=Thread.currentThread().getContextClassLoader();
}
服务加载器;
列表提供程序=新的ArrayList();
如果(层!=null){
loader=ServiceLoader.load(层,类型);
providers.addAll(loader.stream().collect(Collectors.toList());
}
loader=ServiceLoader.load(类型,classLoader);
providers.addAll(loader.stream().collect(Collectors.toList());
if(classname!=null){
//使用显式类名
//首先让我们看看提供者,以定位在封闭模块中
对于(提供商p:提供商){
if(p.type().getName().equals(classname))
返回p.get();
}
//未找到任何内容,请加载反射
试一试{
Class clazz=classLoader.loadClass(classname);
if(类型isAssignableFrom(clazz)){
//你什么意思?看上面一行
@抑制警告(“未选中”)
T值=(T)clazz.getConstructor().newInstance();
返回值;
}否则{
//错误类型
抛出新的IllegalArgumentException(classname+”不是类型“+type.getCanonicalName());
}
}捕获(运行时异常e){
抛出e;//避免不必要的包装
}捕获(例外e){
抛出新的运行时异常(e);
}
}否则{
if(providers.isEmpty()){
抛出新的IllegalStateException(“未找到“+type.getCanonicalName()的提供程序”);
}
列表值=providers.stream().map(Provider::get).collect(Collectors.toList());
long maxVersion=long.MIN_值;
T maxValue=null;
for(T:值){
长版本=t.version();

如果(maxVersion嗯,我不知道这是否是最佳做法,但这解决了:

public static <T extends Service> T loadService(Object layer, ClassLoader classLoader, Class<T> type, String classname) {
    if (classname != null && !StringUtils.isClassName(classname)) {
        throw new IllegalArgumentException(classname + " is not a valid Java class name.");
    }

    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }

    ServiceLoader<T> loader;

    loader = ServiceLoader.load(type, classLoader);
    Iterator<T> iterator = loader.iterator();

    if (classname != null) {
        // an explicit class name is used
        // first lets iterate on providers, to locate in closed modules
        while (iterator.hasNext()) {
              T p = iterator.next();
              if (p.getClass().getName().equals(classname))
                   return p;
        }

        // nothing found, lets load with reflection
        try {
            Class<?> clazz = classLoader.loadClass(classname);

            if (type.isAssignableFrom(clazz)) {

                // What do you mean?? look 1 line above
                @SuppressWarnings("unchecked")
                T value = (T) clazz.getConstructor().newInstance();
                return value;

            } else {
                // wrong type
                throw new IllegalArgumentException(classname + " is not of type " + type.getCanonicalName());
            }
        } catch (RuntimeException e) {
            throw e; // avoid unnecessary wrapping
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    } else {
        if (!iterator.hasNext()) {
            throw new IllegalStateException("No provider found for " + type.getCanonicalName());
        }
        List<T> values = new ArrayList();
        while (iterator.hasNext()) {
            T p = iterator.next();
            values.add(p);
        }

        long maxVersion = Long.MIN_VALUE;
        T maxValue = null;
            for (T t : values) {
            long version = t.version();
            if (maxVersion <= version) {
                maxVersion = version;
                maxValue = t;
            }
        }

        return maxValue;
    }
}
publicstatictloadservice(对象层、类加载器、类类型、字符串类名称){
if(classname!=null&&!StringUtils.isClassName(classname)){
抛出新的IllegalArgumentException(classname+“不是有效的Java类名”);
}
if(classLoader==null){
classLoader=Thread.currentThread().getContextClassLoader();
}
服务加载器;
loader=ServiceLoader.load(类型,classLoader);
迭代器迭代器=loader.Iterator();
if(classname!=null){
//使用显式类名
//首先让我们在提供者上迭代,以定位在封闭模块中
while(iterator.hasNext()){
tp=迭代器.next();
if(p.getClass().getName().equals(classname))
返回p;
}
//未找到任何内容,请加载反射
试一试{
Class clazz=classLoader.loadClass(classname);
if(类型isAssignableFrom(clazz)){
//你什么意思?看上面一行
@抑制警告(“未选中”)
T值=(T)clazz.getConstructor().newInstance();
返回值;
}否则{
//错误类型
抛出新的IllegalArgumentException(classname+”不是类型“+type.getCanonicalName());
}
}捕获(运行时异常e){
抛出e;//避免不必要的包装
}捕获(例外e){
抛出新的运行时异常(e);
}
}否则{
如果(!iterator.hasNext()){
抛出新的IllegalStateException(“未找到“+type.getCanonicalName()的提供程序”);
}
列表值=新的ArrayList();
while(iterator.hasNext()){
tp=迭代器.next();
增加(p);
}
long maxVersion=long.MIN_值;
T maxValue=null;
for(T:值){
长版本=t.version();

如果(maxVersion嗯,我不知道这是否是最佳做法,但这解决了:

public static <T extends Service> T loadService(Object layer, ClassLoader classLoader, Class<T> type, String classname) {
    if (classname != null && !StringUtils.isClassName(classname)) {
        throw new IllegalArgumentException(classname + " is not a valid Java class name.");
    }

    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }

    ServiceLoader<T> loader;

    loader = ServiceLoader.load(type, classLoader);
    Iterator<T> iterator = loader.iterator();

    if (classname != null) {
        // an explicit class name is used
        // first lets iterate on providers, to locate in closed modules
        while (iterator.hasNext()) {
              T p = iterator.next();
              if (p.getClass().getName().equals(classname))
                   return p;
        }

        // nothing found, lets load with reflection
        try {
            Class<?> clazz = classLoader.loadClass(classname);

            if (type.isAssignableFrom(clazz)) {

                // What do you mean?? look 1 line above
                @SuppressWarnings("unchecked")
                T value = (T) clazz.getConstructor().newInstance();
                return value;

            } else {
                // wrong type
                throw new IllegalArgumentException(classname + " is not of type " + type.getCanonicalName());
            }
        } catch (RuntimeException e) {
            throw e; // avoid unnecessary wrapping
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    } else {
        if (!iterator.hasNext()) {
            throw new IllegalStateException("No provider found for " + type.getCanonicalName());
        }
        List<T> values = new ArrayList();
        while (iterator.hasNext()) {
            T p = iterator.next();
            values.add(p);
        }

        long maxVersion = Long.MIN_VALUE;
        T maxValue = null;
            for (T t : values) {
            long version = t.version();
            if (maxVersion <= version) {
                maxVersion = version;
                maxValue = t;
            }
        }

        return maxValue;
    }
}
publicstatictloadservice(对象层、类加载器、类类型、字符串类名称){
if(classname!=null&&!StringUtils.isClassName(classname)){
抛出新的IllegalArgumentException(classname+“不是有效的Java类名”);
}
if(classLoader==null){
classLoader=Thread.currentThread().getContextClassLoader();
}
服务加载器;
loader=ServiceLoader.load(类型,classLoader);
迭代器迭代器=loader.Iterator();
if(classname!=null){
//使用显式类名
//首先让我们在提供者上迭代,以定位在封闭模块中
while(iterator.hasNext()){
tp=迭代器.next();
if(p.getClass().getName().equals(classname))
返回p;
}
//未找到任何内容,请加载反射
试一试{
Class clazz=classLoader.loadClass(classname);
if(类型isAssignableFrom(clazz)){
//你什么意思?看上面一行
@抑制警告(“未选中”)
T值=(T)clazz.getConstructor().newInstance();
返回值;
}否则{
//错误类型
抛出新的IllegalArgumentException(classname+”不是类型“+type.getCanonicalName());
}
}捕获(运行时异常e){
抛出e;//避免不必要的包装