// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.channel.socket.nio;

import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.StandardProtocolFamily;
import io.netty.channel.socket.SocketProtocolFamily;
import java.lang.reflect.InvocationTargetException;
import java.io.IOException;
import java.nio.channels.Channel;
import java.net.ProtocolFamily;
import java.nio.channels.spi.SelectorProvider;
import io.netty.util.internal.PlatformDependent;
import java.lang.reflect.Method;
import io.netty.util.internal.logging.InternalLogger;

final class SelectorProviderUtil
{
    private static final InternalLogger logger;
    
    static Method findOpenMethod(final String methodName) {
        if (PlatformDependent.javaVersion() >= 15) {
            try {
                return SelectorProvider.class.getMethod(methodName, ProtocolFamily.class);
            }
            catch (final Throwable e) {
                SelectorProviderUtil.logger.debug("SelectorProvider.{}(ProtocolFamily) not available, will use default", methodName, e);
            }
        }
        return null;
    }
    
    private static <C extends Channel> C newChannel(final Method method, final SelectorProvider provider, final Object family) throws IOException {
        if (family != null && method != null) {
            try {
                final C channel = (C)method.invoke(provider, family);
                return channel;
            }
            catch (final InvocationTargetException | IllegalAccessException e) {
                throw new IOException(e);
            }
        }
        return null;
    }
    
    static <C extends Channel> C newChannel(final Method method, final SelectorProvider provider, final SocketProtocolFamily family) throws IOException {
        if (family != null) {
            return newChannel(method, provider, family.toJdkFamily());
        }
        return null;
    }
    
    static <C extends Channel> C newDomainSocketChannel(final Method method, final SelectorProvider provider) throws IOException {
        return newChannel(method, provider, StandardProtocolFamily.valueOf("UNIX"));
    }
    
    private SelectorProviderUtil() {
    }
    
    static {
        logger = InternalLoggerFactory.getInstance(SelectorProviderUtil.class);
    }
}
