TogetherOS / cicada

🚀 Fast lightweight HTTP service framework.
https://crossoverjie.top/categories/cicada/
Apache License 2.0
953 stars 212 forks source link

Interceptor加载优化 #23

Closed binarytom closed 5 years ago

binarytom commented 5 years ago

Describe the bug 在加载Interceptor时候的优化;

To Reproduce ClassScanner类中的getCicadaInterceptor方法,原实现如下:

for (Class<?> cls : clsList) {

    if (cls.getAnnotation(Interceptor.class) == null) {
        continue;
    }

    Annotation[] annotations = cls.getAnnotations();
    for (Annotation annotation : annotations) {
        if (!(annotation instanceof Interceptor)) {
            continue;
        }
        Interceptor interceptor = (Interceptor) annotation;
        interceptorMap.put(interceptor.order(), cls);
    }
}

建议改为:

for (Class<?> cls : clsList) {
    Annotation annotation = cls.getAnnotation(Interceptor.class);
    if (annotation == null) {
        continue;
    }

    Interceptor interceptor = (Interceptor) annotation;
    interceptorMap.put(interceptor.order(), cls);
}

Expected behavior 只需要执行一次cls.getAnnotation,没有必要取第二次遍历判断。

crossoverJie commented 5 years ago

@chosenboy

可以提交 PRfix 分支。

binarytom commented 5 years ago

@crossoverJie ok