Java注解之@Inherited的使用
2017年09月22日 10:39:48 零度anngle 阅读数:6392 标签: java annotation 更多
个人分类: Java注解
一、@Inherited
@Inherited:允许子类继承父类的注解。
二、代码
Java代码
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Inherited
- public @interface DBTable {
- public String name() default "";
- }
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface DBTable2 {
- public String name() default "";
- }
- package com.jyz.study.jdk.reflect;
- import java.util.Arrays;
- import com.jyz.study.jdk.annotation.DBTable;
- import com.jyz.study.jdk.annotation.DBTable2;
- /**
- * 1.演示从Class对象上获得反射元素Field Method Constructor
- * 2.演示AnnotatedElement接口的四个方法
- * @author JoyoungZhang@gmail.com
- *
- */
- public class DeclaredOrNot {
- public static void main(String[] args) {
- Class<Sub> clazz = Sub.class;
- System.out.println("============================Field===========================");
- //public + 继承
- System.out.println(Arrays.toString(clazz.getFields()));
- //all + 自身
- System.out.println(Arrays.toString(clazz.getDeclaredFields()));
- System.out.println("============================Method===========================");
- //public + 继承
- System.out.println(Arrays.toString(clazz.getMethods()));
- //all + 自身
- System.out.println(Arrays.toString(clazz.getDeclaredMethods()));
- System.out.println("============================Constructor===========================");
- //public + 自身
- System.out.println(Arrays.toString(clazz.getConstructors()));
- //all + 自身
- System.out.println(Arrays.toString(clazz.getDeclaredConstructors()));
- System.out.println("============================AnnotatedElement===========================");
- //注解DBTable2是否存在于元素上
- System.out.println(clazz.isAnnotationPresent(DBTable2.class));
- //如果存在该元素的指定类型的注释DBTable2,则返回这些注释,否则返回 null。
- System.out.println(clazz.getAnnotation(DBTable2.class));
- //继承
- System.out.println(Arrays.toString(clazz.getAnnotations()));
- //自身
- System.out.println(Arrays.toString(clazz.getDeclaredAnnotations()));
- }
- }
- @DBTable
- class Super{
- private int superPrivateF;
- public int superPublicF;
- public Super(){
- }
- private int superPrivateM(){
- return 0;
- }
- public int superPubliceM(){
- return 0;
- }
- }
- @DBTable2
- class Sub extends Super{
- private int subPrivateF;
- public int subPublicF;
- private Sub(){
- }
- public Sub(int i){
- }
- private int subPrivateM(){
- return 0;
- }
- public int subPubliceM(){
- return 0;
- }
- }
- console output:
- ============================Field===========================
- [public int com.jyz.study.jdk.reflect.Sub.subPublicF, public int com.jyz.study.jdk.reflect.Super.superPublicF]
- [private int com.jyz.study.jdk.reflect.Sub.subPrivateF, public int com.jyz.study.jdk.reflect.Sub.subPublicF]
- ============================Method===========================
- [public int com.jyz.study.jdk.reflect.Sub.subPubliceM(), public int com.jyz.study.jdk.reflect.Super.superPubliceM(), public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]
- [private int com.jyz.study.jdk.reflect.Sub.subPrivateM(), public int com.jyz.study.jdk.reflect.Sub.subPubliceM()]
- ============================Constructor===========================
- [public com.jyz.study.jdk.reflect.Sub(int)]
- [private com.jyz.study.jdk.reflect.Sub(), public com.jyz.study.jdk.reflect.Sub(int)]
- ============================AnnotatedElement===========================
- true
- @com.jyz.study.jdk.annotation.DBTable2(name=)
- [@com.jyz.study.jdk.annotation.DBTable(name=), @com.jyz.study.jdk.annotation.DBTable2(name=)]
- [@com.jyz.study.jdk.annotation.DBTable2(name=)]
三、代码说明
- 代码演示了从Class对象上获得反射元素Field Method Constructor时get*和getDeclared*的区别。
- 代码演示了AnnotatedElement接口的四个方法。
- java.lang.reflect.AnnotatedElement表示可以被注解的元素。它只有四个方法,参考代码DeclaredOrNot.java。
- 当我使用clazz.getAnnotations()时,我期望得到控制台打印出来的内容,但实际上却只得到了[@com.jyz.study.jdk.annotation.DBTable2(name=)],后来发现是DBTable里没有声明@Inherited。
- java.lang.reflect.AnnotatedElement表示可以被注解的元素。它只有四个方法,参考代码DeclaredOrNot.java。