netty5/transport/src/test/java/io/netty/channel/ChannelHandlerAdapterTest.java
Kirils Mensikovs 4b7e5c96b4 Add @Sharable TYPE_USE support for inner class annotations #7756 (#8800)
Motivation:

Make @sharable annotation works with anonymous inner types. Add Java 8 ElementType.TYPE_USE feature that makes easy to use @sharable annotation.

Modification:

transport/src/main/java/io/netty/channel/ChannelHandler.java - Target ElementType.TYPE_USE added.
transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java - isSharable method improved to verify AnnotatedSuperclass for annotation.
transport/src/test/java/io/netty/channel/ChannelHandlerAdapterTest.java - Tests added.

Result:

ChannelInboundHandler handler = new @Sharable ChannelInboundHandlerAdapter() {
      @Override
      public void channelRead(ChannelHandlerContext context, Object message) {
           context.write(message);
      }
};

Note:

The following changes don't support local variable annotation:
ChannelInboundHandler handler1 = new @sharable ChannelInboundHandlerAdapter();
@sharable ChannelInboundHandler handler2 = new ChannelInboundHandlerAdapter();

Fixes #7756
2019-01-31 07:20:29 +01:00

48 lines
1.5 KiB
Java

/*
* Copyright 2019 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import io.netty.channel.ChannelHandler.Sharable;
public class ChannelHandlerAdapterTest {
@Sharable
private static final class SharableChannelHandlerAdapter extends ChannelHandlerAdapter {
}
@Test
public void testSharable() {
ChannelHandlerAdapter handler = new SharableChannelHandlerAdapter();
assertEquals(true, handler.isSharable());
}
@Test
public void testInnerClassSharable() {
ChannelHandlerAdapter handler = new @Sharable ChannelHandlerAdapter() { };
assertEquals(true, handler.isSharable());
}
@Test
public void testWithoutSharable() {
ChannelHandlerAdapter handler = new ChannelHandlerAdapter() { };
assertEquals(false, handler.isSharable());
}
}