Open JoeyBling opened 4 years ago
重写父类方法注解是不会被继承的,不仅仅是JSONField注解,其他注解也不会被继承
看了下源码,应该是TypeUtils.getAnnotation
无法获取继承的注解。
顺带看了下Spring是如何获取继承的注解的。通过AnnotationUtils.findAnnotation
可以正常获取。
贴下代码
// 获取父类注解 Class<?> bClass = B.class; for (Method method : bClass.getDeclaredMethods()) { // FastJson内置获取注解无法获取 JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class); // false System.out.println(annotation != null); annotation = AnnotationUtils.getAnnotation(method, JSONField.class); // false System.out.println(annotation != null); annotation = AnnotationUtils.findAnnotation(method, JSONField.class); // True System.out.println(annotation != null); annotation = AnnotatedElementUtils.findMergedAnnotation(method, JSONField.class); // True System.out.println(annotation != null); }
不知道是FastJson团队设计如此,还是其他的问题... 还是比较希望能把这问题再优化下,或者通过可配置方法进行开启获取父类注解
+1
Demo代码:
输出结果: