All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.toolchain.common.spi.SpiUtil Maven / Gradle / Ivy

package com.neko233.toolchain.common.spi;

import java.util.Iterator;
import java.util.ServiceLoader;

public class SpiUtil {

    /**
     * Groovy 是否可用
     *
     * @return
     */
    public static boolean isGroovyAvailable() {

        ClassLoader classLoader = SpiUtil.class.getClassLoader();

        try {
            Class bindingClass = classLoader.loadClass("groovy.lang.Binding");
            return bindingClass != null;
        } catch (ClassNotFoundException var2) {
            return false;
        }
    }


    /**
     * SPI get instance
     * need 'META-INF/services/' have API file Name and Impl every line.
     *
     * @param c   class
     * @param  any
     * @return class instance
     */
    public static  T loadFromServiceLoader(Class c) {
        ServiceLoader loader = ServiceLoader.load(c, getServiceLoaderClassLoader());
        Iterator it = loader.iterator();
        return it.hasNext() ? it.next() : null;
    }

    /**
     * @return SPI CL
     */
    private static ClassLoader getServiceLoaderClassLoader() {
        return SpiUtil.class.getClassLoader();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy