3737 */
3838public final class CloseShieldChannel {
3939
40- private static Class <?>[] collectChannelInterfaces (final Class <?> type ) {
41- final Set <Class <?>> out = new LinkedHashSet <>();
42- collectChannelInterfaces (type , out );
43- return out .toArray (new Class <?>[0 ]);
44- }
40+ private static final Class <?>[] EMPTY = {};
4541
46- private static void collectChannelInterfaces (final Class <?> type , final Set <Class <?>> out ) {
42+ private static Set < Class <?>> collectChannelInterfaces (final Class <?> type , final Set <Class <?>> out ) {
4743 // Visit interfaces
4844 for (final Class <?> iface : type .getInterfaces ()) {
4945 if (Channel .class .isAssignableFrom (iface ) && out .add (iface )) {
5046 collectChannelInterfaces (iface , out );
5147 }
5248 }
49+ return out ;
5350 }
5451
5552 /**
@@ -63,18 +60,14 @@ private static void collectChannelInterfaces(final Class<?> type, final Set<Clas
6360 public static <T extends Channel > T wrap (final T channel ) {
6461 Objects .requireNonNull (channel , "channel" );
6562 // Fast path: already our shield
66- if (Proxy .isProxyClass (channel .getClass ())) {
67- if (Proxy .getInvocationHandler (channel ) instanceof CloseShieldChannelHandler ) {
68- return channel ;
69- }
63+ if (Proxy .isProxyClass (channel .getClass ()) && Proxy .getInvocationHandler (channel ) instanceof CloseShieldChannelHandler ) {
64+ return channel ;
7065 }
7166 // Collect only Channel sub-interfaces.
72- Class <?>[] ifaces = collectChannelInterfaces (channel .getClass ());
73- if (ifaces .length == 0 ) {
74- ifaces = new Class <?>[] { Channel .class }; // fallback to root surface
75- }
67+ final Set <Class <?>> set = collectChannelInterfaces (channel .getClass (), new LinkedHashSet <>());
68+ // fallback to root surface
7669 return (T ) Proxy .newProxyInstance (channel .getClass ().getClassLoader (), // use delegate's loader
77- ifaces , new CloseShieldChannelHandler (channel ));
70+ set . isEmpty () ? new Class <?>[] { Channel . class } : set . toArray ( EMPTY ) , new CloseShieldChannelHandler (channel ));
7871 }
7972
8073 private CloseShieldChannel () {
0 commit comments