From 95c3354fcdd9b63ff6d8abdbe5869a7f5320783a Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Wed, 19 Jun 2019 15:47:46 +0800 Subject: [PATCH] =?UTF-8?q?WebSocketParam=E5=A2=9E=E5=8A=A0getAnnotations?= =?UTF-8?q?=E7=B3=BB=E5=88=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/http/Rest.java | 31 ++++++++++++++++++- src/org/redkale/net/http/WebSocketParam.java | 29 ++++++++++++++++- .../wsdync/_DyncChatWebSocketServlet.java | 21 ++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index aaecc2a2f..b39018b93 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -458,6 +458,12 @@ public final class Rest { av0.visitEnd(); fv.visitEnd(); } + {//_redkale_annotations + fv = cw2.visitField(ACC_PUBLIC + ACC_STATIC, "_redkale_annotations", "[Ljava/lang/annotation/Annotation;", null, null); + av0 = fv.visitAnnotation(convertDisabledDesc, true); + av0.visitEnd(); + fv.visitEnd(); + } { //空构造函数 mv = new MethodDebugVisitor(cw2.visitMethod(ACC_PUBLIC, "", "()V", null, null)); mv.visitVarInsn(ALOAD, 0); @@ -504,6 +510,25 @@ public final class Rest { mv.visitMaxs(2, 2); mv.visitEnd(); } + { + mv = new MethodDebugVisitor(cw2.visitMethod(ACC_PUBLIC, "getAnnotations", "()[Ljava/lang/annotation/Annotation;", null, null)); + mv.visitFieldInsn(GETSTATIC, newDynMessageFullName + endfix, "_redkale_annotations", "[Ljava/lang/annotation/Annotation;"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNONNULL, l1); + mv.visitInsn(ICONST_0); + mv.visitTypeInsn(ANEWARRAY, "java/lang/annotation/Annotation"); + mv.visitInsn(ARETURN); + mv.visitLabel(l1); + mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); + mv.visitFieldInsn(GETSTATIC, newDynMessageFullName + endfix, "_redkale_annotations", "[Ljava/lang/annotation/Annotation;"); + mv.visitFieldInsn(GETSTATIC, newDynMessageFullName + endfix, "_redkale_annotations", "[Ljava/lang/annotation/Annotation;"); + mv.visitInsn(ARRAYLENGTH); + mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "copyOf", "([Ljava/lang/Object;I)[Ljava/lang/Object;", false); + mv.visitTypeInsn(CHECKCAST, "[Ljava/lang/annotation/Annotation;"); + mv.visitInsn(ARETURN); + mv.visitMaxs(2, 1); + mv.visitEnd(); + } { //execute mv = new MethodDebugVisitor(cw2.visitMethod(ACC_PUBLIC, "execute", "(L" + newDynWebSokcetFullName + ";)V", null, null)); mv.visitVarInsn(ALOAD, 0); @@ -543,7 +568,11 @@ public final class Rest { mv.visitEnd(); } cw2.visitEnd(); - newLoader.loadClass((newDynMessageFullName + endfix).replace('/', '.'), cw2.toByteArray()); + try { + newLoader.loadClass((newDynMessageFullName + endfix).replace('/', '.'), cw2.toByteArray()).getField("_redkale_annotations").set(null, method.getAnnotations()); + } catch (Exception e) { + throw new RuntimeException(e); + } } { //_DynXXXWebSocketMessage class diff --git a/src/org/redkale/net/http/WebSocketParam.java b/src/org/redkale/net/http/WebSocketParam.java index 927bc6d53..6fdd99bc4 100644 --- a/src/org/redkale/net/http/WebSocketParam.java +++ b/src/org/redkale/net/http/WebSocketParam.java @@ -5,6 +5,10 @@ */ package org.redkale.net.http; +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; +import java.util.Arrays; + /** * * 供WebSocket.preOnMessage 方法获取RestWebSocket里OnMessage方法的参数
@@ -16,6 +20,29 @@ package org.redkale.net.http; public interface WebSocketParam { public T getValue(String name); - + public String[] getNames(); + + public Annotation[] getAnnotations(); + + default T getAnnotation(Class annotationClass) { + for (Annotation ann : getAnnotations()) { + if (ann.getClass() == annotationClass) return (T) ann; + } + return null; + } + + default T[] getAnnotationsByType(Class annotationClass) { + Annotation[] annotations = getAnnotations(); + if (annotations == null) return (T[]) Array.newInstance(annotationClass, 0); + T[] news = (T[]) Array.newInstance(annotationClass, annotations.length); + int index = 0; + for (Annotation ann : annotations) { + if (ann.getClass() == annotationClass) { + news[index++] = (T) ann; + } + } + if (index < 1) return (T[]) Array.newInstance(annotationClass, 0); + return Arrays.copyOf(news, index); + } } diff --git a/test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java b/test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java index 3bfe24c36..f59ecfd79 100644 --- a/test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java +++ b/test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java @@ -6,7 +6,8 @@ package org.redkale.test.wsdync; import java.io.Serializable; -import java.util.Map; +import java.lang.annotation.Annotation; +import java.util.*; import java.util.function.BiConsumer; import javax.annotation.Resource; import org.redkale.convert.ConvertDisabled; @@ -70,6 +71,9 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet { @ConvertDisabled public _DyncChatWebSocket _redkale_websocket; + @ConvertDisabled + public static Annotation[] _redkale_annotations; + @Override public String[] getNames() { return new String[]{"message", "extmap"}; @@ -82,6 +86,12 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet { return null; } + @Override + public Annotation[] getAnnotations() { + if (_redkale_annotations == null) return new Annotation[0]; + return Arrays.copyOf(_redkale_annotations, _redkale_annotations.length); + } + public void execute(_DyncChatWebSocket websocket) { this._redkale_websocket = websocket; websocket.preOnMessage("sendmessage", this, this); @@ -105,6 +115,9 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet { @ConvertDisabled public _DyncChatWebSocket _redkale_websocket; + @ConvertDisabled + public static Annotation[] _redkale_annotations; + @Override public String[] getNames() { return new String[]{"roomid"}; @@ -116,6 +129,12 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet { return null; } + @Override + public Annotation[] getAnnotations() { + if (_redkale_annotations == null) return new Annotation[0]; + return Arrays.copyOf(_redkale_annotations, _redkale_annotations.length); + } + public void execute(_DyncChatWebSocket websocket) { this._redkale_websocket = websocket; websocket.preOnMessage("joinroom", this, this);