From 1bda2f92b9a41527ebe030ff294411ecaf933588 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Tue, 18 Jun 2019 22:59:07 +0800 Subject: [PATCH] =?UTF-8?q?HttpRequest=E5=A2=9E=E5=8A=A0getAnnotation?= =?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/HttpRequest.java | 54 +++++++++++++++++++++++ src/org/redkale/net/http/HttpServlet.java | 18 ++++++-- src/org/redkale/net/http/Rest.java | 15 +++++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/org/redkale/net/http/HttpRequest.java b/src/org/redkale/net/http/HttpRequest.java index bba000486..b009b770c 100644 --- a/src/org/redkale/net/http/HttpRequest.java +++ b/src/org/redkale/net/http/HttpRequest.java @@ -6,6 +6,8 @@ package org.redkale.net.http; import java.io.*; +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.Channels; @@ -69,6 +71,8 @@ public class HttpRequest extends Request { protected int actionid; + protected Annotation[] annotations; + protected Object currentUser; private final String remoteAddrHeader; @@ -298,6 +302,55 @@ public class HttpRequest extends Request { return this.actionid; } + /** + * 获取当前操作Method上的注解集合 + * + * @return Annotation[] + */ + public Annotation[] getAnnotations() { + if (this.annotations == null) return new Annotation[0]; + Annotation[] newanns = new Annotation[this.annotations.length]; + System.arraycopy(this.annotations, 0, newanns, 0, newanns.length); + return newanns; + } + + /** + * 获取当前操作Method上的注解 + * + * @param 注解泛型 + * @param annotationClass 注解类型 + * + * @return Annotation + */ + public T getAnnotation(Class annotationClass) { + if (this.annotations == null) return null; + for (Annotation ann : this.annotations) { + if (ann.getClass() == annotationClass) return (T) ann; + } + return null; + } + + /** + * 获取当前操作Method上的注解集合 + * + * @param 注解泛型 + * @param annotationClass 注解类型 + * + * @return Annotation[] + */ + public T[] getAnnotationsByType(Class annotationClass) { + if (this.annotations == null) return (T[]) Array.newInstance(annotationClass, 0); + T[] news = (T[]) Array.newInstance(annotationClass, this.annotations.length); + int index = 0; + for (Annotation ann : this.annotations) { + if (ann.getClass() == annotationClass) { + news[index++] = (T) ann; + } + } + if (index < 1) return (T[]) Array.newInstance(annotationClass, 0); + return Arrays.copyOf(news, index); + } + /** * 获取客户端地址IP * @@ -443,6 +496,7 @@ public class HttpRequest extends Request { this.bodyparsed = false; this.moduleid = 0; this.actionid = 0; + this.annotations = null; this.currentUser = null; this.attachment = null; diff --git a/src/org/redkale/net/http/HttpServlet.java b/src/org/redkale/net/http/HttpServlet.java index 8247030aa..acec01934 100644 --- a/src/org/redkale/net/http/HttpServlet.java +++ b/src/org/redkale/net/http/HttpServlet.java @@ -6,6 +6,7 @@ package org.redkale.net.http; import java.io.IOException; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.*; @@ -71,6 +72,7 @@ public class HttpServlet extends Servlet request.attachment = entry; request.moduleid = entry.moduleid; request.actionid = entry.actionid; + request.annotations = entry.annotations; if (entry.auth) { response.thenEvent(authSuccessServlet); authenticate(request, response); @@ -210,6 +212,7 @@ public class HttpServlet extends Servlet InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, HttpServlet servlet) { this(moduleid, actionid, name, methods, method, auth(method), cacheseconds(method), servlet); + this.annotations = annotations(method); } //供Rest类使用,参数不能随便更改 @@ -232,16 +235,21 @@ public class HttpServlet extends Servlet } : null; } - private static boolean auth(Method method) { + protected static boolean auth(Method method) { HttpMapping mapping = method.getAnnotation(HttpMapping.class); return mapping == null || mapping.auth(); } - private static int cacheseconds(Method method) { + protected static int cacheseconds(Method method) { HttpMapping mapping = method.getAnnotation(HttpMapping.class); return mapping == null ? 0 : mapping.cacheseconds(); } + //Rest.class会用到此方法 + protected static Annotation[] annotations(Method method) { + return method.getAnnotations(); + } + boolean isNeedCheck() { return this.moduleid != 0 || this.actionid != 0; } @@ -270,9 +278,11 @@ public class HttpServlet extends Servlet final String[] methods; - final Method method; - final HttpServlet servlet; + + Method method; + + Annotation[] annotations; } private HttpServlet createActionServlet(final Method method) { diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index cb90eb5d5..aaecc2a2f 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -1810,9 +1810,10 @@ public final class Rest { // HashMap _createRestInnerActionEntry() { // HashMap map = new HashMap<>(); // map.put("asyncfind3", new InnerActionEntry(100000,200000,"asyncfind3", new String[]{},null,false,0, new _Dync_asyncfind3_HttpServlet())); -// map.put("asyncfind3", new InnerActionEntry(1,2,"asyncfind2", new String[]{"GET", "POST"},null,true,0, new _Dync_asyncfind2_HttpServlet())); +// map.put("asyncfind2", new InnerActionEntry(1,2,"asyncfind2", new String[]{"GET", "POST"},null,true,0, new _Dync_asyncfind2_HttpServlet())); // return map; // } + Map mappingurlToMethod = new HashMap<>(); { //_createRestInnerActionEntry 方法 mv = new MethodDebugVisitor(cw.visitMethod(0, "_createRestInnerActionEntry", "()Ljava/util/HashMap;", "()Ljava/util/HashMap;", null)); //mv.setDebug(true); @@ -1822,6 +1823,7 @@ public final class Rest { mv.visitVarInsn(ASTORE, 1); for (final MappingEntry entry : entrys) { + mappingurlToMethod.put(entry.mappingurl, entry.mappingMethod); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn(entry.mappingurl); //name mv.visitTypeInsn(NEW, innerEntryName); //new InnerActionEntry @@ -1837,9 +1839,9 @@ public final class Rest { mv.visitLdcInsn(entry.methods[i]); mv.visitInsn(AASTORE); } - mv.visitInsn(ACONST_NULL); //method + mv.visitInsn(ACONST_NULL); //method mv.visitInsn(entry.auth ? ICONST_1 : ICONST_0); //auth - pushInt(mv, entry.cacheseconds); //cacheseconds + pushInt(mv, entry.cacheseconds); //cacheseconds mv.visitTypeInsn(NEW, newDynName + "$" + entry.newActionClassName); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); @@ -1920,7 +1922,12 @@ public final class Rest { restactMethod.setAccessible(true); Field tmpentrysfield = HttpServlet.class.getDeclaredField("_tmpentrys"); tmpentrysfield.setAccessible(true); - tmpentrysfield.set(obj, restactMethod.invoke(obj)); + HashMap innerEntryMap = (HashMap) restactMethod.invoke(obj); + for (Map.Entry en : innerEntryMap.entrySet()) { + Method m = mappingurlToMethod.get(en.getKey()); + if (m != null) en.getValue().annotations = HttpServlet.InnerActionEntry.annotations(m); + } + tmpentrysfield.set(obj, innerEntryMap); return obj; } catch (Throwable e) { throw new RuntimeException(e);