diff --git a/src/main/java/io/github/yezhihao/netmc/TCPServer.java b/src/main/java/io/github/yezhihao/netmc/TCPServer.java index cb48ad6..3484334 100644 --- a/src/main/java/io/github/yezhihao/netmc/TCPServer.java +++ b/src/main/java/io/github/yezhihao/netmc/TCPServer.java @@ -33,8 +33,8 @@ public class TCPServer { private EventLoopGroup bossGroup = null; private EventLoopGroup workerGroup = null; - private String name; - private NettyConfig config; + private final String name; + private final NettyConfig config; public TCPServer(String name, NettyConfig config) { this.name = name; @@ -53,8 +53,8 @@ public class TCPServer { .childOption(NioChannelOption.TCP_NODELAY, true) .childHandler(new ChannelInitializer() { - private MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder); - private MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder); + private final MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder); + private final MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder); @Override public void initChannel(NioSocketChannel channel) { @@ -71,7 +71,7 @@ public class TCPServer { log.warn("==={}启动成功, port={}===", name, config.port); channelFuture.channel().closeFuture().sync(); } catch (Exception e) { - log.warn("==={}出现异常, port={}===", e); + log.warn("==={}出现异常, port={}===", e, config.port); } finally { stop(); } diff --git a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java index 1825407..707f1b3 100644 --- a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java +++ b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java @@ -27,13 +27,13 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(TCPServerHandler.class.getSimpleName()); - private HandlerMapping handlerMapping; + private final HandlerMapping handlerMapping; - private HandlerInterceptor interceptor; + private final HandlerInterceptor interceptor; - private SessionManager sessionManager; + private final SessionManager sessionManager; - private SessionListener sessionListener; + private final SessionListener sessionListener; public TCPServerHandler(HandlerMapping handlerMapping, HandlerInterceptor interceptor, diff --git a/src/main/java/io/github/yezhihao/netmc/codec/DelimiterBasedFrameDecoder.java b/src/main/java/io/github/yezhihao/netmc/codec/DelimiterBasedFrameDecoder.java index 36baeae..1aa7b59 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/DelimiterBasedFrameDecoder.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/DelimiterBasedFrameDecoder.java @@ -40,7 +40,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { } } - protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { + protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) { // Try all delimiters and choose the delimiter which yields the shortest frame. int minFrameLength = Integer.MAX_VALUE; Delimiter minDelim = null; diff --git a/src/main/java/io/github/yezhihao/netmc/codec/LengthFieldAndDelimiterFrameDecoder.java b/src/main/java/io/github/yezhihao/netmc/codec/LengthFieldAndDelimiterFrameDecoder.java index f43c300..df5320d 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/LengthFieldAndDelimiterFrameDecoder.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/LengthFieldAndDelimiterFrameDecoder.java @@ -100,7 +100,7 @@ public class LengthFieldAndDelimiterFrameDecoder extends DelimiterBasedFrameDeco throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less than initialBytesToStrip: " + initialBytesToStrip); } - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) { if (in.readableBytes() < this.lengthFieldEndOffset) { return null; } else { diff --git a/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoderWrapper.java b/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoderWrapper.java index a43539f..967be35 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoderWrapper.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoderWrapper.java @@ -16,7 +16,7 @@ import io.netty.handler.codec.DecoderException; @ChannelHandler.Sharable public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter { - private MessageDecoder decoder; + private final MessageDecoder decoder; public MessageDecoderWrapper(MessageDecoder decoder) { this.decoder = decoder; diff --git a/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoderWrapper.java b/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoderWrapper.java index 28614f9..78b9813 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoderWrapper.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoderWrapper.java @@ -17,7 +17,7 @@ import io.netty.handler.codec.EncoderException; @ChannelHandler.Sharable public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter { - private MessageEncoder encoder; + private final MessageEncoder encoder; public MessageEncoderWrapper(MessageEncoder encoder) { this.encoder = encoder; diff --git a/src/main/java/io/github/yezhihao/netmc/core/AbstractHandlerMapping.java b/src/main/java/io/github/yezhihao/netmc/core/AbstractHandlerMapping.java index 260338e..42aef77 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/AbstractHandlerMapping.java +++ b/src/main/java/io/github/yezhihao/netmc/core/AbstractHandlerMapping.java @@ -17,7 +17,7 @@ import java.util.Map; */ public abstract class AbstractHandlerMapping implements HandlerMapping { - private final Map handlerMap = new HashMap(60); + private final Map handlerMap = new HashMap<>(64); /** * 将Endpoint中被@Mapping标记的方法注册到映射表 @@ -25,8 +25,6 @@ public abstract class AbstractHandlerMapping implements HandlerMapping { protected synchronized void registerHandlers(Object bean) { Class beanClass = bean.getClass(); Method[] methods = beanClass.getDeclaredMethods(); - if (methods == null) - return; for (Method method : methods) { diff --git a/src/main/java/io/github/yezhihao/netmc/core/handler/AsyncBatchHandler.java b/src/main/java/io/github/yezhihao/netmc/core/handler/AsyncBatchHandler.java index e023c71..767569f 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/handler/AsyncBatchHandler.java +++ b/src/main/java/io/github/yezhihao/netmc/core/handler/AsyncBatchHandler.java @@ -23,17 +23,17 @@ public class AsyncBatchHandler extends Handler { private static final Logger log = LoggerFactory.getLogger(AsyncBatchHandler.class.getSimpleName()); - private ConcurrentLinkedQueue queue; + private final ConcurrentLinkedQueue queue; - private ThreadPoolExecutor executor; + private final ThreadPoolExecutor executor; - private int poolSize; + private final int poolSize; - private int maxElements; + private final int maxElements; - private int maxWait; + private final int maxWait; - private int warningLines; + private final int warningLines; public AsyncBatchHandler(Object actionClass, Method actionMethod, String desc, int poolSize, int maxElements, int maxWait) { super(actionClass, actionMethod, desc); @@ -50,8 +50,7 @@ public class AsyncBatchHandler extends Handler { this.warningLines = maxElements * poolSize * 50; this.queue = new ConcurrentLinkedQueue(); - this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400), - new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build()); + this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400), new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build()); for (int i = 0; i < poolSize; i++) { boolean master = i == 0; @@ -65,7 +64,7 @@ public class AsyncBatchHandler extends Handler { } } - public Message invoke(Message request, Session session) { + public T invoke(T request, Session session) { queue.offer(request); return null; } diff --git a/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java b/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java index b8e2119..4e493df 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java +++ b/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java @@ -34,7 +34,7 @@ public abstract class Handler { try { for (int i = 0; i < types.length; i++) { Type type = types[i]; - Class clazz; + Class clazz; if (type instanceof ParameterizedTypeImpl) clazz = (Class) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0]; else diff --git a/src/main/java/io/github/yezhihao/netmc/core/handler/SimpleHandler.java b/src/main/java/io/github/yezhihao/netmc/core/handler/SimpleHandler.java index 7b0c106..c89e7bc 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/handler/SimpleHandler.java +++ b/src/main/java/io/github/yezhihao/netmc/core/handler/SimpleHandler.java @@ -16,7 +16,7 @@ public class SimpleHandler extends Handler { super(actionClass, actionMethod, desc); } - public Message invoke(Message request, Session session) throws Exception { + public T invoke(T request, Session session) throws Exception { return super.invoke(request, session); } } \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/util/ClassUtils.java b/src/main/java/io/github/yezhihao/netmc/util/ClassUtils.java index 6cf28e7..424b4a1 100644 --- a/src/main/java/io/github/yezhihao/netmc/util/ClassUtils.java +++ b/src/main/java/io/github/yezhihao/netmc/util/ClassUtils.java @@ -8,7 +8,6 @@ import java.lang.annotation.Annotation; import java.net.JarURLConnection; import java.net.URL; import java.util.Enumeration; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.jar.JarEntry; @@ -24,17 +23,12 @@ public class ClassUtils { public static List> getClassList(String packageName, Class annotationClass) { List> classList = getClassList(packageName); - Iterator> iterator = classList.iterator(); - while (iterator.hasNext()) { - Class next = iterator.next(); - if (!next.isAnnotationPresent(annotationClass)) - iterator.remove(); - } + classList.removeIf(next -> !next.isAnnotationPresent(annotationClass)); return classList; } public static List> getClassList(String packageName) { - List> classList = new LinkedList(); + List> classList = new LinkedList<>(); String path = packageName.replace(".", "/"); try { Enumeration urls = ClassUtils.getClassLoader().getResources(path);