diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioChannelOption.java b/transport/src/main/java/io/netty/channel/socket/nio/NioChannelOption.java index 7d94b99706..a56e07b382 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioChannelOption.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioChannelOption.java @@ -19,33 +19,39 @@ import io.netty.channel.ChannelException; import io.netty.channel.ChannelOption; import java.io.IOException; -import java.net.SocketOption; -import java.nio.channels.NetworkChannel; +import java.nio.channels.Channel; import java.util.Set; /** - * Provides {@link ChannelOption} over a given {@link SocketOption} which is then passed through the underlying - * {@link NetworkChannel}. + * Provides {@link ChannelOption} over a given {@link java.net.SocketOption} which is then passed through the underlying + * {@link java.nio.channels.NetworkChannel}. */ public final class NioChannelOption extends ChannelOption { - private final SocketOption option; + private final java.net.SocketOption option; @SuppressWarnings("deprecation") - private NioChannelOption(SocketOption option) { + private NioChannelOption(java.net.SocketOption option) { super(option.name()); this.option = option; } /** - * Returns a {@link ChannelOption} for the given {@link SocketOption}. + * Returns a {@link ChannelOption} for the given {@link java.net.SocketOption}. */ - public static ChannelOption of(SocketOption option) { + public static ChannelOption of(java.net.SocketOption option) { return new NioChannelOption(option); } + // It's important to not use java.nio.channels.NetworkChannel as otherwise the classes that sometimes call this + // method may not be used on Java 6, as method linking can happen eagerly even if this method was not actually + // called at runtime. + // + // See https://github.com/netty/netty/issues/8166 + // Internal helper methods to remove code duplication between Nio*Channel implementations. - static boolean setOption(NetworkChannel channel, NioChannelOption option, T value) { + static boolean setOption(Channel jdkChannel, NioChannelOption option, T value) { + java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel; if (!channel.supportedOptions().contains(option.option)) { return false; } @@ -57,7 +63,9 @@ public final class NioChannelOption extends ChannelOption { } } - static T getOption(NetworkChannel channel, NioChannelOption option) { + static T getOption(Channel jdkChannel, NioChannelOption option) { + java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel; + if (!channel.supportedOptions().contains(option.option)) { return null; } @@ -69,12 +77,13 @@ public final class NioChannelOption extends ChannelOption { } @SuppressWarnings("unchecked") - static ChannelOption[] getOptions(NetworkChannel channel) { - Set> supportedOpts = channel.supportedOptions(); + static ChannelOption[] getOptions(Channel jdkChannel) { + java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel; + Set> supportedOpts = channel.supportedOptions(); ChannelOption[] extraOpts = new ChannelOption[supportedOpts.size()]; int i = 0; - for (SocketOption opt : supportedOpts) { + for (java.net.SocketOption opt : supportedOpts) { extraOpts[i++] = new NioChannelOption(opt); } return extraOpts;