Skip to content

Commit

Permalink
fix issues 1002
Browse files Browse the repository at this point in the history
  • Loading branch information
happyday517 committed Jan 22, 2016
1 parent db757a9 commit 8dd7c16
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
54 changes: 49 additions & 5 deletions src/org/nutz/lang/Mirror.java
Expand Up @@ -1820,18 +1820,35 @@ public boolean hasInterface(Class<?> clzInterface) {
return false;
}


@SuppressWarnings({"unchecked", "rawtypes"})
public static <T extends Annotation> T getAnnotationDeep(Method method, Class<T> annotationClass) {
T t = method.getAnnotation(annotationClass);
if (t != null)
return t;
Class klass = method.getDeclaringClass();
Class klass = method.getDeclaringClass().getSuperclass();
while (klass != null && klass != Object.class) {
try {
Method tmp = klass.getMethod(method.getName(), method.getParameterTypes());
t = tmp.getAnnotation(annotationClass);
if (t != null)
return t;
for (Method m : klass.getMethods()) {
if (m.getName().equals(method.getName())) {
Class[] mParameters = m.getParameterTypes();
Class[] methodParameters = method.getParameterTypes();
if (mParameters.length != methodParameters.length)
continue;
boolean match = true;
for (int i = 0; i < mParameters.length; i++) {
if (!mParameters[i].isAssignableFrom(methodParameters[i])) {
match = false;
break;
}
}
if (match) {
t = m.getAnnotation(annotationClass);
if (t != null)
return t;
}
}
}
}
catch (Exception e) {
break;
Expand All @@ -1845,9 +1862,36 @@ public static <T extends Annotation> T getAnnotationDeep(Method method, Class<T>
if (t != null)
return t;
}
catch (Exception e) {}
}
return null;
}

public static <T extends Annotation> T getAnnotationDeep(Class<?> type, Class<T> annotationClass) {
T t = type.getAnnotation(annotationClass);
if (t != null)
return t;
Class<?> klass = type.getSuperclass();
while (klass != null && klass != Object.class) {
try {
t = klass.getAnnotation(annotationClass);
if (t != null)
return t;
}
catch (Exception e) {
break;
}
klass = klass.getSuperclass();
}
for (Class<?> klass2 : type.getInterfaces()) {
try {
t = klass2.getAnnotation(annotationClass);
if (t != null)
return t;
}
catch (Exception e) {}
}
return null;
}

}
28 changes: 14 additions & 14 deletions src/org/nutz/mvc/impl/Loadings.java
Expand Up @@ -53,14 +53,14 @@ public abstract class Loadings {

public static ActionInfo createInfo(Class<?> type) {
ActionInfo ai = new ActionInfo();
evalEncoding(ai, type.getAnnotation(Encoding.class));
evalHttpAdaptor(ai, type.getAnnotation(AdaptBy.class));
evalActionFilters(ai, type.getAnnotation(Filters.class));
evalPathMap(ai, type.getAnnotation(PathMap.class));
evalOk(ai, type.getAnnotation(Ok.class));
evalFail(ai, type.getAnnotation(Fail.class));
evalAt(ai, type.getAnnotation(At.class), type.getSimpleName());
evalActionChainMaker(ai, type.getAnnotation(Chain.class));
evalEncoding(ai, Mirror.getAnnotationDeep(type, Encoding.class));
evalHttpAdaptor(ai, Mirror.getAnnotationDeep(type, AdaptBy.class));
evalActionFilters(ai, Mirror.getAnnotationDeep(type, Filters.class));
evalPathMap(ai, Mirror.getAnnotationDeep(type, PathMap.class));
evalOk(ai, Mirror.getAnnotationDeep(type, Ok.class));
evalFail(ai, Mirror.getAnnotationDeep(type, Fail.class));
evalAt(ai, Mirror.getAnnotationDeep(type, At.class), type.getSimpleName());
evalActionChainMaker(ai, Mirror.getAnnotationDeep(type, Chain.class));
evalModule(ai, type);
if (Mvcs.DISPLAY_METHOD_LINENUMBER) {
InputStream ins = type.getClassLoader().getResourceAsStream(type.getName().replace(".", "/") + ".class");
Expand Down Expand Up @@ -204,13 +204,13 @@ private static void checkModule(Set<Class<?>> modules, List<Class<?>> subs) {
}

public static void evalHttpMethod(ActionInfo ai, Method method, At at) {
if (method.getAnnotation(GET.class) != null)
if (Mirror.getAnnotationDeep(method, GET.class) != null)
ai.getHttpMethods().add("GET");
if (method.getAnnotation(POST.class) != null)
if (Mirror.getAnnotationDeep(method, POST.class) != null)
ai.getHttpMethods().add("POST");
if (method.getAnnotation(PUT.class) != null)
if (Mirror.getAnnotationDeep(method, PUT.class) != null)
ai.getHttpMethods().add("PUT");
if (method.getAnnotation(DELETE.class) != null)
if (Mirror.getAnnotationDeep(method, DELETE.class) != null)
ai.getHttpMethods().add("DELETE");
for (String m : at.methods()) {
ai.getHttpMethods().add(m.toUpperCase());
Expand Down Expand Up @@ -261,8 +261,8 @@ public static void evalModule(ActionInfo ai, Class<?> type) {
ai.setModuleType(type);
String beanName = null;
// 按照5.10.3章节的说明,优先使用IocBean.name的注解声明bean的名字 Modify By QinerG@gmai.com
InjectName innm = type.getAnnotation(InjectName.class);
IocBean iocBean = type.getAnnotation(IocBean.class);
InjectName innm = Mirror.getAnnotationDeep(type,InjectName.class);
IocBean iocBean = Mirror.getAnnotationDeep(type,IocBean.class);
if (innm == null && iocBean == null) // TODO 再考虑考虑
return;
if (iocBean != null) {
Expand Down
7 changes: 4 additions & 3 deletions src/org/nutz/mvc/impl/NutLoading.java
Expand Up @@ -190,10 +190,11 @@ protected UrlMapping evalUrlMapping(NutConfig config, Class<?> mainModule, Ioc i
ActionInfo moduleInfo = Loadings.createInfo(module).mergeWith(mainInfo);
for (Method method : module.getMethods()) {
/*
* public 并且声明了 @At 的函数,才是入口函数
* public 并且声明了 @At 的当前模块函数,且非继承函数,非Bridge函数,才是入口函数
*/
if (!Modifier.isPublic(method.getModifiers())
|| !method.isAnnotationPresent(At.class))
if (!Modifier.isPublic(method.getModifiers()) || method.isBridge()
|| Mirror.getAnnotationDeep(method, At.class) == null
|| method.getDeclaringClass() != module)
continue;
// 增加到映射中
ActionInfo info = Loadings.createInfo(method).mergeWith(moduleInfo);
Expand Down

0 comments on commit 8dd7c16

Please sign in to comment.