WebSocketParam增加getAnnotations系列方法

This commit is contained in:
Redkale
2019-06-19 15:47:46 +08:00
parent 1bda2f92b9
commit 95c3354fcd
3 changed files with 78 additions and 3 deletions

View File

@@ -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, "<init>", "()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

View File

@@ -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方法的参数 <br>
@@ -16,6 +20,29 @@ package org.redkale.net.http;
public interface WebSocketParam {
public <T> T getValue(String name);
public String[] getNames();
public Annotation[] getAnnotations();
default <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
for (Annotation ann : getAnnotations()) {
if (ann.getClass() == annotationClass) return (T) ann;
}
return null;
}
default <T extends Annotation> T[] getAnnotationsByType(Class<T> 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);
}
}

View File

@@ -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);